html,,,,, canvas {, display: block;, margin: auto;, backgroundcolor: #f0f0f0;, },,,,,, var canvas = document.getElementById('clockCanvas');, var ctx = canvas.getContext('2d');, var radius = canvas.height / 2;, ctx.translate(radius, radius);, radius = radius * 0.9;, setInterval(drawClock, 1000);,, function drawClock() {, drawFace(ctx, radius);, drawNumbers(ctx, radius);, drawTime(ctx, radius);, },, function drawFace(ctx, radius) {, ctx.beginPath();, ctx.arc(0, 0, radius, 0, 2 * Math.PI);, ctx.fillStyle = 'white';, ctx.fill();, ctx.strokeStyle = 'black';, ctx.lineWidth = radius * 0.1;, ctx.stroke();, ctx.beginPath();, ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);, ctx.fillStyle = 'black';, ctx.fill();, },, function drawNumbers(ctx, radius) {, var ang;, var num;, ctx.font = radius * 0.15 + 'px arial';, ctx.textBaseline = 'middle';, ctx.textAlign = 'center';, for (num = 1; num< 13;="" num++)="" {,="" ang="num" *="" math.pi="" 6;,="" ctx.rotate(ang);,="" ctx.translate(0,="" radius="" *="" 0.85);,="" ctx.rotate(ang);,="" ctx.filltext(num.tostring(),="" 0,="" 0);,="" ctx.rotate(ang);,="" ctx.translate(0,="" radius="" *="" 0.85);,="" ctx.rotate(ang);,="" },="" },,="" function="" drawtime(ctx,="" radius)="" {,="" var="" now="new" date();,="" var="" hour="now.getHours();," var="" minute="now.getMinutes();," var="" second="now.getSeconds();," hour="hour" %="" 12;,="" hour="(hour" *="" math.pi="" 6)="" +="" (minute="" *="" math.pi="" (6="" *="" 60))="" +="" (second="" *="" math.pi="" (360));,="" drawhand(ctx,="" hour,="" radius="" *="" 0.5,="" radius="" *="" 0.07);,="" minute="(minute" *="" math.pi="" 30)="" +="" (second="" *="" math.pi="" (30="" *="" 60));,="" drawhand(ctx,="" minute,="" radius="" *="" 0.8,="" radius="" *="" 0.07);,="" second="(second" *="" math.pi="" 30);,="" drawhand(ctx,="" second,="" radius="" *="" 0.9,="" radius="" *="" 0.02);,="" },,="" function="" drawhand(ctx,="" pos,="" length,="" width)="" {,="" ctx.beginpath();,="" ctx.linewidth="width;," ctx.linecap='round' ;,="" ctx.moveto(0,="" 0);,="" ctx.rotate(pos);,="" ctx.lineto(0,="" length);,="" ctx.stroke();,="" ctx.rotate(pos);,="">
``,,这段代码使用HTML5 Canvas绘制了一个模拟时钟。它包括绘制表盘、数字和指针的功能。时钟的指针会根据系统时间动态更新。使用HTML5 Canvas画时钟代码实例分享
在现代网页设计中,HTML5的Canvas元素为开发者提供了强大的绘图工具,本文将介绍如何使用HTML5 Canvas来绘制一个动态的模拟时钟,通过这个实例,你不仅可以学习到Canvas的基本用法,还能了解到如何实现动画效果以及时间的处理。
准备工作
确保你的HTML文档已经包含了一个<canvas>
元素:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8"> <title>Canvas Clock</title> <style> canvas { display: block; margin: auto; backgroundcolor: #f0f0f0; border: 1px solid #000; } </style> </head> <body> <canvas id="clockCanvas" width="400" height="400"></canvas> <script src="clock.js"></script> </body> </html>
我们将在clock.js
文件中编写JavaScript代码来实现时钟功能。
JavaScript代码详解
1、获取Canvas和Context
```javascript
const canvas = document.getElementById('clockCanvas');
const ctx = canvas.getContext('2d');
```
2、设置画布大小
```javascript
canvas.width = 400;
canvas.height = 400;
```
3、绘制表盘
```javascript
function drawFace(context, radius) {
context.beginPath();
context.arc(context.canvas.width / 2, context.canvas.height / 2, radius, 0, 2 * Math.PI);
context.fillStyle = 'white';
context.fill();
context.strokeStyle = 'black';
context.lineWidth = radius * 0.1;
context.stroke();
context.beginPath();
for (let i = 0; i < 12; i++) {
let angle = (i * 30) * (Math.PI / 180);
context.moveTo(radius * 1.1 * Math.cos(angle) + context.canvas.width / 2, radius * 1.1 * Math.sin(angle) + context.canvas.height / 2);
context.lineTo(radius * 0.9 * Math.cos(angle) + context.canvas.width / 2, radius * 0.9 * Math.sin(angle) + context.canvas.height / 2);
context.stroke();
}
}
```
4、绘制指针
```javascript
function drawNumbers(context, radius) {
context.font = radius * 0.15 + 'px arial';
context.textBaseline = 'middle';
context.textAlign = 'center';
for (let i = 1; i <= 12; i++) {
let angle = (i * 30 90) * (Math.PI / 180);
context.rotate(angle);
context.translate(0, radius * 0.85);
context.rotate(angle);
context.fillText(i.toString(), context.canvas.width / 2, context.canvas.height / 2);
context.rotate(angle);
context.translate(0, radius * 0.85);
context.rotate(angle);
}
}
```
5、计算时针、分针、秒针的角度
```javascript
function setTime() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
return { hours, minutes, seconds };
}
```
6、绘制时针、分针、秒针
```javascript
function drawClockHands(context, time) {
const hours = time.hours % 12;
const minutes = time.minutes;
const seconds = time.seconds;
const hourAngle = (hours * 60 + minutes) * 0.5 * (Math.PI / 180);
const minuteAngle = minutes * 6 * (Math.PI / 180);
const secondAngle = seconds * 6 * (Math.PI / 180);
// Hours Hand
context.beginPath();
context.moveTo(context.canvas.width / 2, context.canvas.height / 2);
context.lineTo(context.canvas.width / 2 + 100 * Math.cos(hourAngle), context.canvas.height / 2 + 100 * Math.sin(hourAngle));
context.lineWidth = 8;
context.stroke();
// Minutes Hand
context.beginPath();
context.moveTo(context.canvas.width / 2, context.canvas.height / 2);
context.lineTo(context.canvas.width / 2 + 150 * Math.cos(minuteAngle), context.canvas.height / 2 + 150 * Math.sin(minuteAngle));
context.lineWidth = 6;
context.stroke();
// Seconds Hand
context.beginPath();
context.moveTo(context.canvas.width / 2, context.canvas.height / 2);
context.lineTo(context.canvas.width / 2 + 200 * Math.cos(secondAngle), context.canvas.height / 2 + 200 * Math.sin(secondAngle));
context.lineWidth = 4;
context.stroke();
}
```
7、更新时钟显示
```javascript
function updateClock() {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
drawFace(ctx, ctx.canvas.width / 2);
drawNumbers(ctx, ctx.canvas.width / 2);
const time = setTime();
drawClockHands(ctx, time);
setTimeout(updateClock, 1000);
}
```
8、启动时钟
```javascript
updateClock();
```
完整代码示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8"> <title>Canvas Clock</title> <style> canvas { display: block; margin: auto; backgroundcolor: #f0f0f0; border: 1px solid #000; } </style> </head> <body> <canvas id="clockCanvas" width="400" height="400"></canvas> <script> const canvas = document.getElementById('clockCanvas'); const ctx = canvas.getContext('2d'); canvas.width = 400; canvas.height = 400; function drawFace(context, radius) { /*...*/ } // 省略部分代码,请参考上面的详细解释。 function drawNumbers(context, radius) { /*...*/ } // 省略部分代码,请参考上面的详细解释。 function setTime() { /*...*/ } // 省略部分代码,请参考上面的详细解释。 function drawClockHands(context, time) { /*...*/ } // 省略部分代码,请参考上面的详细解释。 function updateClock() { /*...*/ } // 省略部分代码,请参考上面的详细解释。 updateClock(); </script> </body> </html>
FAQs
Q1: 如何调整时钟的样式?
A1: 你可以通过修改CSS样式来调整时钟的外观,改变canvas
的背景颜色、边框样式、指针的颜色和宽度等,在<style>
标签中进行相应的修改即可。
Q2: 如何使时钟在页面加载时自动开始运行?
A2: 如上文所示,通过调用updateClock()
函数,并在该函数末尾使用setTimeout
定时器每秒调用自身,可以确保时钟在页面加载时自动开始运行,如果需要停止时钟,只需移除或注释掉setTimeout
调用即可。
```html
```
这段HTML代码创建了一个简单的HTML5 Canvas时钟,以下是代码的详细说明:
1. `