当前位置:Gxlcms > html代码 > HTML5组件Canvas实现电子钟的图文代码详情介绍

HTML5组件Canvas实现电子钟的图文代码详情介绍

时间:2021-07-01 10:21:17 帮助过:44人阅读

基本思路:

首先绘制一个矩形背景,设置颜色为灰色。在背景上绘制一个简单的矩形外边框,然后再绘

制一个内边框,接着加载选定的图像做为电子钟内部的背景图片。然后开始绘制时钟刻度,

绘制分钟刻度,最后获取当前系统时间,绘制时分秒三个手柄。

技术要点:

使用HTML5的Canvas 2D绘制对象,主要使用context.save()与context.restore()方法来保存

绘制状态和重置绘制状态,使用Transform和fillRect()方法来绘制时钟和分钟刻度。使用

drawImage()方法来绘制背景图片,使用setTimeout()方法来刷新时间显示。

代码详解:

获取HTML5 Canvas绘制对象的代码如下:

  1. var canvas = document.getElementById("canvas1");
  2. ctx = canvas.getContext("2d");
  3. ctx.clearRect(0, 0, 500, 500);

绘制时钟刻度的代码如下:

  1. var sin = Math.sin(Math.PI/6);
  2. var cos = Math.cos(Math.PI/6);
  3. ctx.translate(245, 245);
  4. for (var i=0; i <= 12; i++) {
  5. // top
  6. ctx.fillRect(160,-7.5,30,10);
  7. ctx.strokeRect(160,-7.5,30,10);
  8. ctx.transform(cos, sin, -sin, cos, 0, 0);
  9. }

绘制分钟分钟刻度的代码如下:

  1. var sin = Math.sin(Math.PI/30);
  2. var cos = Math.cos(Math.PI/30);
  3. for (var i=0; i <= 60; i++) {
  4. ctx.fillRect(170,-5,10,2);
  5. ctx.transform(cos, sin, -sin, cos, 0, 0);
  6. }

保存制状态代码如下:

  1. ctx.translate(245, 245);
  2. ctx.save();

恢复绘制状态代码如下:

  1. ctx.restore();

运行效果如下:


程序完全源代码如下:

  1. <html>
  2. <head>
  3. <script>
  4. window.onload = function() {
  5. clockHand();
  6. };
  7. function clockHand() {
  8. var canvas = document.getElementById("canvas1");
  9. ctx = canvas.getContext("2d");
  10. ctx.clearRect(0, 0, 500, 500);
  11. // create background rectangle
  12. // ctx.lineWidth = 10;
  13. ctx.fillStyle = "gray";
  14. ctx.fillRect(0,0,500,500);
  15. // draw frame
  16. ctx.lineWidth = 10;
  17. ctx.strokeStyle = "green";
  18. ctx.strokeRect(0,0,500,500);
  19. // draw author infomation
  20. ctx.fillStyle = "blue";
  21. ctx.font = "20px Times New Roman";
  22. ctx.fillText("-created by gloomyfish", 150, 30);
  23. // draw inner rectangle
  24. ctx.lineWidth = 10;
  25. ctx.strokeStyle = "black";
  26. ctx.strokeRect(45,45,400,400);
  27. // create background image
  28. var img=new Image();
  29. img.src="background.png";
  30. img.onload = function() {
  31. ctx.drawImage(img,45,45,400,400);
  32. ctx.save();
  33. // draw marker unit
  34. ctx.lineWidth = 2;
  35. ctx.fillStyle = "purple";
  36. ctx.strokeStyle = "black";
  37. var sin = Math.sin(Math.PI/6);
  38. var cos = Math.cos(Math.PI/6);
  39. ctx.translate(245, 245);
  40. for (var i=0; i <= 12; i++) {
  41. // top
  42. ctx.fillRect(160,-7.5,30,10);
  43. ctx.strokeRect(160,-7.5,30,10);
  44. ctx.transform(cos, sin, -sin, cos, 0, 0);
  45. }
  46. // transform back center point
  47. // ctx.translate(245, 245);
  48. var sin = Math.sin(Math.PI/30);
  49. var cos = Math.cos(Math.PI/30);
  50. for (var i=0; i <= 60; i++) {
  51. ctx.fillRect(170,-5,10,2);
  52. ctx.transform(cos, sin, -sin, cos, 0, 0);
  53. }
  54. ctx.restore();
  55. // top
  56. ctx.fillText("12", 233,100);
  57. // bottom
  58. ctx.fillText("6", 240,400);
  59. // left
  60. ctx.fillText("9", 90,252);
  61. // right
  62. ctx.fillText("3", 395,250);
  63. // get time
  64. ctx.save();
  65. ctx.translate(245, 245);
  66. ctx.save();
  67. // dynamic show time
  68. var now=new Date();
  69. var hrs=now.getHours();
  70. var min=now.getMinutes();
  71. var sec=now.getSeconds();
  72. //Draw hour hand
  73. ctx.rotate(Math.PI/6*(hrs+(min/60)+(sec/3600)));
  74. ctx.beginPath();
  75. ctx.moveTo(0,10);
  76. ctx.lineTo(0,-60);
  77. ctx.stroke();
  78. ctx.restore();
  79. ctx.save();
  80. //Draw minute hand
  81. ctx.rotate(Math.PI/30*(min+(sec/60)));
  82. ctx.beginPath();
  83. ctx.moveTo(0,20);
  84. ctx.lineTo(0,-110);
  85. ctx.stroke();
  86. ctx.restore();
  87. ctx.save();
  88. //Draw second hand
  89. ctx.rotate(Math.PI/30*sec);
  90. ctx.strokeStyle="#E33";
  91. ctx.lineWidth = 2;
  92. ctx.beginPath();
  93. ctx.moveTo(0,20);
  94. ctx.lineTo(0,-110);
  95. ctx.stroke();
  96. ctx.restore();
  97. // finally store to originall point
  98. ctx.restore();
  99. setTimeout(clockHand,1000);
  100. };
  101. }
  102. </script>
  103. </head>
  104. <body bgcolor="#E6E6FA">
  105. <canvas id="canvas1" width="500" height="500">electronic clock</canvas>
  106. </body>
  107. </html>

不足之处:

每次都刷新加载image对象不怎么好,我是在google浏览器中测试的,建议在

google浏览器中运行上面代码。

以上就是HTML5 组件Canvas实现电子钟的图文代码详情介绍的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行