canvas 实例之闹钟

canvas实例闹钟

    let canvas = document.getElementById('canvas');
    let ctx = canvas.getContext('2d');
    let x = canvas.width / 2, //
        y = canvas.height / 2;
    function draw() {
        ctx.translate(x, y); //将画布原点移到中心  默认画布原点在左上角
        ctx.save();
        //画大圆盘
        ctx.beginPath();
        ctx.arc(0, 0, 200, 0, Math.PI * 2);
        ctx.strokeStyle = 'blue';
        ctx.lineWidth = '10';
        ctx.stroke();
        ctx.restore();

        //画小时数
        ctx.save();
        let hourNum = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];
        ctx.font = '18px Arial';
        ctx.strokeStyle = '#333';
        ctx.textAlign = 'center';//水平对齐方式
        ctx.textBaseline = 'middle';//垂直方向上水平居中
        hourNum.forEach((item, index) => {
            let rad = Math.PI * 2 / 12 * index;
            let x = Math.cos(rad) * 160;
            let y = Math.sin(rad) * 160;
            ctx.fillText(item, x, y);
        })
     
        //画小数点
        for (let i = 0; i < 60; i++) {
            let rad = Math.PI * 2 / 60 * i;
            let x = Math.cos(rad) * 180,
                y = Math.sin(rad) * 180;
            if (i % 5 == 0) {
                //小时对应的点
                ctx.beginPath();
                ctx.arc(x, y, 6, 0, Math.PI * 2);
                ctx.fillStyle = 'green'
                ctx.fill();
            }
            else {
                ctx.beginPath();
                ctx.arc(x, y, 3, 0, Math.PI * 2);
                ctx.fillStyle = 'red';
                ctx.fill();
            }
        }
        ctx.restore();
    }
    function drawDot() {
        ctx.save();
        ctx.beginPath(0,0);
        ctx.arc(0, 0, 10, 0, Math.PI * 2);
        // ctx.fillStyle = 'red';
        ctx.fill();
        ctx.restore();
    }
    function drawHour(h,m) {
        ctx.save();
        ctx.beginPath(0, 0);
        let rad=Math.PI*2/12*(h+m/60);
        ctx.rotate(rad);//旋转的角度必须写在画线之前
        ctx.moveTo(0, 20);
        ctx.lineTo(0, -100);
        ctx.strokeStyle = 'green';
        ctx.lineWidth = '3'
        ctx.stroke();
        ctx.restore();
    }
    function drawMinute(m) {
        ctx.save();
        ctx.beginPath(0, 0);
        let rad=Math.PI*2/60*m;
        ctx.rotate(rad);
        ctx.moveTo(0, 20);
        ctx.lineTo(0, -120);
        ctx.strokeStyle = 'blue';
        ctx.lineWidth = '2'
        ctx.stroke();
        ctx.restore();
    }
    function drawSecond(s) {
        ctx.save();
        ctx.beginPath();
        let rad=Math.PI*2/60*s;
        ctx.rotate(rad);
        ctx.moveTo(0, 20);
        ctx.lineTo(0, -140);
        ctx.strokeStyle = 'red';
        ctx.lineWidth = '1'
        ctx.stroke();
        ctx.restore();
    }
    let timer = setInterval(() => {
        ctx.save();
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        let date = new Date();
        let h = date.getHours(),
            m = date.getMinutes(),
            s = date.getSeconds();
        draw();
        drawDot();
        drawHour(h,m);
        drawMinute(m);
        drawSecond(s);
        ctx.restore();
    }, 1000)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

哆啦咪唏

看到这里了,不留下点什么吗

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

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

打赏作者

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

抵扣说明:

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

余额充值