Canvas画表实现当前时间走动

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
	*{padding: 0;margin: 0;}
	div{
		width:400px;height:400px;
		position:relative;
		margin:100px auto;
	}
	canvas{
		background:white;
	}
</style>
</head>
<body>
<div>
	<canvas id="mycan" width="400px" height="400px"></canvas>
</div>
<script>
	var myCan=document.getElementById("mycan");
	var Can=myCan.getContext("2d");
	Can.translate(200,200);
	var str=[12,1,2,3,4,5,6,7,8,9,10,11];
	Can.rotate(-Math.PI/2);


	setInterval(function(){
		Can.clearRect(-200,-200,400,400);
		var data = new Date();
		var h = data.getHours();
		var f = data.getMinutes();
		var s = data.getSeconds();
		var hours=h*Math.PI/6+f*Math.PI/360+s*Math.PI/21600;
		var minutes=f*Math.PI/30+s*Math.PI/30/60;
		var seconds=s*Math.PI/30;

		Can.save();
		Can.beginPath();
		for(var i=0;i<60;i++){
			Can.strokeStyle="red";
			Can.moveTo(0,0);
			Can.arc(0,0,180,(Math.PI)/30*i,(Math.PI/30)*(i+1),false);
			Can.stroke();
		}
		Can.restore();

		Can.save();
		Can.beginPath();
		Can.fillStyle="white"
		Can.arc(0,0,172,0,Math.PI*2,false);
		Can.fill();
		Can.restore();

		Can.save();
		Can.beginPath();
		for(var i=0;i<12;i++){
			Can.lineWidth="3";
			Can.strokeStyle="red";
			Can.moveTo(0,0);
			Can.arc(0,0,180,(Math.PI)/6*i,(Math.PI/6)*(i+1),false);
			Can.stroke();
		}
		Can.restore();

		Can.save();
		Can.beginPath();
		Can.fillStyle="white"
		Can.arc(0,0,165,0,Math.PI*2,false);
		Can.fill();
		Can.restore();

		//时
		Can.save();
		Can.beginPath();
		Can.rotate(seconds);
		Can.lineWidth="1";
		Can.moveTo(0,0);
		Can.lineTo(150,0);
		Can.stroke();
		Can.restore();

		//分
		Can.save();
		Can.beginPath();
		Can.rotate(minutes);
		Can.lineWidth="3";
		Can.moveTo(0,0);
		Can.lineTo(120,0);
		Can.stroke();
		Can.restore();

		//秒
		Can.save();
		Can.beginPath();
		Can.rotate(hours);
		Can.lineWidth="5";
		Can.moveTo(0,0);
		Can.lineTo(90,0);
		Can.stroke();
		Can.restore();

		Can.save();
		Can.beginPath();
		Can.fillStyle="black"
		Can.arc(0,0,5,0,Math.PI*2,false);
		Can.fill();
		Can.restore();

		Can.save();
		Can.beginPath();
		Can.rotate(Math.PI/2)
		Can.textBaseline="middle";
		Can.textAlign="center";
		Can.font="bold 20px 微软雅黑";
		for(var i=0;i<str.length;i++){
			Can.fillText(str[i],150*Math.sin(i*Math.PI/6),-150*Math.cos(i*Math.PI/6));
		}
		Can.restore();
	},1000)		
</script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个简单的表盘画法示例,使用了Tkinter的Canvas组件: ``` import math import tkinter as tk # 创建画布并设置大小 root = tk.Tk() canvas = tk.Canvas(root, width=300, height=300) canvas.pack() # 画表盘外圆 canvas.create_oval(50, 50, 250, 250) # 画刻度线和数字 for i in range(1, 13): angle = math.pi/6 * i x1 = 150 + 100 * math.cos(angle) y1 = 150 - 100 * math.sin(angle) x2 = 150 + 120 * math.cos(angle) y2 = 150 - 120 * math.sin(angle) canvas.create_line(x1, y1, x2, y2) canvas.create_text(x2, y2, text=str(i)) # 获取当前时间并计算指针位置 time = '13:30:45' hour, minute, second = map(int, time.split(':')) hour_angle = math.pi/6 * (hour % 12) + math.pi/360 * minute minute_angle = math.pi/30 * minute second_angle = math.pi/30 * second # 画时针 x = 150 + 60 * math.cos(hour_angle) y = 150 - 60 * math.sin(hour_angle) canvas.create_line(150, 150, x, y, width=5, fill='red') # 画分针 x = 150 + 80 * math.cos(minute_angle) y = 150 - 80 * math.sin(minute_angle) canvas.create_line(150, 150, x, y, width=3, fill='blue') # 画秒针 x = 150 + 90 * math.cos(second_angle) y = 150 - 90 * math.sin(second_angle) canvas.create_line(150, 150, x, y, width=1, fill='green') # 运行主循环 root.mainloop() ``` 这个示例会创建一个300x300的画布,并在中心画一个直径为200的圆形表盘,然后画上12个刻度线和数字。最后根据当前时间计算时针、分针和秒针的位置,用不同颜色的线条画出来。 你可以根据需要调整表盘大小和颜色、指针长度和粗细等参数,来实现自己想要的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值