微信小程序canvas2d繪制圓環(huán)進(jìn)度條組件

1.實(shí)現(xiàn)效果

image.png

2.微信小程序canvas官網(wǎng)

https://developers.weixin.qq.com/miniprogram/dev/component/canvas.html

3.組件介紹

Canvas2D接口(type="2d"),支持同層渲染的一個(gè)圓環(huán)進(jìn)度條。(wx.createCanvasContext已廢棄)


image.png

4.部分代碼

drawCanvasRing() {
      //canvas 2d
      const query = wx.createSelectorQuery().in(this);
      query.select('#myCanvas')
        .fields({ node: true , size: true})
        .exec(this.init.bind(this))
    },
  init(res){
      const canvas = res[0].node
      const ctx = canvas.getContext('2d');
      const dpr = wx.getSystemInfoSync().pixelRatio
      canvas.width = res[0].width * dpr
      canvas.height = res[0].height * dpr
      ctx.scale(dpr, dpr);
      // 大小值的計(jì)算
      var circle_r = this.data.canvasWidth / 2; //畫(huà)布的一半,用來(lái)找中心點(diǎn)和半徑
      var startDegree = this.data.startDegree; //從什么角度開(kāi)始
      var maxValue = this.data.maxValue; //最大值
      var minValue = this.data.minValue; //最小值
      var value = this.data.value; //當(dāng)前的值
      var lineColor = this.data.lineColor; //線(xiàn)條顏色
      var lineWidth = this.data.lineWidth; //線(xiàn)條寬度
      var percent = 360 * ((value - minValue) / (maxValue - minValue)); //計(jì)算結(jié)果

      //定義起始點(diǎn)
      ctx.translate(circle_r, circle_r);
      //灰色圓弧
      ctx.beginPath();
      ctx.strokeStyle="#ebebeb";
      ctx.lineWidth=lineWidth;
      ctx.arc(0, 0, circle_r - 10, 0, 2 * Math.PI, true);
      ctx.stroke();
      ctx.closePath();
      //有色彩的圓弧      
      ctx.beginPath();
      ctx.strokeStyle=lineColor;
      ctx.lineWidth=lineWidth;
      ctx.arc(0, 0, circle_r - 10, startDegree * Math.PI / 180 - 0.5 * Math.PI, percent * Math.PI / 180 + startDegree * Math.PI / 180 - 0.5 * Math.PI, false);
      ctx.stroke();
      ctx.closePath();
    }

5.完整代碼

https://gitee.com/susuhhhhhh/components

6.更多小程序案例

https://gitee.com/susuhhhhhh/wxmini_demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容