微信小程序之圓形進(jìn)度條實(shí)現(xiàn)思路
需求概要
小程序中使用圓形倒計(jì)時(shí),效果圖:

思路
- 使用2個(gè)canvas 一個(gè)是背景圓環(huán),一個(gè)是彩色圓環(huán)。
- 使用setInterval 讓彩色圓環(huán)逐步繪制。
解決方案
第一步先寫結(jié)構(gòu)
一個(gè)盒子包裹2個(gè)canvas以及文字盒子;
盒子使用相對(duì)定位作為父級(jí),flex布局,設(shè)置居中;
一個(gè)canvas,使用絕對(duì)定位作為背景,canvas-id="canvasProgressbg"
另一個(gè)canvas,使用相對(duì)定位作為進(jìn)度條,canvas-id="canvasProgress"
代碼如下:
// 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圓環(huán)的直徑 否則繪制到盒子外面就看不見了
// 一開始設(shè)置 width:440rpx; height:440rpx; 發(fā)現(xiàn) 在360X640分辨率的設(shè)備,下繪制的圓環(huán)跑盒子外去了
// 小程序使用rpx單位適配 ,但是canvas繪制的是px單位的。所以只能用px單位繪制的圓環(huán)在盒子內(nèi)顯示
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;
}
第二步數(shù)據(jù)綁定
從wxml中可以看到我們使用了一個(gè)數(shù)據(jù)progress_txt,所以在js中設(shè)置data如下:
data: {
progress_txt: '正在匹配中...',
},
第三步canvas繪制
敲黑板,劃重點(diǎn)
1. 先繪制背景
- 在js中封裝一個(gè)畫圓環(huán)的函數(shù)drawProgressbg,canvas 畫圓
- 在onReady中執(zhí)行這個(gè)函數(shù);
小程序canvas組件與H5的canvas有點(diǎn)差別,請(qǐng)查看文檔,代碼如下
drawProgressbg: function(){
// 使用 wx.createContext 獲取繪圖上下文 context
var ctx = wx.createCanvasContext('canvasProgressbg')
ctx.setLineWidth(4);// 設(shè)置圓環(huán)的寬度
ctx.setStrokeStyle('#20183b'); // 設(shè)置圓環(huán)的顏色
ctx.setLineCap('round') // 設(shè)置圓環(huán)端點(diǎn)的形狀
ctx.beginPath();//開始一個(gè)新的路徑
ctx.arc(110, 110, 100, 0, 2 * Math.PI, false);
//設(shè)置一個(gè)原點(diǎn)(100,100),半徑為90的圓的路徑到當(dāng)前路徑
ctx.stroke();//對(duì)當(dāng)前路徑進(jìn)行描邊
ctx.draw();
},
onReady: function () {
this.drawProgressbg();
},
看一下效果如下:

2. 繪制彩色圓環(huán)
- 在js中封裝一個(gè)畫圓環(huán)的函數(shù)drawCircle,
- 在onReady中執(zhí)行這個(gè)函數(shù);
drawCircle: function (step){
var context = wx.createCanvasContext('canvasProgress');
// 設(shè)置漸變
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();
// 參數(shù)step 為繪制的圓環(huán)周長(zhǎng),從0到2為一周 。 -Math.PI / 2 將起始角設(shè)在12點(diǎn)鐘位置 ,結(jié)束角 通過改變 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) 效果如下:

3. 設(shè)置一個(gè)定時(shí)器
在js中的data設(shè)置一個(gè)計(jì)數(shù)器 count,一個(gè)步驟step,一個(gè)定時(shí)器
在js中封裝一個(gè)定時(shí)器的函數(shù)countInterval,
在onReady中執(zhí)行這個(gè)函數(shù);
data: {
progress_txt: '正在匹配中...',
count:0, // 設(shè)置 計(jì)數(shù)器 初始為0
countTimer: null // 設(shè)置 定時(shí)器 初始為null
},
countInterval: function () {
// 設(shè)置倒計(jì)時(shí) 定時(shí)器 每100毫秒執(zhí)行一次,計(jì)數(shù)器count+1 ,耗時(shí)6秒繪一圈
this.countTimer = setInterval(() => {
if (this.data.count <= 60) {
/* 繪制彩色圓環(huán)進(jìn)度條
注意此處 傳參 step 取值范圍是0到2,
所以 計(jì)數(shù)器 最大值 60 對(duì)應(yīng) 2 做處理,計(jì)數(shù)器count=60的時(shí)候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()
},
最終效果

總結(jié)
以上所述是小編給大家介紹的微信小程序之圓形進(jìn)度條實(shí)現(xiàn)思路,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 詳解微信小程序——自定義圓形進(jìn)度條
- 微信小程序?qū)崿F(xiàn)下載進(jìn)度條的方法
- 微信小程序?qū)崿F(xiàn)時(shí)間進(jìn)度條功能
- 微信小程序?qū)崿F(xiàn)實(shí)時(shí)圓形進(jìn)度條的方法示例
- 微信小程序多音頻播放進(jìn)度條問題
- 微信小程序自定義組件實(shí)現(xiàn)環(huán)形進(jìn)度條
- 微信小程序自定義音樂進(jìn)度條的實(shí)例代碼
- 微信小程序?qū)崿F(xiàn)圓形進(jìn)度條動(dòng)畫
- 微信小程序繪制半圓(弧形)進(jìn)度條
- 微信小程序?qū)崿F(xiàn)圓心進(jìn)度條
相關(guān)文章
JavaScript ES6中export、import與export default的用法和區(qū)別
這篇文章主要給大家介紹了JavaScript ES6中export、import與export default的用法和區(qū)別,文中介紹的非常詳細(xì),相信對(duì)大家學(xué)習(xí)ES6會(huì)有一定的幫助,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03
微信小程序自定義tab實(shí)現(xiàn)多層tab嵌套
這篇文章主要為大家詳細(xì)介紹了微信小程序自定義tab實(shí)現(xiàn)多層tab嵌套,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
小程序?qū)崿F(xiàn)抽獎(jiǎng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)抽獎(jiǎng)動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
javascript用戶注冊(cè)提示效果的簡(jiǎn)單實(shí)例
這個(gè)可以增加用戶驗(yàn)證,不用js alert來作提示,而是在右邊提示,現(xiàn)在很多網(wǎng)站都這樣做,有需要的朋友可以參考一下2013-08-08
通過js動(dòng)態(tài)創(chuàng)建標(biāo)簽,并設(shè)置屬性方法
下面小編就為大家分享一篇通過js動(dòng)態(tài)創(chuàng)建標(biāo)簽,并設(shè)置屬性方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
jquery.rotate.js實(shí)現(xiàn)可選抽獎(jiǎng)次數(shù)和中獎(jiǎng)內(nèi)容的轉(zhuǎn)盤抽獎(jiǎng)代碼
這篇文章主要介紹了jquery.rotate.js實(shí)現(xiàn)可選抽獎(jiǎng)次數(shù)和中獎(jiǎng)內(nèi)容的轉(zhuǎn)盤抽獎(jiǎng)代碼,需要的朋友可以參考下2017-08-08

