Canvas Draw Clock

Source: Internet
Author: User

<! DOCTYPE html>
<title></title>
<body>
<canvas id= "Clock" width= "height=" ></canvas>
<script src= ". /js/jquery-1.9.0.js "></script>
<script type= "Text/javascript" >
function Canvasclock (ID) {
var canvas = document.getElementById (ID);
var context = Canvas.getcontext ("2d");
Context.clearrect (0, 0, 400, 400);
Draw a Circle
Context.strokestyle = "Purple";
Context.beginpath ();
Context.arc (0, Math.PI * 2, true);
Context.closepath ();
Context.stroke ();
Draw minute Ticks
for (var i = 0; i <; i++) {
if (i% 5 = = 0) {
Continue
}
Context.save ();
Context.linewidth = 1;
Context.strokestyle = "#000";
Context.translate (150, 150);
Context.rotate (i * 6 * math.pi/180);
Context.beginpath ();
Context.moveto (0,-100);
Context.lineto (0,-90);
Context.closepath ();
Context.stroke ();
Context.restore ();
};
Draw the clock scale
for (var i = 0; i <; i++) {
Context.save ();
Context.linewidth = 2;
Context.strokestyle = "#000";
Context.translate (150, 150);
Context.rotate (i * * math.pi/180);
Context.beginpath ();
Context.moveto (0,-100);
Context.lineto (0,-85);
Context.closepath ();
Context.stroke ();
Context.restore ();
};
var date = new Date ();
var hour = date.gethours ();
var minutes = date.getminutes ();
var sec = Date.getseconds ();
Hour = hour + minutes/60;
hour = hour > 12? Hour-12:hour;
Clock line
Context.save ();
Context.linewidth = 5;
Context.strokestyle = "#4cff00";
Context.translate (150, 150);
Context.rotate (Hour * * math.pi/180);
Context.beginpath ();
Context.moveto (0,-70);
Context.lineto (0, 10);
Context.closepath ();
Context.stroke ();
Context.restore ();

Minute line
Context.save ();
Context.linewidth = 3;
Context.strokestyle = "#b200ff";
Context.translate (150, 150);
Context.rotate (minutes * 6 * math.pi/180);
Context.beginpath ();
Context.moveto (0,-80);
Context.lineto (0, 10);
Context.closepath ();
Context.stroke ();
Context.restore ();

Seconds line
Context.save ();
Context.linewidth = 1.5;
Context.strokestyle = "#ff00dc";
Context.translate (150, 150);
Context.rotate (SEC * 6 * math.pi/180);
Context.beginpath ();
Context.moveto (0,-90);
Context.lineto (0, 10);
Context.closepath ();
Context.stroke ();
Context.restore ();
};
SetInterval (function () {
Canvasclock ("clock");
}, 1000);
</script>
</body>

Canvas Draw 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.