canvas 画时钟

1 在html中 添加 canvas 标签

    <canvas id="canvas" width="800" height="800"></canvas>

2 获取画笔

/**@type {HTMLCanvasElement} */
     let canvas = document.getElementById("canvas");
     let ctx = canvas.getContext("2d");

3,绘制表盘

			ctx.save();
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.translate(canvas.width / 2, canvas.height / 2);
            ctx.beginPath();
            ctx.lineWidth = 15;
            // 表盘
            ctx.rotate(-Math.PI / 2);
            ctx.arc(0, 0, 200, 0, 2 * Math.PI);
            ctx.stroke();

4 绘制刻度

// 刻度
            ctx.save();
            for (let index = 1; index <= 60; index++) {
                ctx.beginPath();
                ctx.rotate(Math.PI / 30);
                ctx.strokeStyle = "black";
                ctx.lineCap = "round";
                ctx.lineWidth = 5;
                if (index % 5 != 0) {
                    ctx.moveTo(190, 0);
                    ctx.lineTo(185, 0);
                } else {
                    ctx.strokeStyle = "red";
                    ctx.lineWidth = 8;
                    ctx.moveTo(190, 0);
                    ctx.lineTo(180, 0);
                    // ;
                }
                ctx.stroke();
                ctx.closePath();
            }
            ctx.restore();

5,标记时间数字 参考数字调正


			ctx.save();
            ctx.rotate(Math.PI / 2);
            var clockRadius = 250;
            ctx.font = '24px Arial';
            ctx.fillStyle = '#000';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle'
            for (let n = 1; n <= 12; n++) {
                let theta = (n - 3) * (Math.PI * 2) / 12;
                var x = clockRadius * 0.65 * Math.cos(theta);
                var y = clockRadius * 0.65 * Math.sin(theta);
                ctx.fillText(n, x, y);
            }
            ctx.restore();

园的路径 默认是顺时针

在这里插入图片描述

6,弧度计算

            ctx.rotate(Math.PI / 2);
            // 时针

            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            hours = hours > 12 ? hours - 12 : hours;
            var hour = hours + minutes / 60;
            var minute = minutes + seconds / 60;



            var theat = (hour - 3) * (2 * Math.PI) / 12;
            ctx.save();

            ctx.rotate(theat);
            ctx.beginPath();

            ctx.moveTo(-15, -4);
            ctx.lineTo(-15, 4);
            ctx.lineTo(clockRadius * 0.45, 1);
            ctx.lineTo(clockRadius * 0.45, -1);
            ctx.fill();

            ctx.restore();

            var theta = (minute - 15) * 2 * Math.PI / 60;
            // 分针
            ctx.save();

            ctx.rotate(theta);
            ctx.beginPath();
            ctx.fillStyle = "green";
            ctx.moveTo(-20, -4);
            ctx.lineTo(-20, 4);
            ctx.lineTo(clockRadius * 0.55, 1);
            ctx.lineTo(clockRadius * 0.55, -1);
            ctx.fill();
            ctx.restore();


            //秒针
            var theta = (seconds - 15) * 2 * Math.PI / 60;
            ctx.save();

            ctx.rotate(theta);
            ctx.beginPath();
            ctx.fillStyle = "blue";
            ctx.moveTo(-25, -4);
            ctx.lineTo(-25, 4);
            ctx.lineTo(clockRadius * 0.7, 1);
            ctx.lineTo(clockRadius * 0.7, -1);
            ctx.fill();
            ctx.restore();
            //中心圆点
            ctx.save();
            ctx.beginPath();
            ctx.fillStyle = "#D40000";
            ctx.arc(0, 0, 8, 0, Math.PI * 2, true);
            ctx.fill();
            ctx.restore();
            ctx.restore();
        };
 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值