<! DOCTYPE html>
Then start working on the canvas
<script>//gets the canvas label and creates the context object var canvas = document.getElementById (' canvas '), context = Canvas.getcontext ( ' 2d '), deg = Math.pi/180;context.translate (at;</script>)
Description: The GetContext ("2d") object is a built-in HTML5 object that has a variety of drawing paths, rectangles, circles, characters, and methods for adding images, Deg calculates the position of the Pi, translate () canvas.
1. Create dial, number, scale, center point
Create a watch face
Context.beginpath (); Context.arc (0, 0, deg, 0,); context.linewidth = 3;context.stroke (); Context.closepath ();
Create a number
Create a number for (var i = 1; i <=; i++) { context.beginpath (); Context.save (); Context.rotate (* * i * deg); context.textalign = ' center '; if (i% 3 = = 0) { Context.fillstyle = ' red '; Context.font = "normal 28px Arial"; Context.filltext (i, 0, -110); } else { Context.font = "normal 20px Arial"; Context.filltext (i, 0, -120); } Context.restore (); Context.closepath ();}
Create a scale
for (var i = 1; i <=; i++) { context.beginpath (); Context.save (); Context.rotate (6 * i * deg); Context.moveto (0, -150); Judging the scale display color if (i% = = 0) { Context.strokestyle = ' red '; Context.linewidth = 3; Context.lineto (0, -135); Context.stroke (); } else if (i% 5 = = 0) { context.strokestyle = ' orange '; Context.linewidth = 2; Context.lineto (0, -140); Context.stroke (); } else { Context.strokestyle = ' #000 '; Context.linewidth = 1; Context.lineto (0, -145); Context.stroke (); } Context.restore (); Context.closepath ();}
Create a center point
Context.beginpath (); Context.arc (0, 0, 5, 0, deg); Context.fill (); Context.closepath ();
:
2. Creating pointers
var nowdate = new Date (), hour = nowdate.gethours ()%, Minu = Nowdate.getminutes (), second = Nowdate.getsec Onds (); var ms = Nowdate.getmilliseconds (); MS//Seconds Context.beginpath (); Context.save (); Context.linewidth = 1; Context.strokestyle = ' red '; Context.rotate (6*SECOND*DEG); Context.rotate ((ms/1000 + second) * 6 * deg); Context.moveto (0, 20); Context.lineto (0,-130); Context.stroke (); Context.restore (); Context.closepath (); Minute hand context.beginpath (); Context.save (); Context.linewidth = 2; Context.strokestyle = ' orange '; Context.rotate ((Second/60+minu) *6*deg); Context.rotate ((ms/1000/60 + second/60 + minu) * 6 * deg); Context.moveto (0, 10); Context.lineto (0,-120); Context.stroke (); Context.restore (); Context.closepath (); Hour Context.beginpath (); Context.save (); Context.linewidth = 3; Context.strokestyle = ' #000 '; Context.rotate ((second/3600+minu/60+hour) *30*deg); Context.rotate ((ms/1000/60/60 + second/60/60 + MINU/60 + hour) * * deg); ConText.moveto (0, 0); Context.lineto (0,-110); Context.stroke (); Context.restore (); Context.closepath ();
:
Do not think to the end of the present, I loudly tell you no, now is just the beginning, the next is to witness the miracle of the moment ...
3. Final completion
We need to encapsulate the above drawing into a method, and then keep drawing it all the way, and the clock is moving.
function Dialplate () {//Create dial//context.clearrect ( -150,-150,400,400);//Clear Canvas context.beginpath (); Context.arc (0, 0, 0, deg); Context.linewidth = 3; Context.stroke (); Context.closepath (); Create tick for (var i = 1; i <=; i++) {Context.beginpath (); Context.save (); Context.rotate (6 * i * deg); Context.moveto (0,-150); if (i% = = 0) {Context.strokestyle = ' red '; Context.linewidth = 3; Context.lineto (0,-135); Context.stroke (); } else if (i% 5 = = 0) {Context.strokestyle = ' orange '; Context.linewidth = 2; Context.lineto (0,-140); Context.stroke (); } else {context.strokestyle = ' #000 '; Context.linewidth = 1; Context.lineto (0,-145); Context.stroke (); } context.restore (); Context.closepath (); }//Create a number for (var i = 1; i <=; i++) {Context.beginpath (); Context.save (); Context.rotate (* * i * deg); context.textalign = ' center '; if (i% 3 = = 0) {Context.fillstyle = ' red '; Context.font = "normal 28px Arial"; Context.filltext (i, 0,-110); } else {Context.font = "normal 20px Arial"; Context.filltext (i, 0,-120); } context.restore (); Context.closepath (); }//center point Context.beginpath (); Context.arc (0, 0, 5, 0, deg); Context.fill (); Context.closepath ();} function Pointer () {//create pointer var nowdate = new Date (), hour = nowdate.gethours ()%, Minu = Nowdate.getm Inutes (), second = Nowdate.getseconds (); var ms = Nowdate.getmilliseconds (); MS//Seconds Context.beginpath (); Context.save (); Context.linewidth = 1; Context.strokestyle = ' red '; Context.rotate (6*SECOND*DEG); Context.rotate ((ms/1000 + second) * 6 * deg); Context.moveto (0, 20); ContexT.lineto (0,-130); Context.stroke (); Context.restore (); Context.closepath (); Minute hand context.beginpath (); Context.save (); Context.linewidth = 2; Context.strokestyle = ' orange '; Context.rotate ((Second/60+minu) *6*deg); Context.rotate ((ms/1000/60 + second/60 + minu) * 6 * deg); Context.moveto (0, 10); Context.lineto (0,-120); Context.stroke (); Context.restore (); Context.closepath (); Hour Context.beginpath (); Context.save (); Context.linewidth = 3; Context.strokestyle = ' #000 '; Context.rotate ((second/3600+minu/60+hour) *30*deg); Context.rotate ((ms/1000/60/60 + second/60/60 + MINU/60 + hour) * * deg); Context.moveto (0, 0); Context.lineto (0,-110); Context.stroke (); Context.restore (); Context.closepath ();} Dialplate (); Pointer (); SetInterval (function () {dialplate (); Pointer ();},1000/60)
Description: Animations are best performed 60 times per second, so the timer does not allow him to execute 60 times in seconds.