canvas实现时钟转动

1、找到canvas的 中心,画出 表心,以及 表款

2、获取当前时间,并根据时间画出 时针,分针,秒针,还有刻度

3、使用定时器,没过一秒 获取新的时间,并重新绘图,达到时钟 转动的效果

1、表心和表框

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body style="text-align: center;">
  <canvas id="canvas" width="600" height="600"></canvas>
</body>
<script src="./index.js"></script>
</html>
// index.js
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')

// 设置中心点,此时300,300变成了坐标0,0
ctx.translate(300, 300)

/* 
// 这里的操作会造成大园于小圆连接
// 画圆线使用arc(中心点X,中心点Y,半径,起始角度,结束角度)
ctx.arc(0, 0, 100, 0, 2*Math.PI)
ctx.arc(0, 0, 5, 0, 2*Math.PI)
// 执行画线段的操作stroke
ctx.stroke() 
*/

// 画大圆
ctx.beginPath()
ctx.arc(0, 0, 100, 0, 2*Math.PI)
ctx.stroke()
ctx.closePath()

// 画小圆
ctx.beginPath()
ctx.arc(0, 0, 5, 0, 2*Math.PI)
ctx.stroke()
ctx.closePath()

2、指针和刻度

const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')

// 设置中心点,此时300,300变成了坐标0,0
ctx.translate(300, 300)

// 把状态保存起来
ctx.save()

/* 
// 这里的操作会造成大园于小圆连接
// 画圆线使用arc(中心点X,中心点Y,半径,起始角度,结束角度)
ctx.arc(0, 0, 100, 0, 2*Math.PI)
ctx.arc(0, 0, 5, 0, 2*Math.PI)
// 执行画线段的操作stroke
ctx.stroke() 
*/

// 画大圆
ctx.beginPath()
ctx.arc(0, 0, 100, 0, 2*Math.PI)
ctx.stroke()
ctx.closePath()

// 画小圆
ctx.beginPath()
ctx.arc(0, 0, 5, 0, 2*Math.PI)
ctx.stroke()
ctx.closePath()

// 获取当前时间
let time = new Date()
let hour = time.getHours() % 12
let min = time.getMinutes()
let sec = time.getSeconds()

// 时针
ctx.rotate(2 * Math.PI / 12 * hour + 2 * Math.PI / 12 * (min / 60) - Math.PI / 2)
ctx.beginPath()
// moveTo设置画线起点
ctx.moveTo(-10, 0)
// lineTo设置画线经过点
ctx.lineTo(40, 0)
// 设置线宽
ctx.lineWidth = 10
ctx.stroke()
ctx.closePath()
// 恢复成上一次save的状态
ctx.restore()
// 恢复完再保存一次
ctx.save()

// 分针
ctx.rotate(2 * Math.PI / 60 * min + 2 * Math.PI / 60 * (sec / 60) - Math.PI / 2)
ctx.beginPath()
ctx.moveTo(-10, 0)
ctx.lineTo(60, 0)
ctx.lineWidth = 5
ctx.strokeStyle = 'blue'
ctx.stroke()
ctx.closePath()
ctx.restore()
ctx.save()

//秒针
ctx.rotate(2 * Math.PI / 60 * sec -  - Math.PI / 2)
ctx.beginPath()
ctx.moveTo(-10, 0)
ctx.lineTo(80, 0)
ctx.strokeStyle = 'red'
ctx.stroke()
ctx.closePath()
ctx.restore()
ctx.save()

// 绘制刻度,也是跟绘制时分秒针一样,只不过刻度是死的
ctx.lineWidth = 1
// 绘制分钟刻度
for (let i = 0; i < 60; i++) {
    ctx.rotate(2 * Math.PI / 60)
    ctx.beginPath()
    ctx.moveTo(90, 0)
    ctx.lineTo(100, 0)
    // ctx.strokeStyle = 'red'
    ctx.stroke()
    ctx.closePath()
}
ctx.restore()
ctx.save()
ctx.lineWidth = 5
// 绘制时钟刻度
for (let i = 0; i < 12; i++) {
    ctx.rotate(2 * Math.PI / 12)
    ctx.beginPath()
    ctx.moveTo(85, 0)
    ctx.lineTo(100, 0)
    ctx.stroke()
    ctx.closePath()
}
ctx.restore()

3、利用定时器让时钟动起来

const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')


function drawClock () {

  // 把状态保存起来
  ctx.save()
  ctx.clearRect(0, 0, 600, 600)
  // 设置中心点,此时300,300变成了坐标0,0
  ctx.translate(300, 300)
  ctx.save()

  /* 
  // 这里的操作会造成大园于小圆连接
  // 画圆线使用arc(中心点X,中心点Y,半径,起始角度,结束角度)
  ctx.arc(0, 0, 100, 0, 2*Math.PI)
  ctx.arc(0, 0, 5, 0, 2*Math.PI)
  // 执行画线段的操作stroke
  ctx.stroke() 
  */

  // 画大圆
  ctx.beginPath()
  ctx.arc(0, 0, 100, 0, 2*Math.PI)
  ctx.stroke()
  ctx.closePath()

  // 画小圆
  ctx.beginPath()
  ctx.arc(0, 0, 5, 0, 2*Math.PI)
  ctx.stroke()
  ctx.closePath()

  // 获取当前时间
  let time = new Date()
  let hour = time.getHours() % 12
  let min = time.getMinutes()
  let sec = time.getSeconds()

  // 时针
  ctx.rotate(2 * Math.PI / 12 * hour + 2 * Math.PI / 12 * (min / 60) - Math.PI / 2)
  ctx.beginPath()
  // moveTo设置画线起点
  ctx.moveTo(-10, 0)
  // lineTo设置画线经过点
  ctx.lineTo(40, 0)
  // 设置线宽
  ctx.lineWidth = 10
  ctx.stroke()
  ctx.closePath()
  // 恢复成上一次save的状态
  ctx.restore()
  // 恢复完再保存一次
  ctx.save()

  // 分针
  ctx.rotate(2 * Math.PI / 60 * min + 2 * Math.PI / 60 * (sec / 60) - Math.PI / 2)
  ctx.beginPath()
  ctx.moveTo(-10, 0)
  ctx.lineTo(60, 0)
  ctx.lineWidth = 5
  ctx.strokeStyle = 'blue'
  ctx.stroke()
  ctx.closePath()
  ctx.restore()
  ctx.save()

  //秒针
  ctx.rotate(2 * Math.PI / 60 * sec -  - Math.PI / 2)
  ctx.beginPath()
  ctx.moveTo(-10, 0)
  ctx.lineTo(80, 0)
  ctx.strokeStyle = 'red'
  ctx.stroke()
  ctx.closePath()
  ctx.restore()
  ctx.save()

  // 绘制刻度,也是跟绘制时分秒针一样,只不过刻度是死的
  ctx.lineWidth = 1
  // 绘制分钟刻度
  for (let i = 0; i < 60; i++) {
      ctx.rotate(2 * Math.PI / 60)
      ctx.beginPath()
      ctx.moveTo(90, 0)
      ctx.lineTo(100, 0)
      // ctx.strokeStyle = 'red'
      ctx.stroke()
      ctx.closePath()
  }
  ctx.restore()
  ctx.save()
  ctx.lineWidth = 5
  // 绘制时钟刻度
  for (let i = 0; i < 12; i++) {
      ctx.rotate(2 * Math.PI / 12)
      ctx.beginPath()
      ctx.moveTo(85, 0)
      ctx.lineTo(100, 0)
      ctx.stroke()
      ctx.closePath()
  }
  ctx.restore()
  ctx.restore()
}

setInterval(() => {
  drawClock()
}, 1000)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值