Skip to main content

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
  2. Property override types
  3. Complete API index
  4. Complete examples

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 animation
  • json_data (string) — Lottie JSON animation data
  • width (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 index
  • total_frames — Total frame count
  • framerate — Frame rate (fps)
  • width — Render width
  • height — 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 object
  • frame (integer) — Frame number to render
  • x (integer) — Screen X coordinate
  • y (integer) — Screen Y coordinate
  • flag (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 object
  • type (integer) — Property type; see Property override types
  • keypath (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:

ValueConstantDescriptionParameter values
0FILLCOLORFill colorv1=R, v2=G, v3=B (range [0..1])
1FILLOPACITYFill opacityv1=opacity (range [0..100])
2STROKECOLORStroke colorv1=R, v2=G, v3=B (range [0..1])
3STROKEOPACITYStroke opacityv1=opacity (range [0..100])
4STROKEWIDTHStroke widthv1=width (float)
5TR_ANCHORTransform anchorv1, v2, v3 (int)
6TR_POSITIONTransform positionv1, v2, v3 (int)
7TR_SCALETransform scalev1, v2, v3 (range [0..100])
8TR_ROTATIONTransform rotationv1=degrees (range [0..360])
9TR_OPACITYTransform opacityv1=opacity (range [0..100])

3. Complete API Index​

FunctionReturnsDescription
rlottie_load(key, json, w, h)animationLoad Lottie animation
rlottie_close(animation)—Close animation (release resources)
rlottie_info(animation)cur, total, fps, w, hGet 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_tableGet 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​

FeatureAPI
Load animationrlottie_load
Query frame informationrlottie_info
Render frames to screenrlottie_draw
Get markersrlottie_mark
Override properties dynamicallyrlottie_prop
Release resourcesrlottie_close