利用canvas制作时钟表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>canvas画布绘制时钟</title>
    <style>
      body{background: black}
        #c1{background: white}
        span{color:white;}
    </style>
</head>
<body>
<canvas id="c1" width="400" height="400">
    <span>
        不支持canvas浏览器
    </span>
</canvas>
<script>
    var oC= document.querySelector("canvas");
    var oGC=oC.getContext('2d');
    //浏览器打开时停滞一秒才会运行,解决办法是封装成一个函数,开始时进行调用,在开始计时器。
    //进行画布的反复绘制,从而达到时针变化的效果。
    setInterval(function(){
        oGC.clearRect(0,0,oC.offsetWidth,oC.offsetHeight);
        //保存路径
        oGC.save();
        //大圆和小刻度
        for(var i=0;i<60;i++)
        {
            oGC.beginPath();
            oGC.moveTo(200,200);
            oGC.arc(200,200,100,i*6*Math.PI/180,(i+1)*6*Math.PI/180,false);
            oGC.stroke();
        }
        oGC.beginPath();
        oGC.moveTo(200,200);
        oGC.arc(200,200,90,0,360*Math.PI/180,false);
        oGC.fillStyle="white";
        oGC.fill();
        //大刻度
        for(var i=0;i<12;i++) {
            oGC.beginPath();
            oGC.moveTo(200,200);
            oGC.arc(200,200,100,i*30*Math.PI/180,(i+1)*30*Math.PI/180,false);
            oGC.lineWidth='2';
            oGC.stroke();
        }
        oGC.beginPath();
        oGC.fillStyle="#fff";
        oGC.arc(200,200,80,0,360*Math.PI/180,false)
        oGC.fill();
        //时间的换算
        var oD=new Date();
        //canvas起点是3点位置所以-90是12点的位置。小时的转化是由小时和分钟进行转化。1小时为30度,30度为60分钟,则1分钟为0.5度
        var oH=Math.floor(oD.getHours()*30-90+oD.getMinutes()/2)
        //同理
        var oM=Math.floor(oD.getMinutes()*6-90+oD.getSeconds()/10);
        var oS=oD.getSeconds()*6-90;
        //时针的绘制
        oGC.beginPath();
        oGC.moveTo(200,200);
        oGC.arc(200,200,40,oH*Math.PI/180,oH*Math.PI/180,false);
        oGC.lineWidth='4';
        oGC.strokeStyle="pink";
        oGC.stroke();
        //分针的绘制
        oGC.beginPath();
        oGC.moveTo(200,200);
        oGC.arc(200,200,60,oM*Math.PI/180,oM*Math.PI/180,false);
        oGC.lineWidth='3';
        oGC.strokeStyle="yellow";
        oGC.stroke();
        //秒针的绘制
        oGC.beginPath();
        oGC.moveTo(200,200);
        oGC.arc(200,200,80,oS*Math.PI/180,oS*Math.PI/180,false);
        oGC.lineWidth='2';
        oGC.strokeStyle="green";
        oGC.stroke();
  oGC.restore();
    },1000)

</script>
</body>
</html>

注意:1.圆的绘制:arc(x,y,半径,起始弧度,结束弧度,旋转方向)

           3.canvas的圆的起点在3点钟的位置。

           2.旋转方向:顺时针(默认:false)、逆时针(true)。

          4.时间转化。

 

好的,以下是使用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、付费专栏及课程。

余额充值