Html5 clock implementation code

Source: Internet
Author: User

Comments: The alarm code implemented in html5. For more information, see.
The Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Style type = "text/css">
Canvas {position: absolute; top: 0px; left: 0px ;}
</Style>
<Title> clock </title>
</Head>
<Body>
<Canvas id = "canvas" width = "200" height = "200"> </canvas>
<Canvas id = "p_canvas" width = "200" height = "200"> </canvas>
<Script type = "text/javascript">
// Obtain the Drawing Object
Var canvas = document. getElementById ('canvas ');
Var context = canvas. getContext ('2d ');

Var p_canvas = document. getElementById ('P _ canvas ');
Var p_context = p_canvas.getContext ('2d ');

Var height = 200, width = 200;
// Draw a circle
Context. beginPath ();
Context. strokeStyle = "#009999 ";
Context. arc (width/2, height/2, width/2-1, 0, Math. PI * 2, true );
Context. stroke ();
Context. closePath ();
// Draw the center point
Context. beginPath ();
Context. fillStyle = "#000 ";
Context. arc (width/2, height/2, 0, Math. PI * 2, true );
Context. fill ();
Context. closePath ();

// Draw a small scale
Var angle = 0, radius = width/2-4;
For (var I = 0; I <60; I ++ ){
Context. beginPath ();
Context. strokeStyle = "#000 ";
// Confirm the start point of the Scale
Var x = width/2 + radius * Math. cos (angle), y = height/2 + radius * Math. sin (angle );
Context. moveTo (x, y );
// This is used to point another point of the scale to the center point and give the correct angle.
// PI is 180 degrees. The correct angle is angle + 180 degrees, which is in the opposite direction.
Var temp_angle = Math. PI + angle;
Context. lineTo (x + 3 * Math. cos (temp_angle), y + 3 * Math. sin (temp_angle ));
Context. stroke ();
Context. closePath ();
Angle ++ = 6/180 * Math. PI;
}
// Large Scale
Angle = 0, radius = width/2-4;
Context. textBaseline = 'middle ';
Context. textAlign = 'center ';
Context. lineWidth = 2;
For (var I = 0; I <12; I ++ ){
Var num = I + 3> 12? I + 3-12: I + 3;
Context. beginPath ();
Context. strokeStyle = "# FFD700 ";
Var x = width/2 + radius * Math. cos (angle), y = height/2 + radius * Math. sin (angle );
Context. moveTo (x, y );
Var temp_angle = Math. PI + angle;
Context. lineTo (x + 8 * Math. cos (temp_angle), y + 8 * Math. sin (temp_angle ));
Context. stroke ();
Context. closePath ();
// Large scale text
Context. fillText (num, x + 16 * Math. cos (temp_angle), y + 16 * Math. sin (temp_angle ));
Angle ++ = 30/180 * Math. PI;
}

Function Pointer (){
Var p_type = [['# 000',], [' # ccc ',], ['red',];
Function drwePointer (type, angle ){
Type = p_type [type];
Angle = angle * Math. PI * 2-90/180 * Math. PI;
Var length = type [1];
P_context.beginPath ();
P_context.lineWidth = type [2];
P_context.strokeStyle = type [0];
P_context.moveTo (width/2, height/2 );
P_context.lineTo (width/2 + length * Math. cos (angle), height/2 + length * Math. sin (angle ));
P_context.stroke ();
P_context.closePath ();

}
SetInterval (function (){
P_context.clearRect (0, 0, height, width );
Var time = new Date ();
Var h = time. getHours ();
Var m = time. getMinutes ();
Var s = time. getSeconds ();
H = h> 12? H-12: h;
H = h + m/60;
H = h/12;
M = m/60;
S = s/60;
DrwePointer (0, s );
DrwePointer (1, m );
DrwePointer (2, h );
},500 );
}
Var p = new Pointer ();
</Script>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.