答案:使用HTML5 Canvas结合JavaScript绘制动态时钟,通过arc绘制表盘外圈,createRadialGradient实现渐变填充,for循环绘制12个刻度及数字,利用translate和rotate变换简化指针旋转逻辑,通过requestAnimationFrame实现每秒更新动画,配合save/restore保持坐标系状态,最终呈现一个带立体感渐变、平滑转动的模拟时钟。

用 HTML5 的 Canvas 绘制一个动态时钟,不仅能展示绘图能力,还能体现对动画和时间处理的理解。下面是一个完整的实现方案,结合了 Canvas 的高级绘图技巧,如路径绘制、变换、渐变与平滑动画。
在 HTML 中添加一个 canvas 元素,并设置宽高。通过 JavaScript 获取上下文进行绘图。
<canvas id="clock" width="400" height="400"></canvas>
JavaScript 中获取绘图上下文:
const canvas = document.getElementById('clock');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = Math.min(centerX, centerY) - 10;
绘制一个带外圈、数字和刻度的表盘,提升视觉效果。
立即学习“前端免费学习笔记(深入)”;
function drawDial() {
// 清除画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
<p>// 外圈边框
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
ctx.lineWidth = 6;
ctx.strokeStyle = '#333';
ctx.stroke();</p><p>// 内部渐变填充
const gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
gradient.addColorStop(0, '#f0f0f0');
gradient.addColorStop(1, '#ccc');
ctx.fillStyle = gradient;
ctx.fill();</p><p>// 绘制刻度
for (let i = 0; i < 12; i++) {
const angle = (i * Math.PI / 6);
const isMajor = i % 3 === 0; // 每3个是大刻度(对应数字)
const startRadius = isMajor ? radius - 10 : radius - 5;
const endRadius = radius;</p><pre class='brush:php;toolbar:false;'>ctx.beginPath();
ctx.moveTo(
centerX + Math.cos(angle) * startRadius,
centerY + Math.sin(angle) * startRadius
);
ctx.lineTo(
centerX + Math.cos(angle) * endRadius,
centerY + Math.sin(angle) * endRadius
);
ctx.lineWidth = isMajor ? 3 : 2;
ctx.strokeStyle = '#000';
ctx.stroke();
// 添加数字
if (isMajor) {
const num = i === 0 ? 12 : i;
ctx.font = 'bold 18px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(num,
centerX + Math.cos(angle) * (radius - 30),
centerY + Math.sin(angle) * (radius - 30)
);
}} }
使用 translate 和 rotate 简化指针旋转逻辑。
function drawHand(length, width, rotation) {
ctx.save();
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = 'round';
ctx.translate(centerX, centerY);
ctx.rotate(rotation);
ctx.moveTo(0, 0);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.restore();
}
<p>function drawClock() {
const now = new Date();
const hours = now.getHours() % 12;
const minutes = now.getMinutes();
const seconds = now.getSeconds();</p><p>// 重绘表盘
drawDial();</p><p>// 计算角度(注意:Math.PI <em> 0.5 是起始偏移,因为 0 度指向右,而时钟从上开始)
const secAngle = seconds </em> (Math.PI / 30) - Math.PI / 2;
const minAngle = (minutes + seconds / 60) <em> (Math.PI / 30) - Math.PI / 2;
const hourAngle = (hours + minutes / 60) </em> (Math.PI / 6) - Math.PI / 2;</p><p>// 绘制指针
drawHand(radius <em> 0.5, 6, hourAngle); // 时针
drawHand(radius </em> 0.7, 4, minAngle); // 分针
drawHand(radius * 0.9, 2, secAngle); // 秒针</p><p>// 小圆心盖住中心
ctx.beginPath();
ctx.arc(centerX, centerY, 6, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
}</p>使用 requestAnimationFrame 替代 setInterval,实现更流畅的动画。
function animate() {
drawClock();
requestAnimationFrame(() => {
setTimeout(animate, 1000); // 每秒更新一次
});
}
animate(); // 启动时钟
这样可以避免频繁重绘造成性能浪费,同时保持与屏幕刷新率同步。
基本上就这些。通过组合路径、变换和定时机制,你可以用 Canvas 实现出既美观又精准的动态时钟。
以上就是html5使用canvas绘制动态时钟 html5使用图形画布的高级技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号