Html Canvas之动态表盘时钟(带数字刻度数)

Html Canvas之动态表盘时钟(带数字刻度数)

能够实现指针按标准时间走,并加入一个倒计时。
效果图:
在这里插入图片描述
代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>clock</title>
</head>
<body>
	<canvas id=mycanvas width=500 height=500 ></canvas>
	<script>
	var cav=document.getElementById("mycanvas");
	var cxt=cav.getContext("2d");

	function drawClock(){
		cxt.clearRect(0,0,500,500)

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

	var endTime=new Date("2020/12/19 00:00:00"); 
	var second = parseInt((endTime.getTime()-now.getTime())/1000);
	var d = parseInt(second/3600/24); //天数
	cxt.fillStyle='red'
	cxt.font="15px 楷体"
	cxt.fillText("距2021年考研还剩: "+d+"天 ",160,330)		

	// 画表盘
	cxt.beginPath()
	cxt.strokeStyle='#999';
	cxt.lineWidth=1;
	cxt.arc(250,250,205,0,2*Math.PI,false)
	cxt.stroke()
	cxt.closePath()

	cxt.beginPath()
	cxt.strokeStyle='black';
	cxt.lineWidth=2;
	cxt.arc(250,250,190,0,2*Math.PI,false)
	cxt.stroke()
	cxt.closePath()

	// 画时刻度
	for(var i=0;i<12;i++){
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=2
		cxt.strokeStyle='black'
		cxt.rotate(i*30*Math.PI/180)
		cxt.moveTo(0,-190)
		cxt.lineTo(0,-170)
		cxt.stroke()
		cxt.restore()
		cxt.closePath()
	}
	 // 画分刻度
	for(var i=0;i<60;i++){
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=1
		cxt.strokeStyle='black'
		cxt.rotate(i*6*Math.PI/180)
		cxt.moveTo(0,-190)
		cxt.lineTo(0,-180)
		cxt.stroke()
		cxt.restore()
		cxt.closePath()
	}

    //表盘数字
	for(var i=0;i<12;i++){
		var num=[3,4,5,6,7,8,9,10,11,12,1,2]
         cxt.beginPath()
		 cxt.save()
		cxt.translate(235,265)
	    cxt.fillStyle='black'
	    cxt.font="50px 黑体"
		var x=Math.cos(2*Math.PI/12*i)*140;
		var y=Math.sin(2*Math.PI/12*i)*140;
		cxt.fillText(num[i],x,y)
	    cxt.stroke()
		cxt.restore()
		cxt.closePath()
    }

	// 画时针
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=5
		cxt.strokeStyle='black'
		cxt.rotate(30*hour*Math.PI/180)
		cxt.moveTo(0,-100)
		cxt.lineTo(0,10)
		cxt.stroke()
		cxt.restore()
		cxt.closePath()

		// 画分针
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=3
		cxt.strokeStyle='black'
		cxt.rotate(min*6*Math.PI/180)
		cxt.moveTo(0,-130)
		cxt.lineTo(0,17)
		cxt.stroke()
		cxt.restore()
		cxt.closePath()

		// 画秒针
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=1
		cxt.strokeStyle='red'
		cxt.rotate(sec*6*Math.PI/180)
		cxt.moveTo(0,-150)
		cxt.lineTo(0,25)
		cxt.stroke()
		cxt.restore()
		cxt.closePath()

		// 画圆心
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=1
		cxt.strokeStyle='red'
		cxt.fillStyle="#ccc"
		cxt.arc(0,0,5,0,2*Math.PI,true)
		cxt.stroke()
		cxt.fill()
		cxt.restore()
		cxt.closePath()


		// 画秒针末端圆圈
		cxt.beginPath()
		cxt.save()
		cxt.translate(250,250)
		cxt.lineWidth=1
		cxt.strokeStyle='red'
		cxt.fillStyle="#ccc"
		cxt.rotate(sec*6*Math.PI/180)
		cxt.arc(0,-120,5,0,2*Math.PI,true)
		cxt.stroke()
		cxt.fill()
		cxt.restore()
		cxt.closePath()
	}
		setInterval(drawClock,1000)

	</script>
	
	
</body>
</html>
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个自己设置表盘指针刻度的Tkinter时钟代码,并加上详细注释: ```python import tkinter as tk from math import cos, sin, pi import time # 创建主窗口 root = tk.Tk() root.title("时钟") # 创建标签,用于显示时间 time_label = tk.Label(root, font=("Arial", 80)) time_label.pack() # 画表盘 def draw_clock(): # 获取画布大小 width = canvas.winfo_width() height = canvas.winfo_height() # 计算表盘半径和中心坐标 r = min(width, height) / 2 * 0.8 cx = width / 2 cy = height / 2 # 画表盘外圆 canvas.delete("clock") # 删除之前的表盘 canvas.create_oval(cx-r, cy-r, cx+r, cy+r, width=5, outline="black", tags="clock") # 画表盘刻度 for i in range(12): angle = pi / 6 * i # 计算刻度线的角度 x1 = cx + (r-20) * cos(angle) y1 = cy + (r-20) * sin(angle) x2 = cx + r * cos(angle) y2 = cy + r * sin(angle) canvas.create_line(x1, y1, x2, y2, width=5, tags="clock") # 画表盘指针 # 计算指针位置 now = time.localtime() hour_angle = pi / 6 * now.tm_hour + pi / 360 * now.tm_min minute_angle = pi / 30 * now.tm_min second_angle = pi / 30 * now.tm_sec hour_length = r * 0.5 minute_length = r * 0.7 second_length = r * 0.9 # 画指针 # 时针 x = cx + hour_length * sin(hour_angle) y = cy - hour_length * cos(hour_angle) canvas.create_line(cx, cy, x, y, width=10, fill="red", tags="clock") # 分针 x = cx + minute_length * sin(minute_angle) y = cy - minute_length * cos(minute_angle) canvas.create_line(cx, cy, x, y, width=5, fill="blue", tags="clock") # 秒针 x = cx + second_length * sin(second_angle) y = cy - second_length * cos(second_angle) canvas.create_line(cx, cy, x, y, width=2, fill="green", tags="clock") # 在1000ms后调用自身,实现更新表盘指针 canvas.after(1000, draw_clock) # 创建画布,用于画表盘 canvas = tk.Canvas(root, width=600, height=600) canvas.pack(expand=True, fill="both") # 在100ms后调用draw_clock()函,实现画表盘和指针 canvas.after(100, draw_clock) # 运行主循环 root.mainloop() ``` 这个程序使用Tkinter创建一个窗口,通过Canvas组件画出时钟表盘和指针。程序分为两个主要部分:`draw_clock()`函和主循环部分。 `draw_clock()`函用于画表盘和指针,并且每秒钟更新指针位置。首先获取画布的大小,计算表盘半径和中心坐标,然后画出表盘外圆和刻度线。接着计算当前时间对应的指针位置,画出时针、分针和秒针。最后在1000ms后调用自身,实现每秒钟更新指针位置。 主循环部分首先创建画布,然后在100ms后调用`draw_clock()`函,实现画出表盘和指针。最后运行主循环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值