banner
banner
banner
NEWS LETTER

小程序充电动画:SVG 环形进度与定时器管理

Scroll down

充电动画要做的事很简单:一圈进度从 0 绕到 100,中间实时显示百分比,走完停在「充电已完成」。
实现上没有引任何图表库,也没有 canvas 绘制,而是把每次要显示的 SVG 拼成字符串、encodeURIComponent 编码后塞进 <image> 的 src。
全部逻辑集中在一个自定义组件里,进度推进靠一个 600ms 的定时器,改的只是 SVG 里 stroke-dashoffset 这一个数字。
真正的坑在定时器的归属:原稿把它写在 pageLifetimes.show 的局部变量里,组件销毁时根本拿不到它。

一、背景

组件放在 components/charging/charging,页面里直接 <charging /> 就能用:

  • 外圈灰色实线是容器,内圈灰色虚线是刻度,红色弧是当前进度;
  • 中间叠一层 <text> 显示百分比;
  • 进度每 600ms 涨 1%,到 100% 后清掉定时器并改成「充电已完成」;
  • 组件跟着 pageLifetimes 走:页面显示时开始播,切到别的页面暂停,回来重新从 0 播;组件销毁时务必清掉定时器。

二、实现

2.1 拼出 SVG 字符串

进度圆 r=100,周长 2πr ≈ 628。把 stroke-dasharray 设成整圈周长、stroke-dashoffset 从 628 递减到 0,红色弧就从无到有绕满一圈。

1
2
3
4
5
6
7
8
9
10
11
12
13
buildSvg(dashoffset: number) {
return `<?xml version="1.0" encoding="UTF-8"?>
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<circle cx="150" cy="150" r="120" fill="transparent" stroke-width="1" stroke="grey"/>
<circle cx="150" cy="150" r="100" fill="transparent" stroke-width="12" stroke="grey" stroke-dasharray="2 5"/>
<circle cx="150" cy="150" r="100" fill="transparent" stroke-width="12" stroke="red"
stroke-dasharray="628" stroke-dashoffset="${dashoffset}"/>
</svg>`
}

toDataUri(svg: string) {
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`
}

2.2 定时器推进进度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Component({
data: {
url: '',
textContent: '0%',
},

pageLifetimes: {
show() {
this.startTimer()
},
hide() {
this.stopTimer()
},
},

lifetimes: {
detached() {
this.stopTimer() // 组件销毁前必须清掉,否则定时器会打到已卸载的实例上
},
},

methods: {
startTimer() {
this.stopTimer() // 先清残留,避免多次 show 叠加出多个 interval
this.index = 0
this.timer = setInterval(() => {
const progress = 628 - (628 * this.index) / 100
this.setData({
url: this.toDataUri(this.buildSvg(progress)),
textContent: `${this.index}%`,
})
this.index += 1

if (this.index > 100) {
this.stopTimer()
this.setData({ textContent: '充电已完成' })
}
}, 600)
},

stopTimer() {
if (this.timer) {
clearInterval(this.timer)
this.timer = 0
}
},
},
})

原稿问题:let timer = setInterval(...) 是 show() 内的局部变量,hide / detached 里拿不到它,所以第一次写就注定漏清理——组件进出两三次就会叠加出多个 interval,进度肉眼可见地跳变。改为挂在 this.timer 上,并补上 hide 与 detached 两处 clearInterval。

2.3 结构:把 SVG 当图片渲染

组件只负责产出 url,wxml 用普通 <image> 渲染,零额外依赖:

1
2
3
4
<view class="svg-wrap">
<image class="svg-image" src="{{url}}" />
<text class="svg-text">{{textContent}}</text>
</view>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.svg-wrap {
position: relative;
display: flex;
align-items: center;
justify-content: center;
transform: rotate(-90deg); // 让进度从 12 点方向起步
}
.svg-image {
width: 240px !important;
height: 240px !important;
border-radius: 100%;
}
.svg-text {
position: absolute;
transform: rotate(90deg); // 反向抵消父级的 -90deg,文字才是正的
}

三、踩坑与修正

  • 628 不是魔法数:它等于 2 * π * 100。stroke-dasharray="628" 表示虚线一周期正好绕满整圈,stroke-dashoffset 就是「藏起来的长度」,所以从 628 减到 0 就是弧从无到满。
  • -90deg 要配一次反向旋转:外层容器转 -90deg 把起点挪到正上方,里面的文字必须再 rotate(90deg) 抵消,否则百分比是横躺的。
  • data URI 必须编码:data:image/svg+xml;charset=utf-8, 后面接 encodeURIComponent(svg),漏了编码,SVG 里的 #、<、空格会直接截断 URI,图片白屏。
  • 不要每帧重建整段 SVG 字符串再 setData 大对象:这里每次只更新 url 与 textContent 两个短字段,600ms 一次,压力不大;如果换成 16ms 一次的高频动画,就该改走 canvas,否则逻辑层到视图层的通信会成为瓶颈。
  • 纯前端模拟:progress 与设备真实电量无关,只是动画演示,接真实电量要换成 wx.getBatteryInfo。

小结

  • 「SVG 字符串 + encodeURIComponent + stroke-dashoffset」这套组合零依赖、矢量清晰,改色改尺寸都只是拼字符串,环形进度、评分环、加载圈都能直接套。
  • 定时器句柄一定要挂在实例上(this.timer),并在 hide 和 detached 两处都清,只清一处就会在反复进出时叠加。
  • 组件里凡是「进入时启动、离开时停止」的副作用,pageLifetimes 和 lifetimes 都要写一遍,前者管切页,后者管销毁。
其他文章
目录导航 置顶
  1. 1. 一、背景
  2. 2. 二、实现
    1. 2.1. 2.1 拼出 SVG 字符串
    2. 2.2. 2.2 定时器推进进度
    3. 2.3. 2.3 结构:把 SVG 当图片渲染
  3. 3. 三、踩坑与修正
  4. 4. 小结
请输入关键词进行搜索