当前位置:网站首页>【微信小程序开发(六)】绘制音乐播放器环形进度条
【微信小程序开发(六)】绘制音乐播放器环形进度条
2022-07-28 01:39:00 【袜子是一只狗】
需求背景
需要开发类似于网易云播放器的功能,这里主要讲解环形进度条的问题
下图是最终上线的效果图
一、微信小程序不支持svg
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、使用canvas
1.这里主要放绘制环形进度条的代码
代码如下(示例):
drawCanvasCircleByStep() {
// 背景播放器暂停进度结束就不画了
if (isNaN(wx.$globalData.backgroundAudioManager.duration)) {
return;
}
// 画满是 this.data.step 2 其中 this.data.innerAudioContextStatus 内存控制音频播放状态 1 正在播放
if(this.data.step >= 2 || this.data.innerAudioContextStatus != '1') {
return;
}
// 这里同时控制中间图片的旋转,这样可以在同一个方法中控制进度条和图片的旋转暂停
this.data.ratateDeg = this.data.ratateDeg + 0.2;
this.setData({
ratateDeg: this.data.ratateDeg
})
// 2 乘以 音频播放进度比例
this.data.step = 2 * ((wx.$globalData.backgroundAudioManager.currentTime)/(wx.$globalData.backgroundAudioManager.duration));
// console.log(this.data.step)
if (!this.data.secondCanvas) {
// 部分机型不支持多次创建 所以只有第一次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)
// 绘制前清空画布
ctx.clearRect(0, 0, this.data.secondCanvas.width, this.data.secondCanvas.height)
ctx.lineWidth = 6;// 设置圆环的宽度
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; // 设置圆环的颜色
ctx.lineCap = '#A9B9D4' // 设置圆环端点的形状
ctx.beginPath();//开始一个新的路径
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;
// 使用 requestAnimationFrame 递归绘制
this.data.requestAnimationFrame = this.data.secondCanvas.requestAnimationFrame(this.drawCanvasCircleByStep)
})
} else {
const ctx = this.data.secondCtx
// 绘制前清空画布
ctx.clearRect(0, 0, this.data.secondCanvas.width, this.data.secondCanvas.height)
ctx.lineWidth = 6;// 设置圆环的宽度
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; // 设置圆环的颜色
ctx.lineCap = 'round' // 设置圆环端点的形状
ctx.beginPath();//开始一个新的路径
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. 部分机型安卓不支持渐变
代码如下(示例):
gradient.addColorStop("0", "rgba(191, 204, 222, 1)");
gradient.addColorStop("1", "rgba(104, 129, 183, 1)");
ctx.strokeStyle = gradient; // 设置圆环的颜色 这里直接改为纯色值就好了
3. 处理好页面中的定时器 在onUnload中清除,微信小程序有内存泄漏
onUnload:function(){
if (this.timeupdateOnBackgourndAudio) {
clearInterval(this.timeupdateOnBackgourndAudio)
}
// 同理setTimeout 清除使用 clearTimeout(timer);
},
边栏推荐
- 【HCIP】BGP 特性
- Read Plato & nbsp; Eplato of farm and the reasons for its high premium
- Leetcode judge whether palindrome number
- [in depth study of 4g/5g/6g topic -42]: urllc-14 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -8-low delay technology-2-slot based scheduling and
- Representation of children and brothers of trees
- 第三章 队列
- Digital empowerment and innovation in the future: hese eredi appears at the 5th Digital China Construction Summit
- Pycharm 快速给整页全部相同名称修改的快捷键
- First knowledge of C language -- structure, branch and loop statements
- MySQL is shown in the figure. The existing tables a and B need to be associated with a and B tables through projectcode to find idcardnum with different addresses.
猜你喜欢

LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines

基于stm32的恒功率无线充电

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

ERD online 4.0.0 free private deployment scheme

Wechat campus bathroom reservation applet graduation design finished product (1) development outline

Three core issues of concurrent programming (glory Collection Edition)

【软件测试】—— 自动化测试之unittest框架

Is the interface that can be seen everywhere in the program really useful? Is it really right?

1313_pyserial的安装以及文档的生成

【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
随机推荐
selenium+pytest+allure综合练习
Wechat campus bathroom reservation applet graduation design finished product (2) applet function
MySQL blocking monitoring script
Common SQL statement query
Learn this trick and never be afraid to let the code collapse by mistake
2020.7.7 eth price analysis
unordered_map的hash function及hash bucket存储方式探索
MySQL 中的 INSERT 是怎么加锁的?(荣耀典藏版)
unity中物体碰撞反弹(学习)
【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码
MySQL锁系列之锁算法详解(荣耀典藏版)
数字孪生农业丨智慧农业稻米加工厂从“看天吃饭”到“知天而作”
分层图解决的一些最短路问题
New infrastructure helps the transformation and development of intelligent road transportation
新基建助力智能化道路交通领域的转型发展
Necessary knowledge points of the original group
Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice
Email security report in the second quarter: email attacks have soared fourfold, and well-known brands have been used to gain trust
Chapter 3 business function development (batch export of market activities, Apache POI)
MySQL explain (glory Collection Edition)