微信小程序实时圆形进度条实现
WXML代码
<view class="wrap">
<view class="circleBox ">
<canvas class="circle c1" canvas-id="canvasCircle">
</canvas>
<canvas class="circle c2" canvas-id="canvasArcCir">
</canvas>
<view class="drawBtn" bindtap="drawCircle">开始动态绘制</view>
</view>
</view>
WXSS代码
Page {
width: 100%;
height: 100%;
background-color: #fff;
}
.circleBox {
text-align: center;
margin-top: 100rpx;
}
.circle {
position: absolute;
left: 0;
right: 0;
margin: auto;
}
.c1 {
z-index: -99;
width:300rpx;
height:300rpx;
}
.c2 {
width:300rpx;
height:300rpx;
}
.drawBtn {
width: 70rpx;
position: absolute;
top: 60rpx;
right: 0;
left: 0;
margin: 0 auto;
border: 1px #000000 solid;
border-radius: 10rpx;
}
js
//获取应用实例
var app = getApp()
var interval;
var varName;
var ctx = wx.createCanvasContext('canvasArcCir');
Page({
data: {
},
onReady: function () {
//创建并返回绘图上下文context对象。
var cc = wx.createCanvasContext('canvasCircle');
cc.setLineWidth(6);
cc.setStrokeStyle('#eaeaea');
cc.setLineCap('round');
cc.beginPath();
cc.arc(100, 100, 96, 0, 2 * Math.PI, false);
cc.stroke();
cc.draw();
},
onLoad: function (options) {
}
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);
},
})