This example for you to share the use of canvas to draw a clock specific code for your reference, the specific contents are as follows
1. clock.html
2. JavaScript code
<script> var Canvas=document.getelementbyid ("Canvas");
var context=canvas.getcontext (' 2d ');
Draw ();
The function Draw () {//Gets the current system's time in minutes var now=new Date ();
var sec=now.getseconds ();
var min=now.getminutes ();
var hour=now.gethours ();
hour>=12&& (HOUR=HOUR-12);
var radius = math.min (CANVAS.WIDTH/2,CANVAS.HEIGHT/2);
Initialize canvas context.save ();
Context.clearrect (0,0,canvas.width,canvas.height);
Context.translate (CANVAS.WIDTH/2,CANVAS.HEIGHT/2);
Context.rotate (-MATH.PI/2);
Context.save ();
Table frame//hour scale context.strokestyle= "black";
Context.fillstyle= "BLACK";
context.linewidth=3;
context.linecap= "Round";
Context.beginpath ();
for (Var i=0;i<12;i++) {context.rotate (MATH.PI/6);
Context.moveto (radius-30,0);
Context.lineto (radius-10,0);
} context.stroke ();
Context.restore (); Context.save ();
Minute scale context.linewidth=2;
Context.beginpath ();
for (Var i=0;i<60;i++) {if (!i%5==0) {Context.moveto (radius-15,0);
Context.lineto (radius-10,0);
} context.rotate (MATH.PI/30);
} context.stroke ();
Context.restore ();
Context.save ();
Draw Hand Context.rotate (MATH.PI/6) *hour+ (math.pi/360) *min+ (math.pi/21600) *sec);
context.linewidth=6;
Context.beginpath ();
Context.moveto ( -10,0);
Context.lineto (radius*0.5,0);
Context.stroke ();
Context.restore ();
Context.save ();
Context.rotate ((MATH.PI/30) *min+ (math.pi/1800) *sec);
Context.strokestyle= "#29A8DE";
context.linewidth=4;
context.linecap= "Butt";
Context.beginpath ();
Context.moveto ( -20,0);
Context.lineto (radius*0.7,0);
Context.stroke ();
Context.restore ();
Context.save ();
Context.rotate (SEC*MATH.PI/30);
Context.strokestyle= "Red"; Context.linewidth=2;
context.linecap= "Butt";
Context.beginpath ();
Context.moveto ( -30,0);
Context.lineto (radius*0.9,0);
Context.stroke ();
Context.restore ();
Context.save ();
context.linewidth=4;
Context.strokestyle= "Gray";
Context.beginpath ();
Context.arc (0,0,radius,0,math.pi*2,true);
Context.stroke ();
Context.restore ();
Context.restore ();
} window.onload=function () {setinterval (draw,1000)} </script>
The above is the entire content of this article, I hope to learn JavaScript program to help you.