类: Rainbow (彩虹动画)
约 1942 字大约 6 分钟
2026-09-06
构建固件和导入到JS
在 C++ 里加入以下代码, 然后重新编译构建固件:
// 为了控制固件的尺寸,BeShell 的 module 是按需引入的。
beshell.use<be::driver::ws2812b::WS2812B>() ;Rainbow 类由 ws2812b 模块提供:
import { Rainbow } from 'ws2812b'简介
彩虹动画主控
内建一个 animator 模块的 Timeline(时间线),把若干 Section(灯带分段)的 可动画属性(x/len/起终点 HSV/前后阴影长度)注册为关键帧轨道;播放时 Timeline 逐帧插值属性,并在有属性实际变更的帧自动重绘灯带 (clear → 逐段绘制 → flush),无需 JS 侧参与。
支持 exportConfig()/importConfig() 整体导出/导入配置(含 strip 驱动层 配置、全部分段与关键帧),供编辑器等工具使用。
示例:
import { Strip, Section, Rainbow } from "ws2812b"
const strip = new Strip()
strip.setup({ pin: 8, length: 16, loop: true })
const rainbow = new Rainbow(strip, { fps: 30, frames: 300, repeat: true })
const sec = rainbow.addSection(new Section({ x: 0, len: 8, h0: 0, h1: 360 }))
rainbow.set(0, sec, { x: 0 })
rainbow.set(300, sec, { x: 16, ease: "linear" }) // 300 帧绕回环灯带一圈
rainbow.draw() // 初始帧
rainbow.play()类方法
方法 constructor
原型: constructor (strip:Strip, options:object=)
构造函数
创建彩虹动画主控,绑定目标灯带,并创建内建时间线。
示例:
const rainbow = new Rainbow(strip, { fps: 30, frames: 300, repeat: true })参数:
strip
类型Strip
参数说明目标灯带
options
类型object (详见下方类型定义)
默认值
参数说明时间线选项
options 类型定义:
{ fps?: number, // 帧率,默认 60 frames?: number, // 总帧数,0 表示不限,默认 0 repeat?: boolean // 是否循环播放,默认 false }
返回值:
类型Rainbow
说明实例
方法 addSection
原型: addSection (section:Section)
添加分段
将分段加入 Rainbow 管理并绑定到目标 strip,返回该分段。
参数:
section
类型Section
参数说明分段
返回值:
类型Section
说明该分段
方法 removeSection
原型: removeSection (section:Section)
移除分段
将分段从 Rainbow 移除并解除 strip 绑定。
参数:
section
类型Section
参数说明分段
返回值:
类型undefined
方法 set
原型: set (frame:number, section:Section, prop:string|object, value:number, ease:string="linear", easeS:number=1.70158)
设置关键帧(替换语义:同帧同属性覆盖,ease 省略时重置为 linear)
rainbow.set(30, sec, "x", 100, "outQuad")
rainbow.set(30, sec, { x: 100, h0: [200, "inOutSine"] })参数:
frame
类型number
参数说明帧号
section
类型Section
参数说明目标分段
prop
类型string, object
参数说明属性名或属性表
value
类型number
参数说明目标值(单属性形式)
ease
类型string
默认值"linear"
参数说明缓动类型名称
easeS
类型number
默认值1.70158
参数说明缓动参数
返回值:
undefined
方法 add
原型: add ()
累加关键帧(合并语义:同帧同属性值累加,ease 省略时保留原设置)
输入形式同 set()
返回值:
undefined
方法 unset
原型: unset (frame:number, section:Section=, prop:string=)
移除关键帧
rainbow.unset(frm) // 移除该帧全部轨道
rainbow.unset(frm, sec, "x") // 仅移除该帧某分段的某属性参数:
frame
类型number
参数说明帧号
section
类型Section
默认值
参数说明目标分段(可选)
prop
类型string
默认值
参数说明属性名(可选)
返回值:
undefined
方法 play
原型: play ()
开始播放
从头(第 0 帧)开始播放时间线。建议播放前先调用一次 draw() 绘制初始帧。
返回值:
类型undefined
方法 stop
原型: stop ()
停止播放
停止时间线并回到第 0 帧(播完自动停止与手动停止效果相同)。
返回值:
类型undefined
方法 pause
原型: pause ()
暂停播放
暂停在当前帧,可用 resume() 继续。
返回值:
类型undefined
方法 resume
原型: resume ()
继续播放
从暂停的帧继续播放。
返回值:
类型undefined
方法 nextFrame
原型: nextFrame ()
等待下一帧(转发内建 Timeline 的 nextFrame)
返回值:
类型Promise<number>
说明当前帧序号
方法 getTimeline
原型: getTimeline ()
获取内建 Timeline 对象(高级用法:监听 stop/pause 事件等)
返回值:
类型Timeline
说明内建时间线
方法 draw
原型: draw ()
手动重绘一帧
通常在 play() 前调用一次作为初始帧;播放期间由 change 事件自动重绘。
返回值:
undefined
方法 exportConfig
原型: exportConfig ()
导出配置
返回可 JSON 序列化的完整配置(供编辑器等工具使用):
{
fps, frames, repeat,
strip: { pin, length, loop },
sections: [ ... ],
keyframes: [ { frame, section, prop, value, ease, easeS }, ... ]
}返回值:
类型object
说明配置对象
方法 importConfig
原型: importConfig (config:object)
导入配置
应用 exportConfig() 产出的配置对象:重建 strip 驱动层配置、 重建时间线(fps/frames/repeat)、替换全部分段、恢复全部关键帧, 最后绘制一帧。
参数:
config
类型object
参数说明配置对象
返回值:
类型undefined
