coloX RLottie Lua 绑定使用指南
基于 RLottie 的 Lottie 矢量动画渲染 Lua API,采用全局函数风格。version>=13的版本,使用thorvg代替RLottie,提供兼容的api.
启用方式
使用 usemod('rlottie') 开启 rlottie 模块。
usemod('rlottie')
核心类型
- animation — 动画对象,由
rlottie_load创建,持有渲染缓冲区与帧数据
核心思路
通过 rlottie_load 从 JSON 数据加载动画,返回动画对象。使用 rlottie_draw 渲染指定帧到屏幕,内部自动管理帧缓冲和缓存(重复绘制同一帧不重复渲染)。
local ani = rlottie_load('key', json_data, 160, 160) -- 加载动画
local cur, total, fps, w, h = rlottie_info(ani) -- 查询信息
rlottie_draw(ani, 0, 40, 40, 1) -- 渲染第0帧到屏幕(40,40)
目录
1. 全局函数
所有函数为全局函数,通过 usemod('rlottie') 注册。
rlottie_load(key, json_data, width, height) → animation
加载 Lottie 动画。从 JSON 字符串创建动画对象,分配指定尺寸的渲染缓冲区。
key(string) — 动画缓存标识,每个动画使用唯一 keyjson_data(string) — Lottie JSON 动画数据width(integer) — 渲染宽度(像素)height(integer) — 渲染高度(像素)
local ani = rlottie_load('scene1', scenejson, 160, 160)
JSON 数据通常通过
#blob指令加载外部文件,或使用=[=[...]=]内嵌。
rlottie_close(animation)
关闭并释放动画对象。动画对象被 GC 回收时也会自动释放,但可以手动调用来提前回收资源。
rlottie_close(ani)
rlottie_info(animation) → current_frame, total_frames, framerate, width, height
获取动画信息。
返回值:
current_frame— 当前帧索引total_frames— 总帧数framerate— 帧率 (fps)width— 渲染宽度height— 渲染高度
local cur, total, fps, w, h = rlottie_info(ani)
print(string.format("frames:%d fps:%d size:%dx%d", total, fps, w, h))
rlottie_draw(animation, frame, x, y [, flag])
渲染动画的指定帧并输出到屏幕。
animation— 动画对象frame(integer) — 要渲染的帧号x(integer) — 屏幕 X 坐标y(integer) — 屏幕 Y 坐标flag(integer, 可选, 默认 0) — 绘制标志
内部自动缓存:重复请求同一帧不会重复渲染,直接使用缓冲区输出。
rlottie_draw(ani, 0, 40, 40, 1)
rlottie_prop(animation, type, keypath [, v1, v2, v3]) - 已废弃
动态覆盖动画属性。修改后自动使帧缓存失效,下次 rlottie_draw 会重新渲染。
animation— 动画对象type(integer) — 属性类型,见属性覆盖类型keypath(string) — 属性路径,如"layer1.fill1";支持通配符"**"v1, v2, v3(number, 可选) — 属性值,含义取决于 type
-- 修改填充色为红色 (RGB 范围 [0..1])
rlottie_prop(ani, 0, "layer1.fill1", 1.0, 0.0, 0.0)
-- 修改描边宽度
rlottie_prop(ani, 4, "layer1.stroke1", 3.0, 0, 0)
rlottie_mark(animation) → markers_table
获取动画标记列表。返回一个数组表,每个元素为 {name, startframe, endframe}。
local markers = rlottie_mark(ani)
if markers then
for i = 1, #markers do
print(markers[i][1], markers[i][2], markers[i][3])
end
end
2. 属性覆盖类型
rlottie_prop 的 type 参数取值:
| 值 | 常量名 | 说明 | 参数值 |
|---|---|---|---|
| 0 | FILLCOLOR | 填充颜色 | v1=R, v2=G, v3=B (范围 [0..1]) |
| 1 | FILLOPACITY | 填充不透明度 | v1=opacity (范围 [0..100]) |
| 2 | STROKECOLOR | 描边颜色 | v1=R, v2=G, v3=B (范围 [0..1]) |
| 3 | STROKEOPACITY | 描边不透明度 | v1=opacity (范围 [0..100]) |
| 4 | STROKEWIDTH | 描边宽度 | v1=width (float) |
| 5 | TR_ANCHOR | 变换锚点 | v1, v2, v3 (int) |
| 6 | TR_POSITION | 变换位置 | v1, v2, v3 (int) |
| 7 | TR_SCALE | 变换缩放 | v1, v2, v3 (范围 [0..100]) |
| 8 | TR_ROTATION | 变换旋转 | v1=degrees (范围 [0..360]) |
| 9 | TR_OPACITY | 变换不透明度 | v1=opacity (范围 [0..100]) |
3. 完整 API 索引
| 函数 | 返回 | 说明 |
|---|---|---|
rlottie_load(key, json, w, h) | animation | 加载 Lottie 动画 |
rlottie_close(animation) | — | 关闭动画(释放资源) |
rlottie_info(animation) | cur, total, fps, w, h | 获取动画信息 |
rlottie_draw(animation, frame, x, y [, flag]) | — | 渲染指定帧到屏幕 |
rlottie_prop(animation, type, keypath [, v1, v2, v3]) | — | 动态覆盖动画属性 |
rlottie_mark(animation) | markers_table | 获取标记列表 |
4. 综合示例
示例1:基础动画播放
加载 Lottie JSON 并循环播放。
usemod('rlottie')
-- 通过 #blob 加载外部 JSON 文件
local scenejson =
#blob lottie-3.json
local scene1 = rlottie_load('scene1', scenejson, 160, 160)
local frm1, frmmax1 = rlottie_info(scene1)
bg = palrgb(100, 0x39, 0x38, 0x39)
function _draw()
cls(bg)
rlottie_draw(scene1, frm1, 40, 40, 1)
frm1 = frm1 + 1
print(string.format("fps:%2d,cpu:%5.1f%%", stat(7), stat(1)*100), 46, 30, red, 0, 16)
end
示例2:多动画与标记查询
源自 mini_home.clx。同时加载多个动画,使用标记信息。
usemod('rlottie')
local ani1json =[=[
{"v":"4.10.2","fr":30,"ip":0,"op":58,"w":800,"h":800, ...}
]=]
local scenejson =[=[
{"v":"5.8.1","fr":30,"ip":0,"op":60,"w":480,"h":480, ...}
]=]
-- 加载动画
local ani1 = rlottie_load('ani1', ani1json, 200, 200)
local scene1 = rlottie_load('scene1', scenejson, 120, 120)
-- 查询信息
printh(rlottie_info(scene1))
-- 查询标记
local marker = rlottie_mark(scene1)
if marker then
printh('marker size', #marker)
for i = 1, #marker do
printh(i, unpack(marker[i], 1, 3))
end
end
local frm1 = 0
local frm2, frmmax2 = rlottie_info(scene1)
-- 渲染循环
function drawbg()
-- 绘制背景...
rlottie_draw(ani1, frm1, 300, 130, 1)
frm1 = frm1 + 1
if frm2 > 0 then
rlottie_draw(scene1, frm2, 180, 140, 1)
frm2 = frm2 + 1
if frm2 > frmmax2 then
frm2 = 0
end
end
end
function _draw()
cls(dark_green)
drawbg()
end
示例3:动态属性覆盖
演示使用 rlottie_prop 在运行时修改动画颜色。
usemod('rlottie')
local anijson =[=[
{"v":"5.8.1","fr":30,"ip":0,"op":60,"w":200,"h":200, ...}
]=]
local ani = rlottie_load('demo', anijson, 200, 200)
local cur, total, fps = rlottie_info(ani)
-- 修改填充色为蓝色 (RGB [0..1])
rlottie_prop(ani, 0, "**.fill1", 0.0, 0.5, 1.0)
-- 修改描边宽度
rlottie_prop(ani, 4, "**.stroke1", 2.0, 0, 0)
-- 修改变换不透明度
rlottie_prop(ani, 9, "layer1", 80, 0, 0)
function _draw()
cls()
rlottie_draw(ani, cur, 20, 20, 1)
cur = cur + 1
end
示例功能覆盖
| 功能 | 用到的 API |
|---|---|
| 加载动画 | rlottie_load |
| 查询帧信息 | rlottie_info |
| 渲染帧到屏幕 | rlottie_draw |
| 获取标记 | rlottie_mark |
| 动态修改属性 | rlottie_prop |
| 释放资源 | rlottie_close |