canvas画时钟

使用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>
    <style>
        body {
            background: #000;
        }

        * {
            margin: 0;
            padding: 0;
        }

        #clock {
            margin: 100px 550px;
            background: #000;
        }
    </style>
</head>
<body>
<canvas width="500" height="500" id="clock">
    您的浏览器不支持canvas
</canvas>
<script>
    var oClock = document.getElementById("clock");
    // 设置环境
    var clock = oClock.getContext('2d');
    // 时钟函数
    function drawClock() {
        clock.clearRect(0, 0, 500, 500);
        var time = new Date();
        var sec = time.getSeconds();
        var min = time.getMinutes();
        var hour = time.getHours();
        hour = hour + min / 60;
        /*if (hour>12){
            hour=12;
        }*/
        // console.log(hour)
        // 画出表盘轮廓
        clock.strokeStyle = "#00f";
        clock.lineWidth = 10;
        clock.beginPath();
        clock.arc(250, 250, 200, 0, 360);
        clock.stroke();
        clock.closePath();
        // 画出小时刻度
        for (let i = 1; i <= 12; i++) {
            clock.save();
            clock.lineWidth = 8;
            clock.strokeStyle = "#fff";
            // clock.font = "14px 黑体";
            clock.translate(250, 250);
            clock.rotate(i * 30 * Math.PI / 180);
            clock.beginPath();
            clock.moveTo(0, -170);
            clock.lineTo(0, -195);
            // clock.fillText(i,0,-150);
            clock.closePath();
            clock.stroke();
            clock.restore();

        }
        // 画出分钟刻度
        for (let i = 1; i <= 60; i++) {
            clock.save();
            clock.lineWidth = 5;
            clock.strokeStyle = "#0f0";
            // clock.font = "14px 黑体";
            clock.translate(250, 250);
            clock.rotate(i * 6 * Math.PI / 180);
            clock.beginPath();
            clock.moveTo(0, -180);
            clock.lineTo(0, -192);
            // clock.fillText(i,0,-150);
            clock.closePath();
            clock.stroke();
            clock.restore();

        }
        // 时针
        clock.save();
        clock.translate(250, 250);
        clock.rotate(hour * 30 * Math.PI / 180);
        clock.beginPath();
        clock.moveTo(-2, -90);
        clock.lineTo(-5, 15);
        clock.lineTo(5, 15);
        clock.lineTo(2, -90);
        clock.closePath();
        clock.fillStyle = "#0ff"
        clock.lineWidth = 3
        clock.fill();
        clock.strokeStyle = "purple";
        clock.stroke();
        clock.restore();
        // 分针
        clock.lineWidth = 2
        clock.save();
        clock.translate(250, 250);
        clock.rotate(min * 6 * Math.PI / 180);
        clock.beginPath();
        clock.moveTo(-2, -135);
        clock.lineTo(-5, 20);
        clock.lineTo(5, 20);
        clock.lineTo(2, -135);
        clock.closePath();
        clock.fillStyle = "#0ff"
        clock.lineWidth = 3
        clock.fill();
        clock.strokeStyle = "purple";
        clock.stroke();
        clock.restore();
        // 秒针
        clock.save();
        clock.translate(250, 250);
        clock.rotate(sec * 6 * Math.PI / 180);
        clock.beginPath();
        clock.moveTo(-2, -160);
        clock.lineTo(-3, 30);
        clock.lineTo(3, 30);
        clock.lineTo(2, -160);
        clock.closePath();
        clock.fillStyle = 'red';
        clock.fill();
        clock.strokeStyle = '#00f'
        clock.stroke();
        clock.beginPath();
        clock.arc(0, -145, 3, 0, 360);
        clock.closePath();
        clock.lineWidth = 2;
        clock.stroke();
        clock.restore();
        clock.beginPath();
        clock.arc(250, 250, 5, 0, 360, false);
        clock.strokeStyle = '#fff'
        clock.lineWidth = 5
        clock.stroke();
        clock.closePath();
    }
    setInterval(drawClock, 1000)
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李学软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值