canvas实现简单时钟

最近有canvas的业务需求,就学习了一下canvas,交一下作业

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas1" width="800" height="600">
        你的浏览器不支持canvas
    </canvas>

    <script>
        let Can = document.querySelector('#canvas1');
        // console.log(Can);
        let ctx = Can.getContext('2d');
        // console.log(ctx);



        function renderClock() {
            // 每次清除画布
            ctx.clearRect(0, 0, 800, 600);


            // 保留初始的状态
            ctx.save();

            // 将坐标移到画布中心
            ctx.translate(400, 300);
            //画小时数
            const hourArray = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];

            const MPI = Math.PI;

            hourArray.forEach(function (num, i) {
                let rad = 2 * MPI / 12 * i;
                let x = Math.cos(rad) * 160;
                let y = Math.sin(rad) * 160;
                ctx.font = "18px sans-serif";
                ctx.textAlign = "center";
                ctx.textBaseline = "middle";
                ctx.fill();
                ctx.fillText(num, x, y);

            });


            let time = new Date();
            // 如果时间大于12就是下午减去12
            let hour = time.getHours() > 12 ? time.getHours() - 12 : time.getHours();
            let min = time.getMinutes();
            let sec = time.getSeconds();

            ctx.font = "18px sans-serif";
            ctx.textAlign = "center";
            ctx.textBaseline = "middle";
            ctx.fillStyle = '#FF0000';
            ctx.fill();
            ctx.fillText(`${hour < 10 ? 0 : ''}${hour}:${min < 10 ? 0 : ''}${min}:${sec < 10 ? 0 : ''}${sec}`, 0, 100);
            console.log(`${hour < 10 ? 0 : ''}${hour}:${min < 10 ? 0 : ''}${min}:${sec < 10 ? 0 : ''}${sec}`);
            ctx.rect(-40, 85, 80, 30);
            ctx.strokeStyle = "#FF0000";
            ctx.stroke();


            ctx.rotate(-2 * MPI / 4);
            ctx.save();


            // 绘制表盘
            ctx.beginPath();
            ctx.arc(0, 0, 200, 0, 2 * MPI);
            ctx.strokeStyle = '#999';
            ctx.lineWidth = 5;
            ctx.stroke();
            ctx.closePath();


            // 绘制时钟刻度
            for (let i = 0; i < 12; i++) {
                ctx.beginPath();
                ctx.rotate(MPI / 6);
                ctx.moveTo(185, 0);
                ctx.lineTo(200, 0);
                ctx.strokeStyle = '#999';
                ctx.lineWidth = 10;
                // ctx.fillText(`${i + 1}`, 160, 5);
                ctx.stroke();
                ctx.closePath();

            };

            ctx.restore();
            ctx.save();

            // 绘制秒针刻度
            for (let j = 0; j < 60; j++) {
                ctx.beginPath();
                ctx.rotate(MPI / 30);
                ctx.moveTo(190, 0);
                ctx.lineTo(200, 0);
                ctx.strokeStyle = '#999';
                ctx.lineWidth = 2;
                ctx.stroke()
                ctx.closePath();

            }
            ctx.restore();
            ctx.save();




            // 绘制秒针
            ctx.beginPath();
            // 根据秒针的时间旋转
            ctx.rotate(2 * MPI / 60 * sec)
            ctx.moveTo(-30, 0);
            ctx.lineTo(170, 0);
            ctx.strokeStyle = 'red';
            ctx.lineWidth = 2;
            ctx.stroke();
            ctx.closePath();

            ctx.restore();
            ctx.save();



            // 绘制分针
            ctx.beginPath();
            // 根据分针的时间旋转
            ctx.rotate(2 * MPI / 60 * min + 2 * MPI / 3600 * sec)
            ctx.moveTo(-10, 0);
            ctx.lineTo(150, 0);
            ctx.strokeStyle = '#999';
            ctx.lineWidth = 3;
            ctx.stroke();
            ctx.closePath();

            ctx.restore();
            ctx.save();




            // 绘制时针
            ctx.beginPath();
            // 根据时针的时间旋转
            ctx.rotate(2 * MPI / 12 * hour + 2 * MPI / 60 / 12 * min + 2 * MPI / 12 / 60 / 60 * sec)
            ctx.moveTo(-10, 0);
            ctx.lineTo(100, 0);
            ctx.strokeStyle = '#999';
            ctx.lineWidth = 3;
            ctx.stroke();
            ctx.closePath();

            ctx.restore();
            ctx.save();




            // h绘制中心点
            ctx.beginPath();
            ctx.arc(0, 0, 8, 0, 2 * MPI);
            ctx.fillStyle = "red";
            ctx.fill();
            ctx.closePath();



            // 回退初始状态
            ctx.restore();
            ctx.restore();

        }


        setInterval(function () {
            renderClock();
        }, 1000);


    </script>
</body>

</html>

效果如下,可以复制粘贴代码直接运行

 一个前端小白的日常

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值