微信小程序 - 实时圆形进度条

Mar7929 8年前
   <h2>最近为了做一个录制按钮,研究了下小程序的实时圆形进度条实现</h2>    <p>废话不多说,先上一张效果图!</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/292301867723c7d74df95385a7d837d0.png"></p>    <p>初始状态</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c760ba7823b7af9d967c17cd28cc4477.png"></p>    <p>点击中间按钮开始绘制</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/68138db2bf889354706cf24f2d5a8c3a.png"></p>    <p>绘制过程</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d298502a64b5d6af5efe367d25cc9020.png"></p>    <p>绘制结束</p>    <h2>实现思路</h2>    <p>建立两个canvas标签,先绘制底层的浅灰色圆圈背景,再绘制上层的红色进度条。</p>    <h2>WXML代码</h2>    <pre>  <code class="language-javascript"><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">开始动态绘制</view>    </view>  </view></code></pre>    <h2>WXSS代码</h2>    <p>特别注意:底层的canvas最好使用</p>    <p>z-index:-5; 放置于底层</p>    <pre>  <code class="language-javascript">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;  }</code></pre>    <h2>JS代码</h2>    <pre>  <code class="language-javascript">//获取应用实例  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 () {      //创建并返回绘图上下文context对象。      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) {      }  })</code></pre>    <h2>注意的要点</h2>    <p>1、关于小程序canvas绘制,请观看 <a href="/misc/goto?guid=4959738644013356704" rel="nofollow,noindex">微信小程序官方文档绘制</a></p>    <p>2、开始绘制的路径可以根据 <em>JS代码中的变量startAngle</em> 来选择从哪里开始绘制</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/8dfb90fddfa0922e82e1ac8ba97bb139.png"></p>    <p>最后有什么问题可以留言,大家一起讨论共同进步~~</p>    <p> </p>    <p>来自:http://www.jianshu.com/p/eab547396163</p>    <p> </p>