使用canvas简单绘制时钟

html部分:

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

js部分:

class Clock {
        constructor(){
          this.canvas = document.getElementById('canvas');
          this.ctx = this.canvas.getContext('2d');
          
        }
        init(){
          this.date = new Date();
          this.hour = this.date.getHours() > 12 ? this.date.getHours() - 12 : this.date.getHours();
          this.min = this.date.getMinutes();
          this.sec = this.date.getSeconds();
          this.renderClockFace();  
          return setTimeout(() => {
            return this.init();
          }, 1000);
          
        }

        renderClockFace(){
          this.ctx.clearRect(0,0,800,600)
          this.ctx.save();
          this.ctx.translate(400,300);
          this.ctx.beginPath();
          this.ctx.strokeStyle = 'grey';
          this.ctx.lineWidth = 10;
          this.ctx.arc(0,0,200,0,2*Math.PI);
          this.ctx.stroke();
          this.ctx.closePath();
          this.ctx.save();
          return this.renderMinutePoint();
        }

        renderMinutePoint(){
          for(let i = 0; i < 60; i++){
            this.ctx.beginPath();
            this.ctx.rotate(2 * Math.PI / 60);
            this.ctx.moveTo(180,0);
            this.ctx.lineWidth = 2;
            this.ctx.strokeStyle = 'black';
            this.ctx.lineTo(190,0);
            this.ctx.stroke();
            this.ctx.closePath();
          }
          this.ctx.restore();
          this.ctx.save();
          return this.renderHourPoint();
        }

        renderHourPoint(){
          for(let i = 0; i < 12; i++){
            this.ctx.beginPath();
            this.ctx.rotate(2 * Math.PI / 12);
            this.ctx.moveTo(180,0);
            this.ctx.lineWidth = 10;
            this.ctx.lineTo(200,0);
            this.ctx.stroke();
            this.ctx.closePath();
          }
          this.ctx.restore();
          this.ctx.save();
          return this.renderSecondHand()
        }

        renderSecondHand(){
          this.ctx.rotate(- 2 * Math.PI / 4);
          this.ctx.save();
          this.ctx.beginPath();
          this.ctx.rotate(2 * Math.PI / 60 * this.sec);
          this.ctx.lineWidth = 2;
          this.ctx.strokeStyle = 'black';
          this.ctx.moveTo(-30,0);
          this.ctx.lineTo(170,0);
          this.ctx.stroke();
          this.ctx.closePath();
          this.ctx.restore();
          this.ctx.save();
          return this.renderMinuteHand();
        }

        renderMinuteHand(){
          this.ctx.beginPath();
          this.ctx.rotate(2 * Math.PI / 60 * this.min + 2 * Math.PI / 3600 * this.sec)
          this.ctx.moveTo(-20,0);
          this.ctx.lineTo(160,0);
          this.ctx.lineWidth = 4;
          this.ctx.strokeStyle = 'black';
          this.ctx.stroke();
          this.ctx.closePath();
          this.ctx.restore();
          this.ctx.save();
          return this.renderHourHand();
        }

        renderHourHand(){
          this.ctx.beginPath();
          this.ctx.rotate(2 * Math.PI / 12 * this.hour + 2 * Math.PI / 60 / 12 * this.min + 2 * Math.PI / 3600 / 12 * this.sec)
          this.ctx.moveTo(-10,0);
          this.ctx.lineTo(140,0);
          this.ctx.lineWidth = 10;
          this.ctx.strokeStyle = 'black'
          this.ctx.stroke();
          this.ctx.closePath();
          this.ctx.restore();
          this.ctx.save();
          return this.renderClockNumber();  
        }

        renderClockNumber(){
          this.ctx.restore();
          this.ctx.restore();
          let arr = [3,4,5,6,7,8,9,10,11,12,1,2,3]
          for(let i = 0; i < 12; i++){
            let rad = 2 * Math.PI / 12 * i;
            let x = Math.cos(rad) * 160
            let y = Math.sin(rad) * 160
            this.ctx.beginPath();
            this.ctx.moveTo(180,0);
            this.ctx.font = "20px Verdana"
            x = arr[i] >= 11 ? x - 7 : x;
            this.ctx.fillText(arr[i],x-5,y+8);
            this.ctx.closePath();
          }
          return this.renderClockCenter();
        }

        renderClockCenter(){
          this.ctx.beginPath();
          this.ctx.arc(0,0,10,0,2*Math.PI)
          this.ctx.fillStyle = 'pink'
          this.ctx.fill();
          this.ctx.closePath();
          this.ctx.restore();
        }
      }

      new Clock().init();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值