当前位置:网站首页>[wechat applet development (VI)] draw the circular progress bar of the music player
[wechat applet development (VI)] draw the circular progress bar of the music player
2022-07-28 02:48:00 【Sock is a dog】
Demand background
We need to develop functions similar to Netease cloud player , Here we mainly explain the problem of the circular progress bar
The following figure is the final online rendering 
List of articles
One 、 Wechat applet does not support svg
Example :pandas Is based on NumPy A tool of , The tool is created to solve data analysis tasks .
Two 、 Use canvas
1. Here is the code for drawing the circular progress bar
The code is as follows ( Example ):
drawCanvasCircleByStep() {
// When the background player pauses, it will stop drawing
if (isNaN(wx.$globalData.backgroundAudioManager.duration)) {
return;
}
// Full of pictures this.data.step 2 among this.data.innerAudioContextStatus Memory controls audio playback status 1 Playing
if(this.data.step >= 2 || this.data.innerAudioContextStatus != '1') {
return;
}
// Here we also control the rotation of the middle picture , In this way, you can control the rotation pause of the progress bar and the picture in the same method
this.data.ratateDeg = this.data.ratateDeg + 0.2;
this.setData({
ratateDeg: this.data.ratateDeg
})
// 2 multiply Audio playback progress ratio
this.data.step = 2 * ((wx.$globalData.backgroundAudioManager.currentTime)/(wx.$globalData.backgroundAudioManager.duration));
// console.log(this.data.step)
if (!this.data.secondCanvas) {
// Some models do not support multiple creation So only for the first time getContext
const query = wx.createSelectorQuery()
query.select('#canvasProgressbg2')
.fields({
node: true, size: true })
.exec((res) => {
this.data.secondCanvas = res[0].node
const ctx = this.data.secondCanvas.getContext('2d')
const dpr = wx.getSystemInfoSync().pixelRatio
this.data.secondCanvas.width = 500 * dpr
this.data.secondCanvas.height = 500 * dpr
ctx.scale(dpr, dpr)
// Empty canvas before painting
ctx.clearRect(0, 0, this.data.secondCanvas.width, this.data.secondCanvas.height)
ctx.lineWidth = 6;// Set the width of the ring
var gradient = ctx.createLinearGradient(500, 250, 250, 500);
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // Set the color of the ring
ctx.lineCap = '#A9B9D4' // Set the shape of the end of the ring
ctx.beginPath();// Start a new path
ctx.arc(250, 250, 484/2, -Math.PI / 2, this.data.step * Math.PI - Math.PI / 2);
ctx.stroke();
ctx.closePath();
this.data.secondCtx = ctx;
// Use requestAnimationFrame Recursive drawing
this.data.requestAnimationFrame = this.data.secondCanvas.requestAnimationFrame(this.drawCanvasCircleByStep)
})
} else {
const ctx = this.data.secondCtx
// Empty canvas before painting
ctx.clearRect(0, 0, this.data.secondCanvas.width, this.data.secondCanvas.height)
ctx.lineWidth = 6;// Set the width of the ring
var gradient = ctx.createLinearGradient(500, 250, 250, 500);
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // Set the color of the ring
ctx.lineCap = 'round' // Set the shape of the end of the ring
ctx.beginPath();// Start a new path
ctx.arc(250, 250, 484/2, -Math.PI / 2, this.data.step * Math.PI - Math.PI / 2);
ctx.stroke();
ctx.closePath();
this.data.requestAnimationFrame = this.data.secondCanvas.requestAnimationFrame(this.drawCanvasCircleByStep)
}
},
2. Some Android models do not support gradients
The code is as follows ( Example ):
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // Set the color of the ring Just change it to the pure color value here
3. Deal with the timer in the page stay onUnload Middle clearance , Wechat applet has memory leak
onUnload:function(){
if (this.timeupdateOnBackgourndAudio) {
clearInterval(this.timeupdateOnBackgourndAudio)
}
// Empathy setTimeout Clear use clearTimeout(timer);
},
边栏推荐
- Canonical Address
- Explore flex basis
- The virtual host website cannot access the self-test method
- Hardware standard
- [TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass
- Why is there no unified quotation for third-party testing fees of software products?
- Find - block search
- 【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码
- Wechat campus bathroom reservation applet graduation design finished product (3) background function
- 【微信小程序开发(五)】接口按照根据开发版体验版正式版智能配置
猜你喜欢

How MySQL uses indexes (glory Collection Edition)

Four methods of modifying MySQL password (suitable for beginners)

初识C语言 -- 结构体,分支和循环语句

Is the interface that can be seen everywhere in the program really useful? Is it really right?
![[self growth website collection]](/img/42/fa17c9167697543defd3e63a97237a.png)
[self growth website collection]

Chapter III queue

Wechat campus bathroom reservation applet graduation design finished product (3) background function

MySQL's way to solve deadlock - lock analysis of common SQL statements

unordered_ The hash function of map and the storage mode of hash bucket

作业7.27 IO进程
随机推荐
Typescript (zero) -- introduction, environment construction, first instance
JS event loop synchronous task, asynchronous task (micro task, macro task) problem analysis
P6118 [JOI 2019 Final]珍しい都市 题解
unity中物体碰撞反弹(学习)
树的孩子兄弟表示法
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
Common SQL statement query
1313_ Pyserial installation and document generation
C # introducing WinAPI to pass the character set of Chinese string parameters
Please, don't use the command line to configure MySQL master-slave replication. Isn't it fragrant to deploy with urlos interface?
Newline required at end of file but not found.
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code
QT implementation disable shortcut key
IO flow: node flow and processing flow are summarized in detail.
Is the interface that can be seen everywhere in the program really useful? Is it really right?
【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码
怎么简单实现菜单拖拽排序的功能
Find - block search
Chapter III queue
PS simple to use