在html中用canvas制作时钟,html5 canvas 画时钟

HTML>

// get the canvas

var myCanvas = document.getElementById("myCanvas");

var ctx = myCanvas.getContext("2d");

function showClock(){

ctx.save();

ctx.save();

ctx.save();

ctx.save();

ctx.save();

ctx.save();

ctx.save();

ctx.clearRect(0,400,400);

// create the clock border

ctx.restore();

ctx.beginPath();

ctx.strokeStyle="pink";

ctx.lineWidth=10;

ctx.arc(200,200,150,Math.PI*2,true);

ctx.stroke();

// create the hour icon

ctx.restore();

ctx.lineWidth=8;

ctx.strokeStyle="blue";

ctx.translate(200,200);

for(var i=0;i<12;i++){

ctx.beginPath();

ctx.moveTo(135,0);

ctx.lineTo(150,0);

ctx.rotate(Math.PI/6);

ctx.stroke();

}

// create the minute icon

ctx.restore();

ctx.lineWidth=4;

ctx.strokeStyle="blue";

ctx.translate(200,200);

for(var i=0;i<60;i++){

ctx.beginPath();

ctx.moveTo(145,0);

ctx.rotate(Math.PI/30);

ctx.stroke();

}

var date = new Date();

var hour = date.getHours();

var minute = date.getMinutes();

var second = date.getSeconds();

hour = hour>=12?hour-12:hour;

// create the hour hand

ctx.restore();

ctx.lineWidth = 14;

ctx.strokeStyle="pink";

ctx.translate(200,200);

ctx.rotate(hour*(Math.PI/6) + (Math.PI/360)*minute + (Math.PI/21600)*second-Math.PI/2);

ctx.beginPath();

ctx.moveTo(-20,0);

ctx.lineTo(90,0);

ctx.stroke();

// create the minute hand

ctx.restore();

ctx.lineWidth = 10;

ctx.strokeStyle="pink";

ctx.translate(200,200);

ctx.rotate((Math.PI/30)*minute + (Math.PI/1800)*second-Math.PI/2);

ctx.beginPath();

ctx.moveTo(-30,0);

ctx.lineTo(125,0);

ctx.stroke();

// create the second hand

ctx.restore();

ctx.lineWidth = 4;

ctx.strokeStyle="pink";

ctx.translate(200,200);

ctx.rotate( (Math.PI/30)*second-Math.PI/2);

ctx.beginPath();

ctx.moveTo(-35,0);

ctx.lineTo(135,0);

ctx.stroke();

ctx.restore();

setTimeout(showClock,1000);

}

showClock();

以下是一个简单的用布标记绘制时钟的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas Clock</title> <style> canvas { border: 1px solid black; } </style> </head> <body> <canvas id="clock" width="200" height="200"></canvas> <script> // 获取布 var canvas = document.getElementById("clock"); var ctx = canvas.getContext("2d"); // 设置时钟半径和圆心坐标 var radius = canvas.width / 2; var centerX = canvas.width / 2; var centerY = canvas.height / 2; // 绘制时钟 function drawClock() { // 绘制外圆 ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI); ctx.stroke(); // 绘制时钟刻度 for (var i = 0; i < 12; i++) { var angle = i * Math.PI / 6; ctx.beginPath(); ctx.moveTo(centerX + (radius - 10) * Math.cos(angle), centerY + (radius - 10) * Math.sin(angle)); ctx.lineTo(centerX + (radius - 20) * Math.cos(angle), centerY + (radius - 20) * Math.sin(angle)); ctx.stroke(); } // 绘制时针 var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); var hourAngle = (hour % 12) * Math.PI / 6 + minute * Math.PI / 360 + second * Math.PI / 21600; ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(centerX + (radius - 50) * Math.cos(hourAngle), centerY + (radius - 50) * Math.sin(hourAngle)); ctx.stroke(); // 绘制分针 var minuteAngle = minute * Math.PI / 30 + second * Math.PI / 1800; ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(centerX + (radius - 30) * Math.cos(minuteAngle), centerY + (radius - 30) * Math.sin(minuteAngle)); ctx.stroke(); // 绘制秒针 var secondAngle = second * Math.PI / 30; ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(centerX + (radius - 10) * Math.cos(secondAngle), centerY + (radius - 10) * Math.sin(secondAngle)); ctx.stroke(); } // 每秒更新时钟 setInterval(drawClock, 1000); </script> </body> </html> ``` 在这个示例中,我们首先获取了一个 ID 为 "clock" 的布元素,然后通过 `getContext()` 方法获取了一个 2D 上下文对象。我们设置了布的宽度和高度,以及时钟的半径和圆心坐标。 在 `drawClock()` 函数中,我们首先绘制了外圆和时钟刻度。然后获取当前时间,计算出时针、分针和秒针的角度,并使用 `moveTo()` 和 `lineTo()` 方法绘制出对应的时针、分针和秒针。 最后,在 `setInterval()` 中每秒调用一次 `drawClock()` 函数来更新时钟
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值