canvas写时钟

简单html页面可以使用Live Preview插件预览,简介:Visual Studio Code实时预览插件live preview的安装和使用_yin_CSDN_1007的博客-CSDN博客

首先加入canvas元素 

  <canvas id="clock" width="400" height="400"></canvas>

 js如下

const ctx = document.getElementById('clock').getContext('2d')
const cWidth = ctx.canvas.width, cHeight = ctx.canvas.height;
const r = Math.min(cWidth, cHeight) / 2 // 半径为canvas宽高中较小值的一半
function animate(time) {
  const now = new Date()
  const sec = now.getSeconds(), min = now.getMinutes(),
        // h = now.getHours() < 12 ? now.getHours() : now.getHours() - 12;
        h = now.getHours() % 12;
  ctx.save()
  ctx.clearRect(0, 0, cWidth, cHeight) // 每秒先清空画布
  ctx.translate(cWidth / 2, cHeight / 2) // 将原点从左上角移到中间
  ctx.rotate(-Math.PI / 2) // PI是180度,将水平的x轴逆时针旋转90度,和时钟一致从最12点开始,不旋转则会从3点开始

  ctx.strokeStyle = 'black'
  ctx.lineWidth = r / 32
  ctx.lineCap = 'round'
  ctx.save()
  // 小时刻度
  for(let i = 0; i < 12; i++) {
    ctx.beginPath()
    ctx.rotate(Math.PI / 6) // PI为180度,时针旋转半圈是6个小时,所以除以6。一圈360度12小时,一小时30度
    ctx.moveTo(r/10*7, 0)
    ctx.lineTo(r/10*9, 0)
    ctx.stroke()
  }
  ctx.restore()
  ctx.save()
  // 分钟刻度
  ctx.lineWidth = r / 50
  for(let i = 0; i < 60; i++) {
    ctx.beginPath()
    ctx.rotate(Math.PI / 30) // 分针走180度为30分钟
    ctx.moveTo(r/10*8, 0)
    ctx.lineTo(r/10*9, 0)
    ctx.stroke()
  }
  ctx.restore()
  ctx.save()
  // 每秒时针旋转的度数:时*每小时30度+分*每分钟30÷60度+秒*30÷60÷60度
  ctx.rotate(
    h * Math.PI / 6 + min * Math.PI / (6 * 60) + sec * Math.PI / (6 * 60 * 60)
  )
  ctx.lineWidth = r / 15
  ctx.beginPath()
  ctx.moveTo(-r / 8, 0)
  ctx.lineTo(r/10*5.5, 0)
  ctx.stroke()
  ctx.restore()

  ctx.save()
  // 分针每秒旋转度数:分*180÷30度+秒*6÷60度
  ctx.rotate(
    min * Math.PI / 30 + sec * Math.PI / (30 * 60)
  )
  ctx.lineWidth = r / 20
  ctx.beginPath()
  ctx.moveTo(-r / 5, 0)
  ctx.lineTo(r / 10*6.5, 0)
  ctx.stroke()
  ctx.restore()

  ctx.save()
  // 秒针每秒旋转度数
  ctx.rotate(
    sec * Math.PI / 30
  )
  ctx.strokeStyle = "red"
  ctx.lineWidth = r / 40
  ctx.beginPath()
  ctx.moveTo(-r/10*2, 0)
  ctx.lineTo(r*0.75, 0)
  ctx.stroke()
  ctx.arc(0, 0, r / 20, 0, 2 * Math.PI) // 红色小圆心,参数:圆心x坐标、圆心y坐标,半径,圆弧开始角度,圆弧结束角度
  ctx.fillStyle = 'red'
  ctx.fill()
  ctx.restore()

  ctx.restore()

  window.requestAnimationFrame(animate)
}
window.requestAnimationFrame(animate)

关于requestAnimationFrame看这篇:requestAnimationFrame,读懂这篇文章就够了_前端同学的博客-CSDN博客 

效果如图

 如何优化

从上面代码可以看出每次都要清空整个画布,但是表的刻度其实是不变的,变化的是3个针。可以用两张画布,一张刻度下在,一张指针在上背景透明即可。

优化后续有空再更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然可以!以下是使用 Canvas 绘制一个简单的时钟的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas 时钟</title> <style> #clockCanvas { border: 1px solid #000; } </style> </head> <body> <canvas id="clockCanvas" width="200" height="200"></canvas> <script> function drawClock() { var canvas = document.getElementById('clockCanvas'); var context = canvas.getContext('2d'); var radius = canvas.height / 2; context.translate(radius, radius); radius = radius * 0.9; setInterval(function() { drawFace(context, radius); drawNumbers(context, radius); drawTime(context, radius); }, 1000); } function drawFace(context, radius) { var grad; context.beginPath(); context.arc(0, 0, radius, 0, 2 * Math.PI); context.fillStyle = 'white'; context.fill(); grad = context.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05); grad.addColorStop(0, '#333'); grad.addColorStop(0.5, 'white'); grad.addColorStop(1, '#333'); context.strokeStyle = grad; context.lineWidth = radius * 0.1; context.stroke(); context.beginPath(); context.arc(0, 0, radius * 0.1, 0, 2 * Math.PI); context.fillStyle = '#333'; context.fill(); } function drawNumbers(context, radius) { var angle; var num; context.font = radius * 0.15 + 'px arial'; context.textBaseline = 'middle'; context.textAlign = 'center'; for (num = 1; num < 13; num++) { angle = num * Math.PI / 6; context.rotate(angle); context.translate(0, -radius * 0.85); context.rotate(-angle); context.fillText(num.toString(), 0, 0); context.rotate(angle); context.translate(0, radius * 0.85); context.rotate(-angle); } } function drawTime(context, radius) { var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); // 对时、分、秒进行角度转换 hour = hour % 12; hour = (hour * Math.PI / 6) + (minute * Math.PI / (6 * 60)) + (second * Math.PI / (360 * 60)); drawHand(context, hour, radius * 0.5, radius * 0.07); minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60)); drawHand(context, minute, radius * 0.8, radius * 0.07); second = (second * Math.PI / 30); drawHand(context, second, radius * 0.9, radius * 0.02); } function drawHand(context, pos, length, width) { context.beginPath(); context.lineWidth = width; context.lineCap = 'round'; context.moveTo(0, 0); context.rotate(pos); context.lineTo(0, -length); context.stroke(); context.rotate(-pos); } drawClock(); </script> </body> </html> ``` 你可以将这段代码保存为 HTML 文件并在浏览器中打开,就可以看到一个实时的时钟效果。希望这能帮到你!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值