coloX RLottie Lua Binding Guide
Lua API for Lottie vector animation rendering based on RLottie, using global functions. In version>=13, thorvg replaces RLottie while providing compatible APIs.
Enabling the Module
Enable the rlottie module with usemod('rlottie').
usemod('rlottie')
Core Types
- animation — Created by
rlottie_load, holding rendering buffers and frame data
Core Approach
Load JSON animation data with rlottie_load to obtain an animation object. Render a specified frame to screen with rlottie_draw; frame buffers and caching are managed internally (repeatedly drawing the same frame does not rerender it).
local ani = rlottie_load('key', json_data, 160, 160) -- Load the animation
local cur, total, fps, w, h = rlottie_info(ani) -- Query information
rlottie_draw(ani, 0, 40, 40, 1) -- Render frame 0 at screen position (40,40)
Contents
1. Global Functions
All functions are global, registered by usemod('rlottie').
rlottie_load(key, json_data, width, height) → animation
Loads a Lottie animation. Creates an animation object from JSON and allocates a rendering buffer of the specified size.
key(string) — Animation cache identifier; use a unique key for each animationjson_data(string) — Lottie JSON animation datawidth(integer) — Render width (pixels)height(integer) — Render height (pixels)
local ani = rlottie_load('scene1', scenejson, 160, 160)
JSON data is usually loaded from external files with
#blob, or embedded with=[=[...]=].
rlottie_close(animation)
Closes and releases the animation object. GC also releases it automatically, but a manual call reclaims resources earlier.
rlottie_close(ani)
rlottie_info(animation) → current_frame, total_frames, framerate, width, height
Gets animation information.
Return values:
current_frame— Current frame indextotal_frames— Total frame countframerate— Frame rate (fps)width— Render widthheight— Render 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])
Renders a specified animation frame to the screen.
animation— Animation objectframe(integer) — Frame number to renderx(integer) — Screen X coordinatey(integer) — Screen Y coordinateflag(integer, optional, default 0) — Drawing flags
Internal caching: repeated requests for the same frame use buffered output without rerendering.
rlottie_draw(ani, 0, 40, 40, 1)
rlottie_prop(animation, type, keypath [, v1, v2, v3]) - Deprecated
Overrides animation properties dynamically. Changes invalidate the frame cache, so the next rlottie_draw rerenders.
animation— Animation objecttype(integer) — Property type; see Property override typeskeypath(string) — Property path, such as"layer1.fill1"; supports wildcard"**"v1, v2, v3(number, optional) — Property values, depending on type
-- Change the fill to red (RGB range [0..1])
rlottie_prop(ani, 0, "layer1.fill1", 1.0, 0.0, 0.0)
-- Change the stroke width
rlottie_prop(ani, 4, "layer1.stroke1", 3.0, 0, 0)
rlottie_mark(animation) → markers_table
Gets animation markers as an array of {name, startframe, endframe} elements.
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. Property Override Types
Values of the type parameter in rlottie_prop:
| Value | Constant | Description | Parameter values |
|---|---|---|---|
| 0 | FILLCOLOR | Fill color | v1=R, v2=G, v3=B (range [0..1]) |
| 1 | FILLOPACITY | Fill opacity | v1=opacity (range [0..100]) |
| 2 | STROKECOLOR | Stroke color | v1=R, v2=G, v3=B (range [0..1]) |
| 3 | STROKEOPACITY | Stroke opacity | v1=opacity (range [0..100]) |
| 4 | STROKEWIDTH | Stroke width | v1=width (float) |
| 5 | TR_ANCHOR | Transform anchor | v1, v2, v3 (int) |
| 6 | TR_POSITION | Transform position | v1, v2, v3 (int) |
| 7 | TR_SCALE | Transform scale | v1, v2, v3 (range [0..100]) |
| 8 | TR_ROTATION | Transform rotation | v1=degrees (range [0..360]) |
| 9 | TR_OPACITY | Transform opacity | v1=opacity (range [0..100]) |
3. Complete API Index
| Function | Returns | Description |
|---|---|---|
rlottie_load(key, json, w, h) | animation | Load Lottie animation |
rlottie_close(animation) | — | Close animation (release resources) |
rlottie_info(animation) | cur, total, fps, w, h | Get animation information |
rlottie_draw(animation, frame, x, y [, flag]) | — | Render a specified frame to screen |
rlottie_prop(animation, type, keypath [, v1, v2, v3]) | — | Override animation properties dynamically |
rlottie_mark(animation) | markers_table | Get marker list |
4. Complete Examples
Example 1: Basic Animation Playback
Loads Lottie JSON and loops playback.
usemod('rlottie')
-- Load an external JSON file with #blob
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
Example 2: Multiple Animations and Marker Queries
From mini_home.clx. Loads multiple animations and uses marker information.
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, ...}
]=]
-- Load the animation
local ani1 = rlottie_load('ani1', ani1json, 200, 200)
local scene1 = rlottie_load('scene1', scenejson, 120, 120)
-- Query information
printh(rlottie_info(scene1))
-- Query markers
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)
-- Render loop
function drawbg()
-- Draw the background...
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
Example 3: Dynamic Property Overrides
Demonstrates changing animation colors at runtime with 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)
-- Change the fill to blue (RGB [0..1])
rlottie_prop(ani, 0, "**.fill1", 0.0, 0.5, 1.0)
-- Change the stroke width
rlottie_prop(ani, 4, "**.stroke1", 2.0, 0, 0)
-- Change transform opacity
rlottie_prop(ani, 9, "layer1", 80, 0, 0)
function _draw()
cls()
rlottie_draw(ani, cur, 20, 20, 1)
cur = cur + 1
end
Example Feature Coverage
| Feature | API |
|---|---|
| Load animation | rlottie_load |
| Query frame information | rlottie_info |
| Render frames to screen | rlottie_draw |
| Get markers | rlottie_mark |
| Override properties dynamically | rlottie_prop |
| Release resources | rlottie_close |