Canvas 动画时钟

川长思鸟来 2023-10-06 19:34 148阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Canvas时钟</title>
  6. <style>
  7. #clock {
  8. margin-left:350px;
  9. }
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var clock = document.getElementById("clock");
  14. var context = clock.getContext("2d");
  15. function drawClock(){
  16. context.clearRect(0,0,500,500);//清空画布 每画一次 都要清除前面的在重新画
  17. var now = new Date();
  18. var sec = now.getSeconds();
  19. var min = now.getMinutes();
  20. var hour = now.getHours();
  21. //小时必须获取浮点类型(小时 + 分数转换成的小时)
  22. hour = hour + min / 60;
  23. //将24小时进制转换为12小时 不然就是 这样的形式不好了 18:30:10
  24. hour = hour > 12 ? hour - 12 : hour;
  25. //表盘
  26. context.lineWidth = 10;
  27. context.strokeStyle = "greenblue"
  28. context.beginPath();
  29. context.arc(250,250,200,0,360,false);//true 顺时针
  30. context.closePath();
  31. context.stroke();
  32. //时刻度
  33. for(var i = 0; i < 12; i++){
  34. context.save();//保存当前状态
  35. context.lineWidth = 7;//时针粗细
  36. context.strokeStyle = "#000";//时针颜色
  37. context.translate(250,250);//设置0,0点
  38. context.rotate(i * 30 * Math.PI / 180);//获得每次旋转之后的角度 设置旋转角度 角度 * Math.PI / 180 = 弧度
  39. context.beginPath();
  40. context.moveTo(0,-170);
  41. context.lineTo(0,-190);
  42. context.stroke(); //笔触
  43. context.closePath();
  44. context.restore();//恢复当前状态
  45. }
  46. //分刻度
  47. for(var i = 0;i < 60; i++){
  48. context.save();
  49. context.lineWidth = 5;//设置分刻度粗细
  50. context.strokeStyle = "#000";
  51. context.translate(250,250);//重置0,0点
  52. context.rotate(i * 6 * Math.PI / 180);//设置旋转角度
  53. context.beginPath();
  54. context.moveTo(0,-180);
  55. context.lineTo(0,-190);
  56. context.stroke();
  57. context.closePath();
  58. context.restore();
  59. }
  60. //时针
  61. context.save(); //保存当前绘制的状态
  62. //设置时针风格
  63. context.lineWidth = 7;
  64. context.strokeStyle = "#000";
  65. context.translate(250,250);//设置异次元空间的0,0 点
  66. context.rotate( hour * 30 * Math.PI / 180);//设置旋转角度
  67. context.beginPath();
  68. context.moveTo(0,-140);
  69. context.lineTo(0,10);
  70. context.closePath();
  71. context.stroke();
  72. context.restore();
  73. //分针
  74. context.save();
  75. context.lineWidth = 5;//设置分针风格
  76. context.strokeStyle = "#000";
  77. context.translate(250,250);
  78. context.rotate(min * 6 * Math.PI / 180);//设置旋转角度
  79. context.beginPath();
  80. context.moveTo(0,-160);
  81. context.lineTo(0,15);
  82. context.closePath();
  83. context.stroke();
  84. context.restore();
  85. //秒针
  86. context.save();
  87. context.lineWidth = 3;//设置分针风格
  88. context.strokeStyle = "#f00";
  89. context.translate(250,250);
  90. context.rotate(sec * 6 * Math.PI / 180);//设置旋转角度 每秒走的角度是6度
  91. context.beginPath();
  92. context.moveTo(0,-170);
  93. context.lineTo(0,20);
  94. context.closePath();
  95. context.stroke();
  96. //画时针 分针 秒针的交叉点
  97. context.beginPath();
  98. context.arc(0,0,5,0,360,false);//FALSE 逆时针
  99. context.closePath();
  100. context.fillStyle = "gray";//设置填充样式
  101. context.fill();
  102. context.stroke();
  103. //设置秒针的小圆点
  104. context.beginPath();
  105. context.arc(0,-160,2,0,360,false);//FALSE 逆时针
  106. context.closePath();
  107. context.fillStyle = "blue";//设置填充样式
  108. context.fill();
  109. context.stroke();
  110. context.restore();
  111. }
  112. drawClock(); //这一句必须加上 因为setInterval 第一秒不执行
  113. setInterval(drawClock,1000);
  114. }
  115. </script>
  116. </head>
  117. <body>
  118. <canvas id="clock" width="500" height="500">
  119. 您的浏览器不支持此效果展示,请升级最新版本
  120. </canvas>
  121. </body>
  122. </html>

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,148人围观)

还没有评论,来说两句吧...

相关阅读