canvas-实现钟表

50 篇文章 1 订阅

canvas-实现钟表 目录


前言

  • 模拟时钟的运行

推荐阅读

思路解析

  1. 主题有2种:表盘,表针
  2. 绘制表针
  3. 绘制表盘
  4. 运行

代码实现

class Click{
    constructor(canvas){
        this.c = document.getElementsByTagName(canvas)[0];
        this.c.width = 300;
        this.c.height = 300;
        this.ctx = this.c.getContext('2d');
        console.log(this.ctx);
    }

    GameStart(){
        let that = this;
        requestAnimationFrame(function step(){                     
            console.log(that.ctx);
            // console.log(this.ctx);            
            
            that.drawDial(that.ctx);
            that.drawAllHands(that.ctx);
            requestAnimationFrame(step);
        });
    }

    drawDial(ctx){
        let pi = Math.PI;
        ctx.clearRect(0,0,300,300);
        ctx.save();
        // 圆心
        ctx.translate(150,150);
        ctx.arc(0,0,148,0,Math.PI*2);
        ctx.stroke();
        ctx.closePath();

        for(let i = 0; i < 60; i++){
           ctx.save();
            // 一周是2pi,分成60份,就是pi/30
            ctx.rotate(-pi/2 + i*pi/30);

            ctx.beginPath();
            ctx.moveTo(110,0);
            ctx.lineTo(140,0);
            
            ctx.lineWidth = i % 5 ? 2 : 4;
            ctx.strokeStyle = i % 5 ? "blue" : "red";

            ctx.stroke();
            ctx.closePath();
            ctx.restore();
        }

        ctx.restore();

    }

    drawAllHands(){
        let time = new Date();

        let hour = time.getHours(),
            minute = time.getMinutes(),
            second = time.getSeconds();

        let pi = Math.PI;
        let secondAngle = pi / 180 * 6 * second;
        let minuteAngle = pi / 180 * 6 * minute + secondAngle / 60;
        let hourAngle = pi / 180 * 30 * hour + minuteAngle / 12;

        this.drawHand(hourAngle,60,6,"red",this.ctx);
        this.drawHand(minuteAngle,106,4,"green",this.ctx);
        this.drawHand(secondAngle,129,2,"blue",this.ctx);
    }

    drawHand(angle, length, width, color, ctx){
        ctx.save();

        ctx.translate(150,150);
        ctx.rotate(-Math.PI / 2 + angle);
        
        ctx.beginPath();
        ctx.moveTo(-4,0);
        ctx.lineTo(length,0);
        ctx.lineWidth = width;
        ctx.strokeStyle = color;
        ctx.lineCap = "round";

        ctx.stroke();
        ctx.closePath();

        ctx.restore();
    }
}
let click = new Click('canvas');
click.GameStart();

结果观测

在这里插入图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值