canvas绘制圆环动画

<!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>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    html, body{
      width: 100%;
      height: 100%;
    }
    body{
      background-size: 100% 180%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  </style>
</head>
<body>
  <canvas id="can" width="500" height="400"></canvas>

  <script>
    // 1 获取画布
    /** @type {HTMLCanvasElement} */
    let canvas = document.getElementById('can')
    // 2 获取上下文
    let cxt = canvas.getContext('2d') // 获取canvas上下文绘制环境 类似绘制图形的工具
    const constant = Math.PI/180
    // 3 绘制
    /* 
      startArc 开始角度
      roateArc 需要旋转的角度
      lineWidth 圆环宽度
      color 圆环颜色
     */
    let fun = function (startArc = 0, roateArc = 0, lineWidth = 6, color = 'white', speed = 1){
      let angle = 0 // 每次旋转的角度
      let origin = 0 // 每次的起点
      let oldangle = 1 // 每次旋转的角度
      let oldorigin = 0 // 每次的起点
      let switchBlean = true
      let a = 1
      let time = setInterval(()=>{
        if(switchBlean){
          canvas.width = canvas.width
          cxt.beginPath()
          cxt.lineWidth = lineWidth
          cxt.strokeStyle = color
          cxt.arc(200,200,100,constant * (origin * roateArc + startArc), constant * (angle + startArc))
          cxt.stroke()
          angle += speed
          if(parseInt(angle / roateArc) -1 === origin){
            origin++
            switchBlean = false
          }
        }else{
          // 改变起始距离 终点只改一次
          if(a == 1){
            console.log(oldorigin + startArc)
            console.log(oldangle * roateArc + startArc)
            a++
          }
          canvas.width = canvas.width
          cxt.beginPath()
          cxt.lineWidth = lineWidth
          cxt.strokeStyle = color
          // cxt.globalAlpha = 0
          cxt.arc(200,200,100, constant * (oldorigin + startArc) , constant * (oldangle * roateArc + startArc))
          cxt.stroke()
          oldorigin+=3
          if( parseInt(oldorigin / roateArc) === oldangle ){ 
            oldangle++
            switchBlean = true
          }
        }
      },20)
    }
    
    fun(0, 360, 30, 'pink', 2)
  </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值