当前位置:首页 > JavaScript

js实现时针

2026-03-16 00:31:00JavaScript

使用Canvas绘制时针

在HTML中创建Canvas元素,通过JavaScript获取上下文并绘制时针。

<canvas id="clock" width="200" height="200"></canvas>
<script>
  const canvas = document.getElementById('clock');
  const ctx = canvas.getContext('2d');
  const centerX = canvas.width / 2;
  const centerY = canvas.height / 2;

  function drawHourHand(hours, minutes) {
    const angle = (hours % 12 + minutes / 60) * Math.PI / 6 - Math.PI / 2;
    const length = canvas.width / 3;

    ctx.beginPath();
    ctx.moveTo(centerX, centerY);
    ctx.lineTo(
      centerX + Math.cos(angle) * length,
      centerY + Math.sin(angle) * length
    );
    ctx.lineWidth = 6;
    ctx.strokeStyle = '#000';
    ctx.stroke();
  }

  function updateClock() {
    const now = new Date();
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawHourHand(now.getHours(), now.getMinutes());
    requestAnimationFrame(updateClock);
  }

  updateClock();
</script>

使用SVG实现动态时针

通过SVG和JavaScript创建可动态更新的时针。

js实现时针

<svg id="clock" width="200" height="200" viewBox="0 0 200 200">
  <circle cx="100" cy="100" r="95" fill="none" stroke="#000" stroke-width="2"/>
  <line id="hourHand" x1="100" y1="100" x2="100" y2="50" stroke="#000" stroke-width="6" stroke-linecap="round"/>
</svg>
<script>
  const hourHand = document.getElementById('hourHand');
  const centerX = 100;
  const centerY = 100;

  function updateHourHand() {
    const now = new Date();
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const angle = (hours % 12 + minutes / 60) * 30 - 90;
    const length = 50;
    const radian = angle * Math.PI / 180;

    hourHand.setAttribute('x2', centerX + Math.cos(radian) * length);
    hourHand.setAttribute('y2', centerY + Math.sin(radian) * length);
    requestAnimationFrame(updateHourHand);
  }

  updateHourHand();
</script>

纯CSS动画时针

利用CSS transform和动画实现时针效果,通过JavaScript更新时间。

js实现时针

<div class="clock">
  <div class="hour-hand"></div>
</div>
<style>
  .clock {
    width: 200px;
    height: 200px;
    border: 2px solid #000;
    border-radius: 50%;
    position: relative;
  }

  .hour-hand {
    width: 6px;
    height: 60px;
    background: #000;
    position: absolute;
    left: 50%;
    top: 50%;
    transform-origin: bottom center;
    margin-left: -3px;
    border-radius: 3px;
  }
</style>
<script>
  const hourHand = document.querySelector('.hour-hand');

  function updateClock() {
    const now = new Date();
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const degrees = (hours % 12 + minutes / 60) * 30;
    hourHand.style.transform = `rotate(${degrees}deg)`;
    requestAnimationFrame(updateClock);
  }

  updateClock();
</script>

使用requestAnimationFrame优化性能

所有实现中都使用了requestAnimationFrame来实现平滑动画,这是浏览器推荐的动画实现方式,相比setInterval能提供更好的性能。

function animate() {
  // 更新逻辑
  requestAnimationFrame(animate);
}
animate();

考虑时区问题

如果需要处理不同时区,可以调整获取的小时数。

const now = new Date();
const hours = now.getHours() + timezoneOffset;  // 时区偏移量

这些方法提供了从简单到复杂的时针实现方案,可以根据项目需求选择适合的方式。Canvas方案适合需要复杂绘制的场景,SVG方案便于维护和缩放,CSS方案性能较好且实现简单。

标签: 时针js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前年…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…