微信小程序之圓形進度條

需求概要

小程序中使用圓形倒計時,效果圖:
clipboard.pnghtml

思路

  1. 使用2個canvas 一個是背景圓環,一個是彩色圓環。
  2. 使用setInterval 讓彩色圓環逐步繪製。

解決方案

第一步先寫結構

一個盒子包裹2個canvas以及文字盒子;
盒子使用相對定位做爲父級,flex佈局,設置居中;
一個canvas,使用絕對定位做爲背景,canvas-id="canvasProgressbg"
另外一個canvas,使用相對定位做爲進度條,canvas-id="canvasProgress"
代碼以下:canvas

// wxml
 <view class="container">
     <view class='progress_box'>
        <canvas class="progress_bg"   canvas-id="canvasProgressbg">  </canvas> 
        <canvas class="progress_canvas"   canvas-id="canvasProgress">  </canvas> 
        <view class="progress_text">
            <view class="progress_dot"></view> 
            <text class='progress_info'> {{progress_txt}}</text>
        </view>     
    </view>
</view>
// wxss
.progress_box{
  position: relative;
  width:220px;
  height: 220px;  
// 這裏的寬高是必須大於等於canvas圓環的直徑 不然繪製到盒子外面就看不見了
// 一開始設置 width:440rpx; height:440rpx; 發現 在360X640分辨率的設備,下繪製的圓環跑盒子外去了
// 小程序使用rpx單位適配 ,可是canvas繪製的是px單位的。因此只能用px單位繪製的圓環在盒子內顯示
  display: flex;  
  align-items: center;
  justify-content: center;
  background-color: #eee;
}
.progress_bg{
  position: absolute;
    width:220px;
  height: 220px; 
}
.progress_canvas{ 
  width:220px;
  height: 220px; 
} 
.progress_text{ 
  position: absolute; 
  display: flex;  
  align-items: center;
  justify-content: center
}
.progress_info{   
  font-size: 36rpx;
  padding-left: 16rpx;
  letter-spacing: 2rpx
} 
.progress_dot{
  width:16rpx;
  height: 16rpx;  
  border-radius: 50%;
  background-color: #fb9126;
}

第二步數據綁定

從wxml中能夠看到咱們使用了一個數據progress_txt,因此在js中設置data以下:小程序

data: {
    progress_txt: '正在匹配中...',  
  },

第三步canvas繪製

敲黑板,劃重點segmentfault

1. 先繪製背景

  1. 在js中封裝一個畫圓環的函數drawProgressbg,canvas 畫圓
  2. 在onReady中執行這個函數;

小程序canvas組件與H5的canvas有點差異,請查看文檔,代碼以下微信小程序

drawProgressbg: function(){
    // 使用 wx.createContext 獲取繪圖上下文 context
    var ctx = wx.createCanvasContext('canvasProgressbg',this)
    ctx.setLineWidth(4);// 設置圓環的寬度
    ctx.setStrokeStyle('#20183b'); // 設置圓環的顏色
    ctx.setLineCap('round') // 設置圓環端點的形狀
    ctx.beginPath();//開始一個新的路徑
    ctx.arc(110, 110, 100, 0, 2 * Math.PI, false);
    //設置一個原點(110,110),半徑爲100的圓的路徑到當前路徑
    ctx.stroke();//對當前路徑進行描邊
    ctx.draw();
  },
 onReady: function () {
    this.drawProgressbg(); 
  },

看一下效果以下:
clipboard.png微信

2. 繪製彩色圓環

  1. 在js中封裝一個畫圓環的函數drawCircle,
  2. 在onReady中執行這個函數;
drawCircle: function (step){  
    var context = wx.createCanvasContext('canvasProgress',this);
      // 設置漸變
      var gradient = context.createLinearGradient(200, 100, 100, 200);
      gradient.addColorStop("0", "#2661DD");
      gradient.addColorStop("0.5", "#40ED94");
      gradient.addColorStop("1.0", "#5956CC");
      
      context.setLineWidth(10);
      context.setStrokeStyle(gradient);
      context.setLineCap('round')
      context.beginPath(); 
      // 參數step 爲繪製的圓環周長,從0到2爲一週 。 -Math.PI / 2 將起始角設在12點鐘位置 ,結束角 經過改變 step 的值肯定
      context.arc(110, 110, 100, -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
      context.stroke(); 
      context.draw() 
  },
 onReady: function () {
     this.drawProgressbg(); 
     this.drawCircle(2) 
  },
this.drawCircle(0.5) 效果以下: this.drawCircle(1) 效果以下: this.drawCircle(2) 效果以下:
clipboard.png clipboard.png clipboard.png

3. 設置一個定時器

  1. 在js中的data設置一個計數器 count,一個步驟step,一個定時器
  2. 在js中封裝一個定時器的函數countInterval,
  3. 在onReady中執行這個函數;
data: {
    progress_txt: '正在匹配中...',  
    count:0, // 設置 計數器 初始爲0
    countTimer: null // 設置 定時器 初始爲null
  },
    countInterval: function () {
    // 設置倒計時 定時器 每100毫秒執行一次,計數器count+1 ,耗時6秒繪一圈
    this.countTimer = setInterval(() => {
      if (this.data.count <= 60) {
        /* 繪製彩色圓環進度條  
        注意此處 傳參 step 取值範圍是0到2,
        因此 計數器 最大值 60 對應 2 作處理,計數器count=60的時候step=2
        */
         this.drawCircle(this.data.count / (60/2))
        this.data.count++;
      } else {
        this.setData({
          progress_txt: "匹配成功"
        }); 
        clearInterval(this.countTimer);
      }
    }, 100)
  },
 onReady: function () {
    this.drawProgressbg();
    // this.drawCircle(2) 
    this.countInterval()
  },

最終效果
圖片描述xss

微信小程序之圓形進度條(自定義組件)函數

因爲版本庫升級作對應的更新(20190716)

wx.createCanvasContext(id) 改成:wx.createCanvasContext(id,this);佈局

相關文章
相關標籤/搜索