半圓形進度條(vue)

<template>
  <div class="circle-progress">
    <canvas id="canvas" width="150" height="150"></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      canvas:'',
      percent:'',
      ctx:'',
      circleX:'',
      circleY:'',
      radius:'',
      cradius:'',
      lineWidth:'',
      fontSize:'',
      color:'',
      process:'',
      circleLoading:null,
    };
  },
  mounted() {
    this.toCanvas("canvas", "#ffbf00", 80);
  },
  methods: {
    //兩端圓點
    smallcircle1(cx, cy, r) {
      this.ctx.beginPath();
      // this.ctx.moveTo(cx + r, cy);
      this.ctx.lineWidth = 1;
      this.ctx.fillStyle = "#06a8f3";
      this.ctx.arc(cx, cy, r, 0, Math.PI * 2);
      this.ctx.fill();
    },
    smallcircle2(cx, cy, r) {
      this.ctx.beginPath();
      //ctx.moveTo(cx + r, cy);
      this.ctx.lineWidth = 1;
      this.ctx.fillStyle = "#fff";
      this.ctx.arc(cx, cy, r, 0, Math.PI * 2);
      this.ctx.fill();
    },
    //畫圓
    circle(cx, cy, r) {
      this.ctx.beginPath();
      //ctx.moveTo(cx + r, cy);
      this.ctx.lineWidth = this.lineWidth;
      this.ctx.strokeStyle = "#eee";
      this.ctx.arc(cx, cy, r, (Math.PI * 2) / 3, (Math.PI * 1) / 3);
      this.ctx.stroke();
    },
    //畫弧線
    sector(cx, cy, r, startAngle, endAngle, anti) {
      this.ctx.beginPath();
      //ctx.moveTo(cx, cy + r); // 從圓形底部開始畫
      this.ctx.lineWidth = this.lineWidth;
      // 進度條顏色
      this.ctx.strokeStyle = this.color;
      //圓弧兩端的樣式
      this.ctx.lineCap = "round";
      //圓弧
      this.ctx.arc(cx,cy,r,(Math.PI * 2) / 3,(Math.PI * 2) / 3 + (endAngle / 100) * ((Math.PI * 5) / 3),false);
      this.ctx.stroke();
    },
    //刷新
    loading(progress) {
      if (this.process >= this.percent) {
        clearInterval(this.circleLoading);
      }
      //清除canvas內容
      this.ctx.clearRect(0, 0, this.circleX * 2, this.circleY * 2);
      //中間的字
      this.ctx.font = this.fontSize + "px April";
      this.ctx.textAlign = "center";
      this.ctx.textBaseline = "middle";
      this.ctx.fillStyle = "#999";
      this.ctx.fillText(parseFloat(this.process).toFixed(0) + "%", this.circleX, this.circleY);
      //圓形
      this.circle(this.circleX, this.circleY, this.radius);
      //圓弧
      this.sector(this.circleX, this.circleY, this.radius, (Math.PI * 2) / 3, this.process);
      //兩端圓點
      this.smallcircle1(
        this.cradius + Math.cos(((2 * Math.PI) / 360) * 120) * this.radius,
        this.cradius + Math.sin(((2 * Math.PI) / 360) * 120) * this.radius,
        0
      );
      this.smallcircle2(
        this.cradius +
          Math.cos(((2 * Math.PI) / 360) * (120 + this.process * 3)) * this.radius,
        this.cradius +
          Math.sin(((2 * Math.PI) / 360) * (120 + this.process * 3)) * this.radius,
        2
      );
      //控制結束時動畫的速度
      if (this.process / this.percent > 0.9) {
        this.process += 0.3;
      } else if (this.process / this.percent > 0.8) {
        this.process += 0.55;
      } else if (this.process / this.percent > 0.7) {
        this.process += 0.75;
      } else {
        this.process += 1.0;
      }
    },
    /** 生成環形進度條 **/
    toCanvas(id, color, progress) {
      //canvas進度條
      this.canvas = document.getElementById(id);
      this.percent = progress //最終百分比
      this.ctx = canvas.getContext("2d");
      this.circleX = canvas.width / 2; //中心x座標
      this.circleY = canvas.height / 2; //中心y座標
      this.radius = 60; //圓環半徑
      this.cradius = 75; // canvas半徑
      this.lineWidth = 6; //圓形線條的寬度
      this.fontSize = 20; //字體大小
      this.process = 20.0; //進度
      this.color = color;
      this.circleLoading = window.setInterval(() => {
        this.loading(progress);
      }, 20);
    }
  }
};
</script>
<style lang="less">

</style>

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章