欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解JavaScript+Canvas繪制環(huán)形進(jìn)度條

 更新時間:2022年02月11日 15:41:19   作者:聆聽  
canvas可以在頁面中設(shè)定一個區(qū)域,再利用JavaScript動態(tài)地繪制圖像。本文將利用canvas繪制一個可以動的環(huán)形進(jìn)度條。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動手試一試

效果圖

思考

移動端的場景里經(jīng)常會出現(xiàn)環(huán)形進(jìn)度條的功能,在實(shí)現(xiàn)這個功能前,我預(yù)想的解決方案大致有: echarts、antv、canvas、svg

前面兩種第三方提供的解決方案當(dāng)然是簡單,拿到案例修整一下即可,但是需要下載依賴,而且代碼量不小。有沒有不需要依賴第三方包,采用原生的寫法,獨(dú)立封裝成一個組件,降低耦合,而且性能優(yōu)越?

當(dāng)然,那就主要介紹canvas的使用

實(shí)現(xiàn)思路

可以展示整個圓、半圓以及任意角度弧形(左右對稱)的進(jìn)度條。整體思路如下:

1.先確定展示的形狀,是整個圓、半圓還是一般的弧形

2.把確定好形狀的圓弧均分100等份,計算出每一份所占的弧度

3.灰色圓弧占100份,紅色圓弧最終占的份數(shù)由參數(shù)確定

4.設(shè)置setInterval定時器,重復(fù)執(zhí)行畫圖操作

  • 清空畫布
  • 先畫灰色的圓弧,占100份
  • 再畫紅色的圓弧:紅色圓弧的份數(shù)從0開始,每次加1
  • 畫紅色圓弧末端的紅色圓:難點(diǎn)在于根據(jù)角度確定紅色圓的圓心,這里面涉及到三角函數(shù),在草稿紙上畫個圖就大致明白了
  • 當(dāng)紅色圓弧的份數(shù)達(dá)到指定值(傳的參數(shù))的時候,清除定時器

具體代碼實(shí)現(xiàn)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  <title>Canvas</title>
</head>
<body>
  <canvas id="canvas" width="300" height="300"></canvas>

  <script>
    draw(66);
    /**
     * [順時針方向畫圖,起始點(diǎn)在左側(cè)]
     * @param  {[number]} percent [所占的進(jìn)度百分比,比如66%,則傳66即可,0 <= percent <= 100]
     * @param  {[number]} sR      [圓弧起始角度,可不傳,默認(rèn)是π/2,Math.PI/2 <= sR < 3/2 * Math.PI]
     */
    function draw(percent, sR) {
      if (percent < 0 || percent > 100) {
        return;
      }
      if (sR < Math.PI/2 || sR >= 3/2 * Math.PI) {
        return;
      }

      var canvas = document.querySelector('#canvas'),
          cxt = canvas.getContext('2d'),
          cWidth = canvas.width,
          cHeight = canvas.height,
          baseColor = '#e1e1e1',
          coverColor = '#fe4d43',
          PI = Math.PI,
          sR = sR || 1/2 * PI; // 默認(rèn)圓弧的起始點(diǎn)弧度為π/2

      var finalRadian = sR + ((PI + (PI - sR) * 2) * percent / 100); // 紅圈的終點(diǎn)弧度
      var step = (PI + (PI - sR) * 2)/100; // 一個1%對應(yīng)的弧度大小
      var text = 0; // 顯示的數(shù)字
      var timer = setInterval(function() {
        cxt.clearRect(0, 0, cWidth, cHeight);
        var endRadian =  sR + text * step;
        // 畫灰色圓弧
        drawCanvas(cWidth/2, cHeight/2, 80, sR, sR + (PI + (PI - sR) * 2), baseColor, 2);
        // 畫紅色圓弧
        drawCanvas(cWidth/2, cHeight/2, 80, sR, endRadian, coverColor, 2);

        // 畫紅色圓頭
        // 紅色圓頭其實(shí)就是一個圓,關(guān)鍵的是找到其圓心,涉及到三角函數(shù)知識,自己畫個圖一看就明了
        var angle = 2*PI - endRadian; // 轉(zhuǎn)換成逆時針方向的弧度(三角函數(shù)中的)
        xPos = Math.cos(angle) * 80 + cWidth/2; // 紅色圓 圓心的x坐標(biāo)
        yPos = -Math.sin(angle) * 80 + cHeight/2; // 紅色圓 圓心的y坐標(biāo)
        drawCanvas(xPos, yPos, 2, 0, 2*PI, coverColor, 2);

        // 數(shù)字
        cxt.fillStyle = coverColor;
        cxt.font = '40px PT Sans';
        var textWidth = cxt.measureText(text+'%').width;
        cxt.fillText(text+'%', cWidth/2 - textWidth/2, cHeight/2 + 15);
        text++;

        if (endRadian.toFixed(2) >= finalRadian.toFixed(2)) {
          clearInterval(timer);
        }
      }, 30);

      function drawCanvas(x,y,r,sRadian,eRadian,color,lineWidth) {
        cxt.beginPath();
        cxt.lineCap = "round";
        cxt.strokeStyle = color;
        cxt.lineWidth = lineWidth;
        cxt.arc(x, y, r, sRadian, eRadian, false);
        cxt.stroke();
      }
    }
  </script>
</body>
</html>

關(guān)于動畫部分,可以使用requestAnimationFrame做優(yōu)化,函數(shù)改寫如下:

function draw(percent, sR) {
  if (percent < 0 || percent > 100) {
    return;
  }
  if (sR < Math.PI/2 || sR >= 3/2 * Math.PI) {
    return;
  }

  var canvas = document.querySelector('#canvas'),
      cxt = canvas.getContext('2d'),
      cWidth = canvas.width,
      cHeight = canvas.height,
      baseColor = '#e1e1e1',
      coverColor = '#fe4d43',
      PI = Math.PI,
      sR = sR || 1/2 * PI; // 默認(rèn)圓弧的起始點(diǎn)弧度為π/2

  var finalRadian = sR + ((PI + (PI - sR) * 2) * percent / 100); // 紅圈的終點(diǎn)弧度
  var step = (PI + (PI - sR) * 2)/100; // 一個1%對應(yīng)的弧度大小
  var text = 0; // 顯示的數(shù)字

  window.requestAnimationFrame(paint);
  function paint() {
    cxt.clearRect(0, 0, cWidth, cHeight);
    var endRadian =  sR + text * step;
    // 畫灰色圓弧
    drawCanvas(cWidth/2, cHeight/2, 80, sR, sR + (PI + (PI - sR) * 2), baseColor, 2);
    // 畫紅色圓弧
    drawCanvas(cWidth/2, cHeight/2, 80, sR, endRadian, coverColor, 2);

    // 畫紅色圓頭
    // 紅色圓頭其實(shí)就是一個圓,關(guān)鍵的是找到其圓心,涉及到三角函數(shù)知識,自己畫個圖一看就明了
    var angle = 2*PI - endRadian; // 轉(zhuǎn)換成逆時針方向的弧度(三角函數(shù)中的)
    xPos = Math.cos(angle) * 80 + cWidth/2; // 紅色圓 圓心的x坐標(biāo)
    yPos = -Math.sin(angle) * 80 + cHeight/2; // 紅色圓 圓心的y坐標(biāo)
    drawCanvas(xPos, yPos, 2, 0, 2*PI, coverColor, 2);

    // 數(shù)字
    cxt.fillStyle = coverColor;
    cxt.font = '40px PT Sans';
    var textWidth = cxt.measureText(text+'%').width;
    cxt.fillText(text+'%', cWidth/2 - textWidth/2, cHeight/2 + 15);
    text++;

    if (endRadian.toFixed(2) < finalRadian.toFixed(2)) {
      window.requestAnimationFrame(paint);
    }
  }

  function drawCanvas(x,y,r,sRadian,eRadian,color,lineWidth) {
    cxt.beginPath();
    cxt.lineCap = "round";
    cxt.strokeStyle = color;
    cxt.lineWidth = lineWidth;
    cxt.arc(x, y, r, sRadian, eRadian, false);
    cxt.stroke();
  }

到此這篇關(guān)于詳解JavaScript+Canvas繪制環(huán)形進(jìn)度條的文章就介紹到這了,更多相關(guān)JavaScript Canvas環(huán)形進(jìn)度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論