Web front-end development learning----9. Use canvas to draw a clock

Source: Internet
Author: User

The importance of canvas elements in HTML5 is unusual. With JS lets us achieve the powerful function of painting in the browser page. No longer rely solely on images, flash and other external files, reduce HTTP requests, resulting in faster page loading.

Simple line Segments:

See a little demo canvas small House

Do not use the function of dead people ....

Rotating:

Use the Rotate () method provided. It should be noted that the origin should be repositioned before rotation. In general, the endpoints of the images that will be rotated are redirected to the origin, which is the basis for rotation. That means positioning first, then setting the rotation angle, and finally drawing. Watch a demo canvas fan. This is much more convenient than a line of drawing.

To fill an image with a gradient color:

is divided into radial and linear gradients, somewhat similar to the CSS3 gradient operation. No need to use the image to do the gradient background.

See a demo canvas stereoscopic ball.

With these methods you can do a clock, pure canvas+js, not using pictures.

View effects Canvas Clock

The complete code:

[HTML]View Plaincopy
  1. <span style="Font-family:microsoft yahei;font-size:14px;" ><! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <metac charset=UTF-8>
  5. <title> Clock </title>
  6. </head>
  7. <body>
  8. <canvas id="Canvas" width="height=" > Please update the browser version </ Canvas>
  9. <script>
  10. var can=document.getElementById ("canvas");
  11. var cxt=Can.getcontext ("2d");
  12. function Drawclock () {
  13. Clear Canvas
  14. Cxt.clearrect (0, 0, 500, 500);
  15. Get current time
  16. var time=New Date ();
  17. var hours=time.gethours ();
  18. var mins=time.getminutes ();
  19. var secs=time.getseconds ();
  20. Convert to 12 binary
  21. hours=hours>12?hours-12:hours;
  22. The hour must get floating point type, cannot show only the whole hour
  23. hours=hours+mins/60;
  24. Draw the Dial first
  25. cxt.linewidth=10;
  26. cxt.strokestyle="Blue";
  27. Cxt.beginpath ();
  28. Cxt.arc (+, 0, ();
  29. Cxt.closepath ();
  30. Cxt.stroke ();
  31. Draw the moment
  32. for (Var i=0;i<12;i++) {
  33. Cxt.save ();
  34. Cxt.beginpath ();
  35. cxt.linewidth=7;
  36. cxt.strokestyle="BLACK";
  37. Rotate according to the center of the circle, good calculation
  38. Cxt.translate (250, 250);
  39. Cxt.rotate (i*30*math.pi/180);
  40. Cxt.beginpath ();
  41. Cxt.moveto (0,-170);
  42. Cxt.lineto (0,-190);
  43. Cxt.closepath ();
  44. Cxt.stroke ();
  45. Cxt.restore ();
  46. }
  47. for (Var i=0;i<60;i++) {
  48. Cxt.save ();
  49. Cxt.beginpath ();
  50. cxt.linewidth=5;
  51. cxt.strokestyle="BLACK";
  52. Rotate according to the center of the circle, good calculation
  53. Cxt.translate (250, 250);
  54. Cxt.rotate (i*6*math.pi/180);
  55. Cxt.beginpath ();
  56. Cxt.moveto (0,-180);
  57. Cxt.lineto (0,-190);
  58. Cxt.closepath ();
  59. Cxt.stroke ();
  60. Cxt.restore ();
  61. }
  62. Draw the hour hand
  63. Cxt.save ();
  64. cxt.linewidth=7;
  65. cxt.strokestyle="BLACK";
  66. Cxt.translate (250, 250);
  67. Cxt.rotate (hours*30*math.pi/180);
  68. Cxt.beginpath ();
  69. Cxt.moveto (0, 15);
  70. Cxt.lineto (0,-130);
  71. Cxt.closepath ();
  72. Cxt.stroke ();
  73. Cxt.restore ();
  74. Minute
  75. Cxt.save ();
  76. cxt.linewidth=5;
  77. cxt.strokestyle="BLACK";
  78. Cxt.translate (250, 250);
  79. Cxt.rotate (mins*6*math.pi/180);
  80. Cxt.beginpath ();
  81. Cxt.moveto (0, 15);
  82. Cxt.lineto (0,-150);
  83. Cxt.closepath ();
  84. Cxt.stroke ();
  85. Cxt.restore ();
  86. Seconds
  87. Cxt.save ();
  88. cxt.linewidth=3;
  89. cxt.strokestyle="Red";
  90. Cxt.translate (250, 250);
  91. Cxt.rotate (secs*6*math.pi/180);
  92. Cxt.beginpath ();
  93. Cxt.moveto (0, 15);
  94. Cxt.lineto (0,-172);
  95. Cxt.closepath ();
  96. Cxt.stroke ();
  97. To beautify a small dot in the middle of a picture
  98. Cxt.beginpath ();
  99. Cxt.arc (0, 0, 6, 0, N, false);
  100. Cxt.closepath ();
  101. cxt.fillstyle="BLACK";
  102. Cxt.fill ();
  103. Cxt.stroke ();
  104. Draw a small dot in front of the second hand
  105. Cxt.beginpath ();
  106. Cxt.arc (0, -150, 8, 0, N, false);
  107. Cxt.closepath ();
  108. cxt.fillstyle="BLACK";
  109. Cxt.fill ();
  110. Cxt.stroke ();
  111. Cxt.restore ();
  112. }
  113. To avoid drawing after waiting 1 seconds to open, call first
  114. Drawclock ();
  115. SetInterval (drawclock,1000);
  116. </Script>
  117. </body>
  118. </html></span>

Web front-end development learning----9. Use canvas to draw a 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.