canvas实现时钟效果

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>时钟</title>
	</head>

	<body>
		<canvas id="clock" width="500" height="500" style="background-color:black;">你的浏览器不支持canvas</canvas>
		<script type="text/javascript">
			var canvas = document.getElementById("clock");
			var cxt = canvas.getContext("2d");

			function drawClock() {
				var now = new Date();
				var sec = now.getSeconds();
				var min = now.getMinutes();
				var hour = now.getHours();
				hour > 12 ? hour - 12 : hour;
				hour += (min / 60);

				//先清空画布
				cxt.clearRect(0, 0, canvas.width, canvas.height);

				//美女图片作为表盘背景     
				var img = new Image();
				img.src = "http://i3.static.olcdn.com/cms/201303/14/MjMw1363244722894_500.jpg";
				img.onload = function() {
					cxt.drawImage(img, 0, 50, canvas.width, 400);

					_innerDraw();
				}

				function _innerDraw() {
					//画表盘大圆 圆心:x=250 y=250
					cxt.strokeStyle = "#00FFFF";
					cxt.lineWidth = 10;
					cxt.beginPath();
					cxt.arc(250, 250, 200, 0, 2 * Math.PI, false);
					cxt.stroke();
					cxt.closePath();

					//时刻度
					for (var i = 0; i < 12; i++) {
						cxt.save(); //保存当前状态
						cxt.lineWidth = 7;
						cxt.strokeStyle = "#FFFF00";

						//设置原点
						cxt.translate(250, 250);
						//设置旋转角度
						cxt.rotate(30 * i * Math.PI / 180); //弧度   角度*Math.PI/180
						cxt.beginPath();
						cxt.moveTo(0, -175);
						cxt.lineTo(0, -195);
						cxt.stroke();
						cxt.closePath();
						cxt.restore(); //把原来状态恢复回来
					}

					//分刻度
					for (var i = 0; i < 60; i++) {
						cxt.save();
						cxt.lineWidth = 5;
						cxt.strokeStyle = "#FFFF00";
						cxt.translate(250, 250);
						cxt.rotate(i * 6 * Math.PI / 180);
						cxt.beginPath();
						cxt.moveTo(0, -185);
						cxt.lineTo(0, -195);
						cxt.stroke();
						cxt.closePath();
						cxt.restore();
					}



					//以下的时针、分针、秒针均要转动,所以在这里要设置其异次元空间的位置
					//根据当前的小时数、分钟数、秒数分别设置各个针的角度即可
					//-----------------------------时针-----------------------------
					cxt.save();
					cxt.lineWidth = 7;
					cxt.strokeStyle = "#00FFFF";
					cxt.translate(250, 250);
					cxt.rotate(hour * 30 * Math.PI / 180); //每小时旋转30度
					cxt.beginPath();
					cxt.moveTo(0, -130);
					cxt.lineTo(0, 10);
					cxt.stroke();
					cxt.closePath();
					cxt.restore();

					//-----------------------------分针-----------------------------
					cxt.save();
					cxt.lineWidth = 5;
					cxt.strokeStyle = "#FFFF00";
					cxt.translate(250, 250);
					cxt.rotate(min * 6 * Math.PI / 180); //每分钟旋转6度
					cxt.beginPath();
					cxt.moveTo(0, -150);
					cxt.lineTo(0, 10);
					cxt.stroke();
					cxt.closePath();
					cxt.restore();

					//-----------------------------秒针-----------------------------
					cxt.save();
					cxt.lineWidth = 3;
					cxt.strokeStyle = "#FF0000";
					cxt.translate(250, 250);
					cxt.rotate(sec * 6 * Math.PI / 180); //每秒旋转6度
					cxt.beginPath();
					cxt.moveTo(0, -170);
					cxt.lineTo(0, 10);
					cxt.stroke();
					cxt.closePath();


					//美化表盘,画中间的小圆
					cxt.beginPath();
					cxt.arc(0, 0, 7, 0, 360 * Math.PI / 180);
					cxt.fillStyle = "#FFFF00";
					cxt.fill();
					cxt.strokeStyle = "#FF0000";
					cxt.stroke();
					cxt.closePath();

					//秒针上的小圆
					cxt.beginPath();
					cxt.arc(0, -140, 7, 0, 360 * Math.PI / 180);
					cxt.fillStyle = "#FFFF00";
					cxt.fill();
					cxt.stroke();
					cxt.closePath();
					cxt.restore();


					//显示时间
					cxt.font = "normal 18px microsoft yahei";
					cxt.lineWidth = 2;
					cxt.fillStyle = "#0000FF";
					hour = now.getHours();
					var str = (hour > 10 ? hour : ("0" + hour)) + ":" + (min > 10 ? min : ("0" + min));
					cxt.fillText(str, 225, 380);

					//中国制造
					cxt.font = "bold 12px simsun";
					cxt.lineWidth = 1;
					cxt.fillText("Made In China", 210, 400);
				}
			}

			drawClock();
			setInterval(drawClock, 1000);
		</script>
	</body>

</html>



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的HTML5 Canvas绘制时钟的示例代码: ``` <!DOCTYPE html> <html> <head> <title>Canvas时钟</title> <style> canvas { border: 2px solid black; } </style> </head> <body> <canvas id="clock" width="200" height="200"></canvas> <script> function drawClock() { // 获取Canvas元素 var canvas = document.getElementById("clock"); var ctx = canvas.getContext("2d"); // 获取Canvas中心点坐标 var centerX = canvas.width / 2; var centerY = canvas.height / 2; // 绘制表盘 ctx.beginPath(); ctx.arc(centerX, centerY, 80, 0, 2 * Math.PI); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制刻度 for (var i = 1; i <= 12; i++) { var angle = i * Math.PI / 6; var x = centerX + 60 * Math.sin(angle); var y = centerY - 60 * Math.cos(angle); ctx.beginPath(); ctx.arc(x, y, 5, 0, 2 * Math.PI); ctx.fillStyle = "black"; ctx.fill(); } // 绘制时针 var now = new Date(); var hour = now.getHours() % 12; var minute = now.getMinutes(); var second = now.getSeconds(); var hourAngle = (hour + minute / 60) * Math.PI / 6; var hourX = centerX + 40 * Math.sin(hourAngle); var hourY = centerY - 40 * Math.cos(hourAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(hourX, hourY); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制分针 var minuteAngle = minute * Math.PI / 30; var minuteX = centerX + 60 * Math.sin(minuteAngle); var minuteY = centerY - 60 * Math.cos(minuteAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(minuteX, minuteY); ctx.strokeStyle = "black"; ctx.stroke(); // 绘制秒针 var secondAngle = second * Math.PI / 30; var secondX = centerX + 60 * Math.sin(secondAngle); var secondY = centerY - 60 * Math.cos(secondAngle); ctx.beginPath(); ctx.moveTo(centerX, centerY); ctx.lineTo(secondX, secondY); ctx.strokeStyle = "red"; ctx.stroke(); } // 每秒钟更新时钟 setInterval(drawClock, 1000); </script> </body> </html> ``` 这段代码使用Canvas绘制了一个时钟,并且每秒钟更新一次。你可以将代码复制到一个HTML文件中运行,就可以看到时钟效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值