Discuz教程网

html5时钟实现代码

[复制链接]
authicon dly 发表于 2011-1-16 10:55:57 | 显示全部楼层 |阅读模式


  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <style type="text/css">
  6. canvas{position:absolute;top:0px;left:0px;}
  7. </style>
  8. <title>时钟</title>
  9. </head>
  10. <body>
  11. <canvas id="canvas" width="200" height="200"></canvas>
  12. <canvas id="p_canvas" width="200" height="200"></canvas>
  13. <script type="text/javascript">
  14. //获取绘图对象
  15. var canvas = document.getElementById(\'canvas\');
  16. var context = canvas.getContext(\'2d\');
  17. var p_canvas = document.getElementById(\'p_canvas\');
  18. var p_context = p_canvas.getContext(\'2d\');
  19. var height=200,width=200;
  20. //画大圆
  21. context.beginPath();
  22. context.strokeStyle="#009999";
  23. context.arc(width/2,height/2,width/2-1,0,Math.PI*2,true);
  24. context.stroke();
  25. context.closePath();
  26. //画中间点
  27. context.beginPath();
  28. context.fillStyle="#000";
  29. context.arc(width/2,height/2,3,0,Math.PI*2,true);
  30. context.fill();
  31. context.closePath();
  32. //画小刻度
  33. var angle = 0,radius = width/2 - 4;
  34. for(var i=0;i<60;i++){
  35. context.beginPath();
  36. context.strokeStyle="#000";
  37. //确认刻度的起始点
  38. var x = width/2 + radius*Math.cos(angle),y = height/2 + radius*Math.sin(angle);
  39. context.moveTo(x,y);
  40. //这里是用来将刻度的另一点指向中心点,并给予正确的角度
  41. //PI是180度,正确的角度就是 angle+180度,正好相反方向
  42. var temp_angle = Math.PI +angle;
  43. context.lineTo(x +3*Math.cos(temp_angle),y+3*Math.sin(temp_angle));
  44. context.stroke();
  45. context.closePath();
  46. angle+=6/180*Math.PI;
  47. }
  48. //大刻度
  49. angle = 0,radius = width/2 - 4;
  50. context.textBaseline = \'middle\';
  51. context.textAlign = \'center\';
  52. context.lineWidth = 2;
  53. for(var i=0;i<12;i++){
  54. var num = i+3>12? i+3-12:i+3 ;
  55. context.beginPath();
  56. context.strokeStyle="#FFD700";
  57. var x = width/2 + radius*Math.cos(angle),y = height/2 + radius*Math.sin(angle);
  58. context.moveTo(x,y);
  59. var temp_angle = Math.PI +angle;
  60. context.lineTo(x +8*Math.cos(temp_angle),y+8*Math.sin(temp_angle));
  61. context.stroke();
  62. context.closePath();
  63. //大刻度 文字
  64. context.fillText(num,x+16*Math.cos(temp_angle),y+16*Math.sin(temp_angle));
  65. angle+=30/180*Math.PI;
  66. }
  67. function Pointer(){
  68. var p_type = [[\'#000\',70,1],[\'#ccc\',60,2],[\'red\',50,3]];
  69. function drwePointer(type,angle){
  70. type = p_type[type];
  71. angle = angle*Math.PI*2 - 90/180*Math.PI;
  72. var length= type[1];
  73. p_context.beginPath();
  74. p_context.lineWidth = type[2];
  75. p_context.strokeStyle = type[0];
  76. p_context.moveTo(width/2,height/2);
  77. p_context.lineTo(width/2 + length*Math.cos(angle),height/2 + length*Math.sin(angle));
  78. p_context.stroke();
  79. p_context.closePath();
  80. }
  81. setInterval(function (){
  82. p_context.clearRect(0,0,height,width);
  83. var time = new Date();
  84. var h = time.getHours();
  85. var m = time.getMinutes();
  86. var s = time.getSeconds();
  87. h = h>12?h-12:h;
  88. h = h+m/60;
  89. h=h/12;
  90. m=m/60;
  91. s=s/60;
  92. drwePointer(0,s);
  93. drwePointer(1,m);
  94. drwePointer(2,h);
  95. },500);
  96. }
  97. var p = new Pointer();
  98. </script>
  99. </body>
  100. </html>
复制代码






上一篇:Html技巧 语义化你的代码
下一篇:html5 学习简单的拾色器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 18:11

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表