启动页要在一两秒内交代「这是哪儿、什么时候能进」,所以这里没有放品牌大图,而是一层持续飘动的粒子背景加一个进度环。
粒子用新版 Canvas 2D 绘制,难点不在画,而在拿到 canvas 节点之后按设备像素比放大画布,否则高清屏上线条全是虚的。
进度环没有走 SVG,而是用 CSS conic-gradient 的两个色标位置表示百分比,改一个数字就是改进度。
下面是可运行的写法,以及原稿里三处会直接报错或失效的地方。
一、背景
启动页是 app.json 中 entryPagePath 指定的首屏(主包 pages/entry/entry),由 entry.ts / entry.wxml / entry.scss / entry.json 四个文件组成,动画播完由按钮 wx.switchTab 进入首页。
页面纵向只有三层,靠 z-index 严格分层:
| 层级 |
内容 |
作用 |
z-index: -2 |
全屏背景图 |
视觉底 |
| 底层 |
全屏 <canvas id="canvasId" type="2d"> |
飘动粒子 |
z-index: 10 |
进度环、文案、底部奔跑图、按钮 |
用户可见的全部交互 |
二、实现
2.1 取 canvas 节点并按 DPR 放大
新版 canvas 必须写 type="2d",节点不能同步取,只能走 SelectorQuery 的回调;取到之后要按 pixelRatio 放大画布,再 ctx.scale(dpr, dpr) 把坐标系缩回逻辑像素,否则高清屏上笔画发虚。
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| interface Particle { x: number y: number size: number vx: number vy: number color: string }
const wind = { width: 0, height: 0, particles: [] as Particle[], timer: 0 }
Page({ onReady() { this.initCanvas() },
initCanvas() { wx.createSelectorQuery() .select('#canvasId') .fields({ node: true, size: true }) .exec((res) => { if (!res[0]) return const canvas = res[0].node const ctx = canvas.getContext('2d') const width = res[0].width const height = res[0].height const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = width * dpr canvas.height = height * dpr ctx.scale(dpr, dpr)
wind.width = width wind.height = height const count = Math.round((width * height) / 12000) wind.particles = Array.from({ length: count }, () => ({ x: Math.random() * width, y: Math.random() * height, size: 0.5 + Math.random() * 1.5, vx: 0.2 + Math.random() * 0.6, vy: (Math.random() - 0.5) * 0.3, color: `rgba(255, 255, 255, ${(0.2 + Math.random() * 0.5).toFixed(2)})`, }))
this.startWind(ctx) }) },
startWind(ctx: any) { wind.timer = setInterval(() => { ctx.clearRect(0, 0, wind.width, wind.height) for (const p of wind.particles) { p.x += p.vx * 2 p.y += p.vy * 2 if (p.x < -10) p.x = wind.width + 10 if (p.x > wind.width + 10) p.x = -10 if (p.y < -10) p.y = wind.height + 10 if (p.y > wind.height + 10) p.y = -10
ctx.beginPath() ctx.strokeStyle = p.color ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2) ctx.stroke() } }, 16) },
onUnload() { clearInterval(wind.timer) }, })
|
原稿问题:canvas.width = width * dpr 里的 width / height 从未定义。fields({ node: true, size: true }) 拿到的尺寸在 res[0].width / res[0].height 上,直接照原稿跑会抛 ReferenceError: width is not defined。
2.2 进度环:用 conic-gradient 的两个色标当百分比
进度环比用 SVG 省事:外层留一圈虚线边框做描边,内层背景写 conic-gradient,第一个色标停在 value%,之后换成深色,改 value 就等于改进度。
1 2 3 4 5
| <canvas id="canvasId" type="2d" class="wind-canvas" /> <view class="circle" style="background: conic-gradient(rgba(240, 255, 240, 0.35) {{animation_circle.value}}%, rgba(34, 139, 34) {{animation_circle.value}}% 100%);" ></view>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| .wind-canvas { position: absolute; left: 0; top: 0; width: 100vw; height: 100vh; } .circle { width: 270rpx; height: 270rpx; border: 1rpx dashed rgba(102, 205, 170, 1); border-radius: 50%; }
|
原稿问题:rgba(240,255,240) 只给了三个通道、缺 alpha,整条 background 声明会被判为无效值,进度环根本不显示。补成 rgba(240, 255, 240, 0.35),浅色扇形半透明,和深绿才有可见分层。
2.3 进度值的驱动与清理
进度每 250ms 减 1,减到 0 就回到 100,形成循环:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| Page({ data: { animation_circle: { value: 100 }, },
onLoad() { this.startProgress() },
startProgress() { this.progressTimer = setInterval(() => { const next = this.data.animation_circle.value - 1 this.setData({ 'animation_circle.value': next <= 0 ? 100 : next, }) }, 250) },
onUnload() { clearInterval(this.progressTimer) }, })
|
原稿问题:原写法把 { value, rafId } 整个对象 setData 到视图层,而 rafId 存的是 setInterval 返回的句柄——渲染层根本用不到数字句柄,白跑一次数据通信;同时页面退出时没有 clearInterval,定时器会一直跑。这里把句柄挂在 this 上,只 setData 变化的那一个字段。
2.4 底部奔跑图与安全区
底部元素用纯 CSS 关键帧无限横移,容器留出 env(safe-area-inset-bottom) 避免被 iPhone 底部横条盖住:
1 2 3 4 5 6 7 8 9 10 11
| .run_wrap { padding-bottom: env(safe-area-inset-bottom); .img { position: absolute; animation: runLoop 10s linear infinite; } } @keyframes runLoop { 0% { right: -30rpx; } 100% { right: 100vw; } }
|
right 从 -30rpx 一路增到 100vw,元素视觉上是从右往左移动,写注释时别把方向写反。
三、踩坑与修正
type="2d" 是硬要求:老的 wx.createCanvasContext 已废弃,2D 节点只能等 SelectorQuery 回调,拿不到节点时 res[0] 是 undefined,代码里要留一次判空。
- DPR 两步都要做:只放大
canvas.width/height 不 scale,坐标会被放大 dpr 倍、内容跑出画布;只 scale 不放大画布,笔画发虚。
setInterval 必须配对清理:onUnload 里 clearInterval,否则退出页面后定时器还在跑,回调里的 ctx 已失效。
conic-gradient 的端点只能是硬边,做不出圆头进度条;要圆头仍得回 SVG 或拿伪元素补两个小圆点。
- 安全区用
env() 比写死 padding 稳,异形屏上不会顶到横条。
小结
- Canvas 2D 的流程固定:
type="2d" → SelectorQuery 取 node 与 size → 按 dpr 放大画布 → ctx.scale(dpr, dpr) 后再画。
- 粒子动画只需要「越界从对侧回来」一条规则,就能模拟持续吹过的风,不要写物理引擎。
conic-gradient 用两个色标卡在同一个 value% 上即可当进度环,零节点、零 JS 绘制,代价是端点只能是直角。
- 动画用的定时器 /
requestAnimationFrame 句柄挂在 this 上,别塞进 data,并在页面卸载时清掉。