Home >Web Front-end >H5 Tutorial >Code sharing for writing a clock on a webpage using pure HTML5_html5 tutorial skills

Code sharing for writing a clock on a webpage using pure HTML5_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:46:231916browse

What you need to know:

The canvas tag is just a graphics container, you have to use a script to draw the graphics. Default size: width 300px, height 150px;

The getContext() method returns an object that provides methods and properties for drawing on the canvas. ——Get the context object.
getContext("2d") object properties and methods, which can be used to draw text, lines, rectangles, circles, etc. on the canvas.

fillRect(l,t,w,h): The default color is black strokeRect(l,t,w,h): A square with a border. Default one pixel black border

The setInterval() method can call a function or calculate an expression according to the specified period (in milliseconds).

beginPath(): Define the beginning of drawing the path, which sets the current point to (0,0). When a canvas environment is first created, the beginPath()
method is called explicitly.
closePath(): End drawing the path (connect the starting point and end point)


Draw a circle:
arc (x, y, radius, starting arc, ending arc, rotation direction)
x, y: starting position
The relationship between arc and angle: Radians = angle *Math.PI/180
Rotation direction: clockwise (default: false, counterclockwise: true)

Code:

XML/HTML CodeCopy content to clipboard
  1. >  
  2. <html lang="en-US">  
  3. <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>title>  
  6.         <script>  
  7.                 window.onload = function(){   
  8.                         var oC = document.getElementById('ch1');   
  9.                         var oGC = oC.getContext('2d');   
  10.   
  11.                         function drawClock(){   
  12.                                 var x = 200;   //指定坐标   
  13.                                 var y = 200;   
  14.                                 var r = 150;  //指定钟表半径   
  15.   
  16.                                 oGC.clearRect(0,0,oC.width,oC.height);//清空画布   
  17.   
  18.                                 var oDate = new Date();      //创建日期对象   
  19. var Ohours = Odate .Gethours (); // var 🎜>
  20. var 🎜>                                                                                                                                                                                                                                                                                       🎜>                           var var
  21. osenvalue = (-90 osen*6)*math.pi/180; oGC.beginPath();//Start
  22. for(var i=
  23. 0;i<60
  24. ;i ){           //i is 60, which represents the 60 small scales of the clock oGC.moveTo(x,y);
  25.                                              oGC.arc(x,y,r,6*i*Math.PI/180,6*(i 1)*Math.PI/180,false); //Loop from 6 degrees to 12 Degree
  26.                                                                   
  27. oGC.closePath();
  28. oGC.stroke();
  29.                                                                                                                                                                                                                               oGC.beginPath(); oGC.moveTo(x,y);
  30. oGC.arc(x,y,r*19/20,0,360*(i 1)*Math.PI/180,false);
  31. oGC.closePath();//End
  32. oGC.fill();
  33.                                                                                                                                                                                                                                            🎜>
  34.                                                                                                                                                                                                                           ’ s ’ s         ’s ‐ to GC.be''s-'s t-----oGC.beginPath();                    
  35. for(i=0;i
  36. <
  37. 12
  38. ;i ){                   //i is 12, which represents the 12 grids of the clock scale
  39. oGC.moveTo(x,y);
  40.                                                                                                                                                                     ‐               oGC. =Angle*Math.PI/180                                                                   
  41. oGC.closePath();
  42. oGC.stroke();
  43.                                                                                                                                                                                                                                                                                             oGC.beginPath(); oGC.moveTo(x,y);
  44.                                   oGC.arc(x,y,r*18/20,360*(i 1)*Math.PI/180,false);                     
  45. oGC.closePath();
  46. oGC.fill();//The dial is completed
  47. <<>
  48. OGC.LineWidth
  49. =
  50. 5
  51. ;
  52. oGC.beginPath();//Start drawing the hour hand
  53. oGC.moveTo(x,y);
  54. oGC.arc(x,y,r*10/20,oHoursValue,oHoursValue,false);//Set the hour hand size and radian
  55. oGC.closePath();
  56. oGC.stroke();
  57. <<>
  58. OGC.LineWidth =
  59. 3 ;
  60. oGC.beginPath();//Start drawing the minute hand
  61. oGC.moveTo(x,y);
  62. oGC.arc(x,y,r*14/20,oMinValue,oMinValue,false);//Set the minute hand size and radian
  63. oGC.closePath();
  64. oGC.stroke();
  65. <<>
  66. OGC.LineWidth = 1 ; // Set the width of the second hand oGC.beginPath();//Start drawing the second hand
  67. oGC.moveTo(x,y);
  68. oGC.arc(x,y,r*19/20,oSenValue,oSenValue,false);//Set the second hand size and arc
  69. oGC.closePath();
  70. oGC.stroke();
  71.                                                                     
  72. setInterval(drawClock,1000);//Set the timer and let the clock run
  73. drawClock();
  74. };
  75. script>
  76. head>
  77. <body>
  78.  <canvas id = "ch1" width = "400px" height = "400px">canvas> 
  79. body>
  80. html>

Click the result below to view the demo:
http://jsfiddle.net/eh02450b/2/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn