使用canvas繪製圓形進度條

lesixing發表於2018-03-28

實現步驟:

  • 繪製一個圓;
  • 繪製圓環;
  • 繪製進度環;
  • 繪製文字;

一、建立畫布

<canvas id='myCanvas' width='200' height='200'></canvas>
複製程式碼

二、繪製一個圓

var canvas = document.getElementById('myCanvas1');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI, false);
ctx.strokeStyle = 'red'; 
ctx.stroke();
複製程式碼

顯示效果

三、繪製圓環:

var canvas = document.getElementById('myCanvas1');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI, false);

// new added
ctx.lineWidth = 15;

ctx.strokeStyle = 'red'; 
ctx.stroke();
複製程式碼

顯示效果

四、繪製進度環

var canvas = document.getElementById('myCanvas1');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI, false);
ctx.lineWidth = 15;
ctx.strokeStyle = 'red'; 
ctx.stroke();

//new added
var startAngle = 3 / 2 * Math.PI; //開始位置弧度
var percentage = 10;  //完成進度值 
var diffAngle = percentage / 100 * Math.PI * 2; //完成進度弧度值
ctx.beginPath();
ctx.arc(100, 100, 50, startAngle, diffAngle + startAngle, false);
ctx.strokeStyle = 'green';
ctx.stroke();
複製程式碼

顯示效果

五、繪製文字

var canvas = document.getElementById('myCanvas1');
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(100, 100, 50, 0, 2 * Math.PI, false);
ctx.lineWidth = 15;
ctx.strokeStyle = 'red'; 
ctx.stroke();

var startAngle = 3 / 2 * Math.PI; //開始位置弧度
var percentage = 10;  //完成進度值 
var diffAngle = percentage / 100 * Math.PI * 2; //完成進度弧度值
ctx.beginPath();
ctx.arc(100, 100, 50, startAngle, diffAngle + startAngle, false);
ctx.strokeStyle = 'green';
ctx.stroke();

//new added
ctx.fillStyle = '#000';
ctx.textAlign = 'center';
ctx.font = '16px serif';
ctx.fillText(percentage + '%', 100+2, 100+5);
複製程式碼

顯示效果

六、完整程式碼

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(factory);// AMD
  } else if (typeof exports === 'object' && typeof module !== 'undefined') {
    module.exports = factory(); // CommonJS
  } else {
    window.Progressbar = factory(); // Browser globals
  }
}(this, function () {
  function Progressbar(options){
    this.id = options.id;
    this.value = options.value || 0;
    this.width = options.width || 200;
    this.height = options.height || 200;
    this.bgColor = options.bgColor || 'green';
    this.barColor = options.barColor || 'red';
    this.fontColor = options.fontColor || '#000';
    if(options.init){
      this.init();
    }
  }
  Progressbar.prototype.init = function(){
    var canvas = document.getElementById(this.id);

    if(!canvas.getContext) {
      throw new Error('your browser does not support canvas')
    }

    if(!this.id){
      throw new Error('your need pass id ')
    }

    var width = parseInt(this.width);
    var height = parseInt(this.height);
    canvas.setAttribute('width',width);
    canvas.setAttribute('height',height);

    var ctx = canvas.getContext("2d");

    var startAngle = 3 / 2 * Math.PI;
    var percentage = 0;
    var diffAngle = 0;
    var cx = width / 2;
    var cy = height / 2;
    var timer = setInterval(draw, 50);
    var value = this.value;
    var bgColor = this.bgColor;
    var barColor = this.barColor;
    var fontColor = this.fontColor;

    function draw(){
      diffAngle = (percentage / 100) * Math.PI * 2;

      //清除矩形區域
      ctx.clearRect(0, 0, width, height);

      ctx.beginPath();

      //設定線段寬度
      ctx.lineWidth = 15;

      //繪製圓
      ctx.arc(cx, cy, 50, 0, 2 * Math.PI, false);

      //設定填充色
      ctx.fillStyle = '#FFF';
      ctx.fill();

      //繪製圖形輪廓
      ctx.strokeStyle = bgColor; 
      ctx.stroke();

      //繪製百分比進度
      ctx.beginPath();
      ctx.arc(cx, cy, 50, startAngle, diffAngle + startAngle, false);
      ctx.strokeStyle = barColor;
      ctx.stroke();

      //繪製百分比文字
      ctx.fillStyle = fontColor;
      ctx.textAlign = 'center';
      ctx.font = '16px serif';
      ctx.fillText(percentage + '%', cx, cy + 6);

      if (percentage >= value) {
        clearTimeout(timer);
      }

      percentage++;
    }
  }
  return Progressbar;
}));

var progressbar1 = new Progressbar({ id: 'myCanvas1',value: 20 ,init: true })
var progressbar2 = new Progressbar({ id: 'myCanvas2',value: 30 ,init: true })
複製程式碼

檢視效果 進度條

參考資料 使用canvas來繪製圖形

原文

相關文章