Canvas制作简单的时钟表盘

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas" width="500" height="500" style="border: 1px solid red ;background:#000"></canvas>
    <script>
        var canvas = document.getElementById('canvas');
        var cxt = canvas.getContext('2d');
        function clock() {
            cxt.restore();
            cxt.clearRect(0, 0, canvas.width, canvas.height);
            var date = new Date();
            var hour = date.getHours();
            var min = date.getMinutes();
            var sec = date.getSeconds();
            hour = hour > 12 ? hour - 12 : hour;
            hour += (min / 60);
            xt.strokeStyle = "#00FFFF";
            cxt.lineWidth = 5;
            cxt.beginPath();
            cxt.arc(250, 250, 200, 0, 2 * Math.PI);
            cxt.stroke();
            cxt.closePath();
            cxt.save();
            cxt.translate(250, 250);
            cxt.strokeStyle = '#f00';
            cxt.lineWidth = 7;
            for (var i = 0; i < 12; i++) {
                cxt.rotate(30 * Math.PI / 180);
                cxt.beginPath();
                cxt.moveTo(0, -195);
                cxt.lineTo(0, -175);
                cxt.closePath();
                cxt.stroke();
            }
            cxt.restore();
            cxt.save();
            cxt.translate(250, 250);
            cxt.strokeStyle = '#f00';
            cxt.lineWidth = 5;
            for (var i = 0; i < 60; i++) {
                cxt.rotate(6 * Math.PI / 180);
                cxt.beginPath();
                cxt.moveTo(0, -195);
                cxt.lineTo(0, -185);
                cxt.closePath();
                cxt.stroke();
            }
            cxt.restore();
            cxt.save();
            cxt.strokeStyle = '#FFFF00';
            cxt.translate(250, 250);
            cxt.lineWidth = 7;
            cxt.rotate(hour * 30 * Math.PI / 180);
            cxt.beginPath();
            cxt.moveTo(0, -100);
            cxt.lineTo(0, 10);
            cxt.closePath();
            cxt.stroke();
            cxt.restore();
            cxt.save();
            cxt.lineWidth = 5;
            cxt.strokeStyle = 'blue';
            cxt.translate(250, 250);
            cxt.rotate(min * 6 * Math.PI / 180);
            cxt.beginPath();
            cxt.moveTo(0, -150);
            cxt.lineTo(0, 10);
            cxt.closePath();
            cxt.stroke();
            cxt.restore();
            cxt.save();
            cxt.lineWidth = 3;
            cxt.strokeStyle = '#FF0000';
            cxt.translate(250, 250);
            cxt.rotate(sec * 6 * Math.PI / 180);
            cxt.beginPath();
            cxt.moveTo(0, -170);
            cxt.lineTo(0, 10);
            cxt.closePath();
            cxt.stroke();
            cxt.beginPath();
            cxt.arc(0, 0, 7, 0, 2 * Math.PI);
            cxt.closePath();
            cxt.strokeStyle = "#ff0000";
            cxt.fillStyle = "#ffff00";
            cxt.fill();
            cxt.stroke();
            cxt.beginPath();
            cxt.arc(0, -140, 7, 0, 2 * Math.PI);
            cxt.closePath();
            cxt.strokeStyle = "#ff0000";
            cxt.fillStyle = "#ffff00";
            cxt.fill();
            cxt.stroke();
        }


        setInterval(clock, 900);
    </script>
</body>

</html>

效果图如下,时间是可以走的
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值