How to draw a watch by canvas _HTML5 Tutorial Tips

Source: Internet
Author: User
Tags drawtext
This article mainly introduced the HTML5 in the canvas how to draw the information of the method of the clock, small series think that HTML is really more and more powerful, and now share to everyone, but also for everyone to do a reference. For the little Friends of HTML, you can follow the small part together.

This article describes how canvas can be used to draw a clock and share it with you, as follows:

For

On the code:


var canvas = document.getElementById (' canvas '); var ctx = Canvas.getcontext (' 2d '); var year,month,day,hour,second,    minute;//Draw dial Function Drawclockpie () {Ctx.beginpath ();    Ctx.linewidth = 2;    Ctx.strokestyle = ' #333 ';    Ctx.arc (150,150,146,0,2*MATH.PI);    Ctx.stroke ();    Ctx.closepath ();    Ctx.beginpath ();    Ctx.arc (150,150,6,0,2*MATH.PI);    Ctx.fillstyle = ' red ';    Ctx.fill (); Ctx.closepath ();}        Draw the Time function drawclockhours () {for (var i = 0,l = n; i <; i++) {ctx.save ();        Ctx.translate (150,150);        Ctx.rotate (I*1/12*2*MATH.PI);        Ctx.beginpath ();        Ctx.linewidth = 5;        Ctx.strokestyle = ' #333 ';        Ctx.moveto (0,-140);        Ctx.lineto (0,-125);        Ctx.stroke ();        Ctx.closepath ();    Ctx.restore ();        }}//Draw the sub-scale function drawclockmins () {for (var i = 0,l =; i <; i++) {ctx.save ();        Ctx.translate (150,150);        Ctx.rotate (I*1/60*2*MATH.PI);        Ctx.beginpath ();   Ctx.linewidth = 3;     Ctx.strokestyle = ' #333 ';        Ctx.moveto (0,-140);        Ctx.lineto (0,-133);        Ctx.stroke ();        Ctx.closepath ();    Ctx.restore ();    }}//draw the Hour function Drawhourpin () {ctx.save ();    Ctx.translate (150,150);    Ctx.rotate ((Hour*60*60+minute*60+second)/(12*60*60) *2*math.pi);    Ctx.beginpath ();    Ctx.strokestyle = ' #333 ';    Ctx.linewidth = 3;    Ctx.moveto (0,0);    Ctx.lineto (0,-40);    Ctx.stroke ();    Ctx.closepath (); Ctx.restore ();}    Draw the minute hand function Drawminpin () {ctx.save ();    Ctx.translate (150,150);    Ctx.rotate ((Minute*60+second)/(60*60) *2*math.pi);    Ctx.beginpath ();    Ctx.strokestyle = ' #333 ';    Ctx.linewidth = 2;    Ctx.moveto (0,0);    Ctx.lineto (0,-60);    Ctx.stroke ();    Ctx.closepath (); Ctx.restore ();}    Draw the second hand function Drawsecpin () {ctx.save ();    Ctx.translate (150,150);    Ctx.rotate (SECOND/60*2*MATH.PI);    Ctx.beginpath ();    Ctx.strokestyle = ' red ';    Ctx.linewidth = 1;    Ctx.moveto (0,0);    Ctx.lineto (0,-80);    Ctx.stroke (); CtX.closepath (); Ctx.restore ();}    Draw Time text function DrawText () {hour = Hour >=? hour-12:hour;    var time = ' Now is ' + year + ' years ' + month + ' month ' + day + ' date ' + hour + ' when ' + minute + ' + ' + second + ' seconds ';    Ctx.font = ' 15px blackbody '; Ctx.filltext (time,24,350);}    Get Time function Gettimes () {var date = new Date ();    Year = Date.getfullyear ();    month = Date.getmonth () + 1;    Day = Date.getdate ();    hour = Date.gethours ();    minute = Date.getminutes (); Second = Date.getseconds ();}    SetInterval (function () {ctx.clearrect (0,0,600,600);    Drawclockpie ();    Drawclockhours ();    Drawclockmins ();    Gettimes ();    DrawText ();    Drawhourpin ();    Drawminpin (); Drawsecpin ();},1000);


Note:

Of course, time can not be obtained every second, the direct acquisition of a second increment on the line.

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.