canvas实现动态闹钟

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>动态闹钟</title>
</head>
<body>
    <canvas id=mycanvas width=800px height=800px style="background:white"></canvas>
    <script>
        var cav=document.getElementById("mycanvas");
        var cxt=cav.getContext("2d");

        function drawclock(){
            cxt.clearRect(0, 0, 800, 800)
        var now=new Date();
        hour=now.getHours();
        sec=now.getSeconds();
        min=now.getMinutes();
        hour=hour+min/60+sec/3600;
        min=min+sec/3600;
        hour=hour>12?hour-12:hour;
        cxt.beginPath();
        cxt.arc(400,400,300,0,2*Math.PI,false);
        cxt.strokeStyle='rgb(0,0,0)'
        cxt.stroke();
        cxt.closePath();

        var endTime=new Date("2020/12/19 00:00:00")
        var second=parseInt((endTime.getTime()-now.getTime())/1000)
        var date=parseInt(second/3600/24);
         cxt.beginPath()
         cxt.fillStyle='red'
        cxt.font="30px 楷体"
     //   cxt.fillstyle='red';
        cxt.fillText("距2021年考研还剩: "+date+"天 ",250,500)
        cxt.fillStyle='black'
      cxt.closePath()
     /*  cxt.fillStyle='red'
    cxt.font="20px 楷体"
    cxt.fillText("距2021年考研还剩: "+date+"天 ",250,500)*/

       for(var i=0;i<12;i++)
       {
        cxt.beginPath();
        cxt.save();
        cxt.translate(400,400);
         cxt.rotate(i*30*Math.PI/180);
        cxt.fillRect(0,-300,3,20);
        cxt.strokeStyle='black';
        cxt.stroke();
        cxt.restore();
        cxt.closePath();
        }
        for(var i=0;i<60;i++)
        {
            cxt.beginPath();
            cxt.save();
            cxt.translate(400,400);
            cxt.rotate(i*6*Math.PI/180);
            cxt.fillRect(0,-300,1,10);
            cxt.strokeStyle='black';
            cxt.stroke();
            cxt.restore();
            cxt.closePath();
        }
        //分针
        cxt.beginPath();
        cxt.save();
        cxt.translate(400,400);
        cxt.rotate(min*6*Math.PI/180);
        cxt.fillRect(-4,-230,8,265);
        cxt.strokeStyle='black';
        cxt.stroke();
        cxt.restore();
        cxt.closePath();
        //时针
        cxt.beginPath();
        cxt.save();
        cxt.translate(400,400);
        cxt.rotate(hour*30*Math.PI/180);
        cxt.fillRect(-5,-150,10,185);
        cxt.strokeStyle='black';
        cxt.stroke();
        cxt.restore();
        cxt.closePath();
        //秒针
        cxt.beginPath();
        cxt.save();
        cxt.translate(400,400);
        cxt.rotate(6*sec*Math.PI/180);
        cxt.fillRect(-1,-200,2,240)
        cxt.strokeStyel='black'
        cxt.restore()
        cxt.closePath();

        cxt.beginPath();
        cxt.arc(400,400,12,0,2*Math.PI,false);
        cxt.fillStyle='black';
        cxt.fill();
        cxt.stroke();
        cxt.closePath();
        //添加数字
        //
        for(var i=12;i>=1;i--)
        {
            cxt.beginPath();
            cxt.save();
            cxt.translate(400,400);
           // cxt.rotate(-(12-i)*30*Math.PI/180)
            cxt.font="60px 楷体 black";
            if(i>=6)
            cxt.fillText(i,250*Math.cos((90+(12-i)*30)*Math.PI/180)-25,-250*Math.sin((90+(12-i)*30)*Math.PI/180)+20);
        else{
            cxt.fillText(i,250*Math.cos((90+(12-i)*30)*Math.PI/180)-3,-250*Math.sin((90+(12-i)*30)*Math.PI/180)+20);

        }
            cxt.restore();
            cxt.closePath();


        }

         }

        setInterval(drawclock,1000)

    </script>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值