HTML5 canvas Painting Clock

Source: Internet
Author: User
Tags drawtext time and seconds

This example uses HTML5 's canvas label and JavaScript script to simulate the display of a clock, using a browser preview effect that supports HTML5:

HTML section:

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">    <title>Canvas Drawing</title></Head><Bodyonload= "init ()"><CanvasID= "Canvas"width= "200px"Height= "200px"></Canvas></Body></HTML>

JavaScript section:

functioninit () {varCanvas = document.getElementById ("Canvas"), Context= Canvas.getcontext ("2d"); SetInterval (function() {Draw (canvas, context)},1000);}functionDraw (canvas, context) {varx =canvas.width, y=Canvas.height, R= Math.min (X/2, Y/2); Context.clearrect (0, 0, x, y);//Clear Painting History    //Painting Clock BoxContext.fillstyle = "#f1f1f1";    Drawcircle (context, x, Y, R); //Painting Text    vartx = X/2,ty = Y/2,TR = 0.8*R; Context.font= "Bold 12px Microsoft Jas Black"; Context.fillstyle= "#000"; DrawText (Context,"1", TX + 0.5*tr,ty-0.866*tr); DrawText (Context,"2", TX + 0.866*TR, ty-0.5*tr); DrawText (Context,"3", TX +tr, Ty); DrawText (Context,"4", TX + 0.866*tr, Ty + 0.5*tr); DrawText (Context,"5", TX + 0.5*tr, Ty + 0.866*tr); DrawText (Context,"6", TX, Ty +tr); DrawText (Context,"7", tx-0.5*tr, Ty + 0.866*tr); DrawText (Context,"8", tx-0.866*tr, Ty + 0.5*tr); DrawText (Context,"9", TX-tr, Ty); DrawText (Context,"Ten", Tx-0.866*tr, ty-0.5*tr); DrawText (Context,"One", Tx-0.5*tr, ty-0.866*tr); DrawText (Context,"A", TX, Ty-tr); //Get current Time    varDate =NewDate (), H=date.gethours (), M=date.getminutes (), S=date.getseconds (), Angleh= (360/12) *math.pi/180, Anglem= (360/60) *math.pi/180Context.strokesyle= "#000"; //draw a time degreeDrawscale (context, x, Y, R, Angleh, -0.88*r, -0.96*r, 3); //draw a sub-scaleDrawscale (context, x, Y, R, Anglem, -0.93*r, -0.96*r, 1); //drawing time and seconds needlesDrawcircle (context, x, Y, 3); Drawneedle (context, x, Y, R, H*angleh + M*ANGLEM/12, -0.5*r);    Drawneedle (context, x, Y, R, M*anglem + S*ANGLEM/60, -0.6*r); Drawneedle (context, x, Y, R, S*anglem, -0.75*r);}//Painting Circlefunctiondrawcircle (context, x, Y, R) {context.save ();    Context.beginpath (); Context.arc (x/2, Y/2, R, 0, math.pi*2, 0);    Context.fill ();    Context.closepath (); Context.restore ();}//Drawing Text MethodfunctionDrawText (context, text, x, y) {context.save (); X-= (Context.measuretext (text). WIDTH/2); Y + = 4;    Context.translate (x, y); Context.filltext (text,0, 0); Context.restore ();}//draw a scale methodfunctionDrawscale (context, x, Y, R, rotate, start, end, linewidth) {context.save ();    Context.beginpath (); Context.translate (x/2,y/2); Context.linewidth=linewidth;  for(vari = 0; I < 60; i++) {context.rotate (rotate); Context.moveto (0, start); Context.lineto (0, end);    } context.closepath ();    Context.stroke (); Context.restore ();}//how to draw a needlefunctionDrawneedle (context, x, Y, R, rotate, line) {Context.save (); Context.translate (x/2,y/2);    Context.beginpath ();    Context.rotate (rotate); Context.moveto (0, 0.1*R); Context.lineto (0, line);    Context.closepath ();    Context.stroke (); Context.restore ();}

HTML5 canvas Painting Clock

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.