使用canvas 绘制半圆形进度条

 使用canvas 绘制半圆形进度条

样式:半圆形的虚线,虚线大小可调整

<template>
<!-- 绘制半圆形进度条 -->
<div class="progress_item">
   <canvas class="progress_ring" type="2d" ref="bgline"></canvas>
   <canvas class="progress_draw" type="2d" ref="drawLine"></canvas>
</div>
</template>


<script>
export default {
  name: 'CircleBar',
  props: {
    percent: {
      type: Number,
      default: 0
    },
    color: {
      type: String,
      default: '#5B9EFF'
    }
  },
  methods: {
    drawProgress() {
   // 1、背景 半圆背景虚线底
   this.drawCircle('bgline')
    // 2、选中半圆虚线
    this.drawCircle('drawLine', this.percent, this.color)
  },
  drawCircle(id, percent = 2, color='#EDEDED') {
    const radius = 120; // 半径
    // 圆心坐标
    const x = 160;
    const y = 140;
    // const Canvas = document.getElementById(id);
    const Canvas = this.$refs[id];
   // 1、背景 半圆背景虚线底
   const Ctx = Canvas.getContext('2d');
    Ctx.clearRect(0, 0, 2*x, y);
    Ctx.beginPath()
    Ctx.strokeStyle = color
    Ctx.lineWidth = 18;
    Ctx.lineCap = "line";
    Ctx.setLineDash([6, 12]);
    Ctx.arc(x,y,radius,Math.PI, percent*Math.PI);
    Ctx.stroke();
  }
},
mounted() {
  this.$nextTick(() => {
    this.drawProgress(this.percent);
  })
  }

}
</script>


<style lang="scss" scoped>
.progress_item{
  width:260px;
  position: relative;
  height: 100%;
}

.progress_ring, .progress_draw{
  position: absolute;
  left:100px;
  transform: translateX(-50%);
  z-index: 3;
  top:0;
  width:200px;
  height: 100px;
}
// .progress_move{position: absolute;left:50%;transform: translateX(-50%);z-index: 5;top:-12px;width:300px;height: 170px;}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值