vue中cavans实现圆环百分比

项目中要求画一个支持任意百分比的圆环进度条逆时针,带圆角如下

找过好几篇文章,有用原生css但是画不出来圆角,也有用svg的,但由于要兼容ie所以使用了cavans画布实现,如下

开始他是从左边中心点开始所以要让他转到上面走个-90度

结束的度数计算纯粹靠运气,没搞明白他的角度计算方式

 // 推荐率画布
    circleImg (percent) {
      console.log('执行了画布', percent)
      var _this = this
      var canvas = document.getElementById('circle')
      var ctx = canvas.getContext('2d')
      /* 开始圆环 */
      var circleObj = {
        ctx: ctx,
        /* 圆心 */
        x: 80,
        y: 78,
        /* 半径 */
        radius: 55,
        /* 环的宽度 */
        lineWidth: 12
      }
      /* 有色的圆环 */
      /* 开始的度数-从上一个结束的位置开始 */
      circleObj.startAngle = -Math.PI * 2 * 90 / 360
      /* 结束的度数 */
      if (percent == 100) {
        circleObj.endAngle = Math.PI * 2 * (100 / 100 - 0.25)
      } else {
        circleObj.endAngle = Math.PI * 2 * (100 - percent / 100 - 0.25)
      }
      var linearGradient = ctx.createLinearGradient(0, 0, 180, 0) // 创建渐变对象  渐变开始点和渐变结束点
      linearGradient.addColorStop(0, '#00DCA6') // 添加颜色点
      linearGradient.addColorStop(0.5, '#00ED97') // 添加颜色点
      linearGradient.addColorStop(1, '#00F791') // 添加颜色点
      circleObj.color = linearGradient
      _this.drawCircle(circleObj)
    },
    /* 画曲线 */
    drawCircle (circleObj) {
      var ctx = circleObj.ctx
      ctx.beginPath()
      ctx.arc(circleObj.x, circleObj.y, circleObj.radius, circleObj.startAngle, circleObj.endAngle, true)
      // 设定曲线粗细度
      ctx.lineWidth = circleObj.lineWidth
      // 给曲线着色
      ctx.strokeStyle = circleObj.color
      ctx.fillStyle = '#0000ff'
      // 连接处样式
      ctx.lineCap = 'round'
      // 阴影的x偏移
      ctx.shadowOffsetX = 0
      // 阴影的y偏移
      ctx.shadowOffsetY = 6
      // 阴影颜色
      ctx.shadowColor = 'rgba(0,204,255,0.25)'
      // 阴影的模糊半径
      ctx.shadowBlur = 16
      // 给环着色
      ctx.stroke()
      ctx.closePath()
    }

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值