canvas 绘制时钟

class Clock {
      constructor() {
        this.canvas = document.querySelector('#canvas')
        this.ctx = this.canvas.getContext('2d')
        this.margin = 30
        this.fontSize = 20
        this.center = {
          x: canvas.width / 2,
          y: canvas.height / 2
        }
        this.init()
      }
      init() {
        this.canvas.addEventListener('mousemove',function(e){
          let can = this.canvas.getBoundingClintRect()
          console.log(e.pageX,e.pageY,'e',can)
        })
      }
      run() {
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
        this.drawCircle()
        this.drawNums()
        this.drawCenter()
        this.drawSecond()
        this.minutes()
        this.hour()
      }
      drawCircle() {
        this.ctx.beginPath()
        this.ctx.arc(this.center.x, this.center.y, this.canvas.width / 2 - this.margin, 0, 2 * Math.PI)
        this.ctx.stroke()
      }
      drawNums() {
        let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        let r = this.canvas.width / 2 - this.margin - this.fontSize
        let angle = Math.PI * 2 / 12
        arr.forEach(item => {
          this.ctx.beginPath()
          this.ctx.font = `${this.fontSize}px Arial`
          let textWith = this.ctx.measureText(item).width
          let x = Math.sin(angle * item) * r + this.canvas.width / 2 - textWith / 2
          let y = this.canvas.height / 2 - Math.cos(angle * item) * r + this.fontSize / 2

          this.ctx.fillText(item, x, y)
        })
      }
      drawCenter() {
        this.ctx.beginPath()
        this.ctx.arc(this.canvas.width / 2, this.canvas.height / 2, 5, 0, 2 * Math.PI)
        this.ctx.fill();
      }
      drawSecond() {
        this.ctx.beginPath()
        this.ctx.moveTo(this.center.x, this.center.y)
        let date = new Date()
        let time = date.getSeconds()
        let angle = Math.PI * 2 / 60
        let r = this.canvas.width / 2 - this.margin - this.fontSize - this.margin
        let x = Math.sin(angle * time) * r + this.canvas.width / 2
        let y = this.canvas.height / 2 - Math.cos(angle * time) * r
        this.ctx.lineTo(x, y)
        this.ctx.stroke()
      }
      minutes() {
        this.ctx.beginPath()
        this.ctx.moveTo(this.center.x, this.center.y)
        let date = new Date()
        let time = date.getMinutes()
        let angle = Math.PI * 2 / 60
        let r = this.canvas.width / 2 - this.margin - this.fontSize - this.margin * 2
        let x = Math.sin(angle * time) * r + this.canvas.width / 2
        let y = this.canvas.height / 2 - Math.cos(angle * time) * r

        this.ctx.lineTo(x, y)
        this.ctx.stroke()
      }
      hour() {
        this.ctx.beginPath()
        this.ctx.moveTo(this.center.x, this.center.y)
        let date = new Date()
        let time = date.getHours()
        let angle = Math.PI * 2 / 12
        let r = this.canvas.width / 2 - this.margin - this.fontSize - this.margin * 3
        let x = Math.sin(angle * time) * r + this.canvas.width / 2
        let y = this.canvas.height / 2 - Math.cos(angle * time) * r
        this.ctx.lineTo(x, y)
        this.ctx.stroke()
      }
    }

    let clock = new Clock()
     setInterval(() => {
        clock.run()
      }, 1000)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值