小程序模板網(wǎng)

微信小程序 實(shí)時(shí)圓形進(jìn)度條實(shí)現(xiàn)

發(fā)布時(shí)間:2018-02-06 15:48 所屬欄目:小程序開發(fā)教程
 

最近為了做一個(gè)錄制按鈕,研究了下小程序的實(shí)時(shí)圓形進(jìn)度條實(shí)現(xiàn)

廢話不多說,先上一張效果圖!

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

建立兩個(gè)canvas標(biāo)簽,先繪制底層的淺灰色圓圈背景,再繪制上層的紅色進(jìn)度條。

WXML代碼

<view class="wrap">
  <view class="circle-box">
    <canvas class="circle" style="width:200px; height:200px;" canvas-id="canvasArcCir">
    </canvas>
    <canvas class="circle" style="z-index: -5; width:200px; height:200px;" canvas-id="canvasCircle">
    </canvas>
    <view class="draw_btn" bindtap="drawCircle">開始動(dòng)態(tài)繪制</view>
  </view>
</view>

WXSS代碼

特別注意:底層的canvas最好使用
z-index:-5;放置于底層

page {
  width: 100%;
  height: 100%;
  background-color: #fff;
}

.circle-box {
  text-align: center;
  margin-top: 10vw;
}

.circle {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
}

.draw_btn {
  width: 35vw;
  position: absolute;
  top: 33vw;
  right: 0;
  left: 0;
  margin: auto;
  border: 1px #000 solid;
  border-radius: 5vw;
}

JS代碼

//獲取應(yīng)用實(shí)例
var app = getApp()

var interval;
var varName;
var ctx = wx.createCanvasContext('canvasArcCir');

Page({
  data: {
  },
  drawCircle: function () {
    clearInterval(varName);
    function drawArc(s, e) {
      ctx.setFillStyle('white');
      ctx.clearRect(0, 0, 200, 200);
      ctx.draw();
      var x = 100, y = 100, radius = 96;
      ctx.setLineWidth(5);
      ctx.setStrokeStyle('#d81e06');
      ctx.setLineCap('round');
      ctx.beginPath();
      ctx.arc(x, y, radius, s, e, false);
      ctx.stroke()
      ctx.draw()
    }
    var step = 1, startAngle = 1.5 * Math.PI, endAngle = 0;
    var animation_interval = 1000, n = 60;
    var animation = function () {
      if (step <= n) {
        endAngle = step * 2 * Math.PI / n + 1.5 * Math.PI;
        drawArc(startAngle, endAngle);
        step++;
      } else {
        clearInterval(varName);
      }
    };
    varName = setInterval(animation, animation_interval);
  },
  onReady: function () {
    //創(chuàng)建并返回繪圖上下文context對(duì)象。
    var cxt_arc = wx.createCanvasContext('canvasCircle');
    cxt_arc.setLineWidth(6);
    cxt_arc.setStrokeStyle('#eaeaea');
    cxt_arc.setLineCap('round');
    cxt_arc.beginPath();
    cxt_arc.arc(100, 100, 96, 0, 2 * Math.PI, false);
    cxt_arc.stroke();
    cxt_arc.draw();
  },
  onLoad: function (options) {

  }
})

注意的要點(diǎn)

1、關(guān)于小程序canvas繪制,請(qǐng)觀看微信小程序官方文檔繪制

2、開始繪制的路徑可以根據(jù) JS代碼中的變量startAngle 來選擇從哪里開始繪制

最后有什么問題可以留言,大家一起討論共同進(jìn)步~~



本文地址:http://22321a.com/wxmini/doc/course/21640.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢