用canvas制作钟表

<!DOCYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<canvas id="canvas" width="500px" height="500px"></canvas>
		
		<script type="text/javascript">
			//获取canvas对象
			var oCanvas = document.getElementById("canvas");
			//获取上下文(画笔)默认画笔颜色为黑色     fillStyle改变颜色
			var context = oCanvas.getContext("2d");
		(function drawClock () {
			//清空画布
			context.clearRect(0,0,oCanvas.width,oCanvas.height);
		
			//绘制背景
			context.fillStyle="#eee";//实心
			//绘制矩形
			context.fillRect(0, 0, oCanvas.width,oCanvas.height);

                        //空心
			//context.strokeStyle = "#eee";
			//绘制矩形
			//context.strokeRect(0, 0, oCanvas.width,oCanvas.height);
			
			//绘制表盘
			context.beginPath();//开始
			context.arc(250, 250, 200, 0, 2 * Math.PI);
			context.strokeStyle="#000";
			context.lineWidth = 4;//设置线的宽度
			context.stroke();//开始画圆
			context.closePath();//关闭
			
			//绘制分刻度
			context.save();//保存当前状态
			context.translate(250,250);//改变坐标轴原点
			for(var i=0; i<60; i++){
				context.beginPath();
				context.rotate(Math.PI/30);//旋转
				context.moveTo(0,-185);//起点
				context.lineTo(0,-195);//终点
				context.lineWidth = 2;
				context.stroke();
				context.closePath();
			}
			context.restore();//将画布恢复到初始状态
			
			
			//绘制时刻度
			context.save();
			context.translate(250,250);//改变坐标轴原点
			for(var i=0; i<12; i++){
				context.beginPath();
				context.rotate(Math.PI/6);//旋转
				context.moveTo(0,-175);
				context.lineTo(0,-195);
				context.lineWidth = 4;
				context.stroke();
				context.closePath();
			}
			context.restore();//将画布恢复到初始状态
			
			//绘制指针
			//获取当前时间
			var now = new Date();
			var sec = now.getSeconds();
			var min = now.getMinutes() + sec / 60;
			var hour = now.getHours() + min / 60;
			
			//绘制时针指针
			context.save();
			context.translate(250,250);
			context.rotate(Math.PI / 6*hour);
			context.beginPath();
			context.moveTo(0,10);
			context.lineTo(0,-150);
			context.lineWidth = 4;
                        context.stroke();
			context.closePath();
			context.restore();
			
			//繪製分針指針
			context.save();
			context.translate(250,250);
			context.rotate(Math.PI / 30*min);
			context.beginPath();
			context.moveTo(0,15);
			context.lineTo(0,-165);
			context.lineWidth = 3;
                        context.stroke();
			context.closePath();
			context.restore();
			
			//绘制秒针指针
			context.save();
			context.translate(250,250);
			context.rotate(Math.PI / 30 * sec);
			context.beginPath();
			context.moveTo(0,25);
			context.lineTo(0,-160);
			context.lineWidth = 2;
			context.strokeStyle="red";
                        context.stroke();
			context.closePath();
			
			//画秒针上的两个小圆
			context.beginPath();
			context.arc(0,0,5,0,2*Math.PI);
			context.fillStyle="white";
			context.strokeStyle="red";
			context.fill();
			context.stroke();
			context.closePath();
			
			context.beginPath();
			context.arc(0,-130,5,0,2*Math.PI);
			context.fillStyle="white";
			context.strokeStyle="red";
			context.fill();
			context.stroke();
			context.closePath();
			
			context.restore();
			
			window.requestAnimationFrame(drawClock);
		})();
			
		</script>
		
	</body>
</html>



好的,以下是使用Python的tkinter模块和canvas组件制作动态钟表的代码: ``` import tkinter as tk import time class Clock: def __init__(self, canvas, x, y, radius): self.canvas = canvas self.x = x self.y = y self.radius = radius self.hour_hand = None self.minute_hand = None self.second_hand = None def draw_clock_face(self): self.canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, width=2) def draw_hour_marks(self): for i in range(12): x1 = self.x + (self.radius - 10) * math.cos(math.pi / 6 * i - math.pi / 2) y1 = self.y + (self.radius - 10) * math.sin(math.pi / 6 * i - math.pi / 2) x2 = self.x + (self.radius - 20) * math.cos(math.pi / 6 * i - math.pi / 2) y2 = self.y + (self.radius - 20) * math.sin(math.pi / 6 * i - math.pi / 2) self.canvas.create_line(x1, y1, x2, y2, width=2) def draw_hour_hand(self, hour): length = self.radius * 0.5 angle = math.pi / 6 * (hour % 12) - math.pi / 2 x = self.x + length * math.cos(angle) y = self.y + length * math.sin(angle) if self.hour_hand: self.canvas.coords(self.hour_hand, self.x, self.y, x, y) else: self.hour_hand = self.canvas.create_line(self.x, self.y, x, y, width=4) def draw_minute_hand(self, minute): length = self.radius * 0.8 angle = math.pi / 30 * minute - math.pi / 2 x = self.x + length * math.cos(angle) y = self.y + length * math.sin(angle) if self.minute_hand: self.canvas.coords(self.minute_hand, self.x, self.y, x, y) else: self.minute_hand = self.canvas.create_line(self.x, self.y, x, y, width=2) def draw_second_hand(self, second): length = self.radius * 0.9 angle = math.pi / 30 * second - math.pi / 2 x = self.x + length * math.cos(angle) y = self.y + length * math.sin(angle) if self.second_hand: self.canvas.coords(self.second_hand, self.x, self.y, x, y) else: self.second_hand = self.canvas.create_line(self.x, self.y, x, y, fill="red", width=1) def update_clock(clock): now = time.localtime() hour = now.tm_hour minute = now.tm_min second = now.tm_sec clock.draw_hour_hand(hour + minute / 60) clock.draw_minute_hand(minute) clock.draw_second_hand(second) clock.canvas.after(1000, update_clock, clock) def main(): root = tk.Tk() root.title("Clock") canvas = tk.Canvas(root, width=400, height=400) canvas.pack() clock = Clock(canvas, 200, 200, 150) clock.draw_clock_face() clock.draw_hour_marks() update_clock(clock) root.mainloop() if __name__ == "__main__": main() ``` 在这个程序中,`Clock`类表示时钟,包括钟表的位置、半径和时针、分针、秒针的状态。`draw_clock_face()`方法用于绘制钟表的圆形,`draw_hour_marks()`方法用于绘制小时刻度线,`draw_hour_hand()`、`draw_minute_hand()`、`draw_second_hand()`方法分别用于绘制时针、分针、秒针。`update_clock()`函数用于更新时钟的状态,每隔一秒钟调用一次。在`main()`函数中,创建主窗口和画布,创建时钟对象并绘制时钟,调用`update_clock()`函数更新时钟的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值