clock.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <div style="text-align:center;padding-top:10px;">
  2. <canvas style="border:1px solid #000;" height="200" width="200" id="canvas">您的浏览器不支持Canvas。</canvas>
  3. </div>
  4. <script>
  5. var canvas = document.getElementById('canvas');
  6. var ctx = canvas.getContext('2d');
  7. if(ctx){
  8. var timerId;
  9. var frameRate = 60;
  10. function canvObject(){
  11. this.x = 0;
  12. this.y = 0;
  13. this.rotation = 0;
  14. this.borderWidth = 2;
  15. this.borderColor = '#000000';
  16. this.fill = false;
  17. this.fillColor = '#ff0000';
  18. this.update = function(){
  19. if(!this.ctx)throw new Error('你没有指定ctx对象。');
  20. var ctx = this.ctx
  21. ctx.save();
  22. ctx.lineWidth = this.borderWidth;
  23. ctx.strokeStyle = this.borderColor;
  24. ctx.fillStyle = this.fillColor;
  25. ctx.translate(this.x, this.y);
  26. if(this.rotation)ctx.rotate(this.rotation * Math.PI/180);
  27. if(this.draw)this.draw(ctx);
  28. if(this.fill)ctx.fill();
  29. ctx.stroke();
  30. ctx.restore();
  31. }
  32. };
  33. function Line(){};
  34. Line.prototype = new canvObject();
  35. Line.prototype.fill = false;
  36. Line.prototype.start = [0,0];
  37. Line.prototype.end = [5,5];
  38. Line.prototype.draw = function(ctx){
  39. ctx.beginPath();
  40. ctx.moveTo.apply(ctx,this.start);
  41. ctx.lineTo.apply(ctx,this.end);
  42. ctx.closePath();
  43. };
  44. function Circle(){};
  45. Circle.prototype = new canvObject();
  46. Circle.prototype.draw = function(ctx){
  47. ctx.beginPath();
  48. ctx.arc(0, 0, this.radius, 0, 2 * Math.PI, true);
  49. ctx.closePath();
  50. };
  51. var circle = new Circle();
  52. circle.ctx = ctx;
  53. circle.x = 100;
  54. circle.y = 100;
  55. circle.radius = 90;
  56. circle.fill = true;
  57. circle.borderWidth = 6;
  58. circle.fillColor = '#ffffff';
  59. var hour = new Line();
  60. hour.ctx = ctx;
  61. hour.x = 100;
  62. hour.y = 100;
  63. hour.borderColor = "#000000";
  64. hour.borderWidth = 10;
  65. hour.rotation = 0;
  66. hour.start = [0,20];
  67. hour.end = [0,-50];
  68. var minute = new Line();
  69. minute.ctx = ctx;
  70. minute.x = 100;
  71. minute.y = 100;
  72. minute.borderColor = "#333333";
  73. minute.borderWidth = 7;
  74. minute.rotation = 0;
  75. minute.start = [0,20];
  76. minute.end = [0,-70];
  77. var seconds = new Line();
  78. seconds.ctx = ctx;
  79. seconds.x = 100;
  80. seconds.y = 100;
  81. seconds.borderColor = "#ff0000";
  82. seconds.borderWidth = 4;
  83. seconds.rotation = 0;
  84. seconds.start = [0,20];
  85. seconds.end = [0,-80];
  86. var center = new Circle();
  87. center.ctx = ctx;
  88. center.x = 100;
  89. center.y = 100;
  90. center.radius = 5;
  91. center.fill = true;
  92. center.borderColor = 'orange';
  93. for(var i=0,ls=[],cache;i<12;i++){
  94. cache = ls[i] = new Line();
  95. cache.ctx = ctx;
  96. cache.x = 100;
  97. cache.y = 100;
  98. cache.borderColor = "orange";
  99. cache.borderWidth = 2;
  100. cache.rotation = i * 30;
  101. cache.start = [0,-70];
  102. cache.end = [0,-80];
  103. }
  104. timerId = setInterval(function(){
  105. // 清除画布
  106. ctx.clearRect(0,0,200,200);
  107. // 填充背景色
  108. ctx.fillStyle = 'orange';
  109. ctx.fillRect(0,0,200,200);
  110. // 表盘
  111. circle.update();
  112. // 刻度
  113. for(var i=0;cache=ls[i++];)cache.update();
  114. // 时针
  115. hour.rotation = (new Date()).getHours() * 30;
  116. hour.update();
  117. // 分针
  118. minute.rotation = (new Date()).getMinutes() * 6;
  119. minute.update();
  120. // 秒针
  121. seconds.rotation = (new Date()).getSeconds() * 6;
  122. seconds.update();
  123. // 中心圆
  124. center.update();
  125. },(1000/frameRate)|0);
  126. }else{
  127. alert('您的浏览器不支持Canvas无法预览.\n跟我一起说:"Fuck Internet Exploer!"');
  128. }
  129. </script>