随着小程序的热度下降,更多的人已经开始静下心来开发微信小程序了,最近在开发一款微信小程序的过程中遇到了一个问题:如何用微信小程序实现一个圆形的进度条?整理一番后,记录下来与大家共享。
代码实现的思路原理:
建立两个canvas标签,先绘制底层的浅灰色圆圈背景,再绘制上层的红色进度条。
.wxml的代码如下:
<view> <view> <canvas> </canvas> <canvas> </canvas> <view>开始动态绘制</view> </view></view>
WXSS代码
特别注意:底层的canvas最好使用。
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代码如下:
//获取应用实例
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 <p>注意的要点</p><p>1、关于小程序canvas绘制,请观看 微信小程序官方文档绘制<br></p><p>2、开始绘制的路径可以根据 JS代码中的变量startAngle 来选择从哪里开始绘制</p><p><img src="https://img.php.cn//upload/image/934/236/150/1488175220826712.png?x-oss-process=image/resize,p_40" title="用微信小程序实现一个圆形的进度条" alt="用微信小程序实现一个圆形的进度条" style="max-width:90%" style="max-width:90%"></p><p>圆形进度条的效果图如下:</p><p><img src="https://img.php.cn//upload/image/472/445/339/1488175234976995.png?x-oss-process=image/resize,p_40" title="用微信小程序实现一个圆形的进度条" alt="用微信小程序实现一个圆形的进度条" style="max-width:90%" style="max-width:90%"></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill11997" title="微信小程序隐私权限调试"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dbae7c507.jpg" alt="微信小程序隐私权限调试" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill11997" title="微信小程序隐私权限调试" class="overflowclass">微信小程序隐私权限调试</a>
<p class="overflowclass">一站式排查小程序隐私授权报错(errno 104 / buttonId is wrong 等)+ 标准修改模式。适用:隐私协议配置、隐私弹窗不弹、授权报错、隐私接口调用失败。已抽离具体项目,适用于任何微信小程序。</p>
</div>
<a rel="nofollow" href="/xiazai/skill11997" title="微信小程序隐私权限调试" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p>初始状态</p><p><img src="https://img.php.cn//upload/image/388/999/370/1488175252339641.png?x-oss-process=image/resize,p_40" title="用微信小程序实现一个圆形的进度条" alt="用微信小程序实现一个圆形的进度条" style="max-width:90%" style="max-width:90%"></p><p>点击中间按钮开始绘制</p><p><img src="https://img.php.cn//upload/image/823/274/468/1488175266473348.png?x-oss-process=image/resize,p_40" title="用微信小程序实现一个圆形的进度条" alt="用微信小程序实现一个圆形的进度条" style="max-width:90%" style="max-width:90%"></p><p>绘制过程</p><p><img src="https://img.php.cn//upload/image/684/724/886/1488175281877063.png?x-oss-process=image/resize,p_40" title="用微信小程序实现一个圆形的进度条" alt="用微信小程序实现一个圆形的进度条" style="max-width:90%" style="max-width:90%"></p><p>绘制结束</p><p>更多用微信小程序实现一个圆形的进度条相关文章请关注PHP中文网!</p><p><br></p>










