lock.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html>
  2. <html lang="zh_cn">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <canvas id="clockCanvas" width="400" height="400"></canvas>
  10. </body>
  11. <script>
  12. // 获取 Canvas 元素和 2D 上下文
  13. const canvas = document.getElementById("clockCanvas");
  14. const ctx = canvas.getContext("2d");
  15. // 获取 Canvas 的中心点坐标
  16. const centerX = canvas.width / 2;
  17. const centerY = canvas.height / 2;
  18. // 绘制时钟的外圆
  19. function drawClockFace() {
  20. ctx.beginPath();
  21. ctx.arc(centerX, centerY, 190, 0, Math.PI * 2);
  22. ctx.lineWidth = 5;
  23. ctx.strokeStyle = "#000";
  24. ctx.stroke();
  25. ctx.closePath();
  26. }
  27. // 绘制时钟的刻度
  28. function drawClockTicks() {
  29. for (let i = 0; i < 12; i++) {
  30. const angle = (Math.PI / 6) * i;
  31. const x1 = centerX + Math.sin(angle) * 160;
  32. const y1 = centerY - Math.cos(angle) * 160;
  33. const x2 = centerX + Math.sin(angle) * 180;
  34. const y2 = centerY - Math.cos(angle) * 180;
  35. ctx.beginPath();
  36. ctx.moveTo(x1, y1);
  37. ctx.lineTo(x2, y2);
  38. ctx.lineWidth = 3;
  39. ctx.strokeStyle = "#000";
  40. ctx.stroke();
  41. ctx.closePath();
  42. }
  43. }
  44. // 绘制时钟的指针
  45. function drawClockHands() {
  46. const now = new Date();
  47. const hours = now.getHours();
  48. const minutes = now.getMinutes();
  49. const seconds = now.getSeconds();
  50. // 绘制时针
  51. const hourAngle = (Math.PI / 6) * (hours % 12) + (Math.PI / 360) * minutes;
  52. const hourHandLength = 100;
  53. const hourX = centerX + Math.sin(hourAngle) * hourHandLength;
  54. const hourY = centerY - Math.cos(hourAngle) * hourHandLength;
  55. ctx.beginPath();
  56. ctx.moveTo(centerX, centerY);
  57. ctx.lineTo(hourX, hourY);
  58. ctx.lineWidth = 8;
  59. ctx.strokeStyle = "#000";
  60. ctx.stroke();
  61. ctx.closePath();
  62. // 绘制分针
  63. const minuteAngle = (Math.PI / 30) * minutes + (Math.PI / 1800) * seconds;
  64. const minuteHandLength = 140;
  65. const minuteX = centerX + Math.sin(minuteAngle) * minuteHandLength;
  66. const minuteY = centerY - Math.cos(minuteAngle) * minuteHandLength;
  67. ctx.beginPath();
  68. ctx.moveTo(centerX, centerY);
  69. ctx.lineTo(minuteX, minuteY);
  70. ctx.lineWidth = 5;
  71. ctx.strokeStyle = "#000";
  72. ctx.stroke();
  73. ctx.closePath();
  74. // 绘制秒针
  75. const secondAngle = (Math.PI / 30) * seconds;
  76. const secondHandLength = 160;
  77. const secondX = centerX + Math.sin(secondAngle) * secondHandLength;
  78. const secondY = centerY - Math.cos(secondAngle) * secondHandLength;
  79. ctx.beginPath();
  80. ctx.moveTo(centerX, centerY);
  81. ctx.lineTo(secondX, secondY);
  82. ctx.lineWidth = 2;
  83. ctx.strokeStyle = "#f00";
  84. ctx.stroke();
  85. ctx.closePath();
  86. }
  87. // 绘制整个时钟
  88. function drawClock() {
  89. ctx.clearRect(0, 0, canvas.width, canvas.height);
  90. drawClockFace();
  91. drawClockTicks();
  92. drawClockHands();
  93. requestAnimationFrame(drawClock);
  94. }
  95. // 开始绘制时钟
  96. drawClock();
  97. </script>
  98. </html>