coloX Anim Easing Functions Lua Programming Guide
The coloX animation module provides numerical interpolation with easing curves (Easing Functions), driving UI animations, motion paths, property transitions, and similar effects.
Enabling the Module
The anim module is ready to use: anim_effect/anim_calc and all Anim* constants are registered at cartridge startup, without usemod.
Contents
1. Core Concepts
Normalized Progress
All easing functions accept normalized input x in [0, 1]:
x = 0→ animation startx = 1→ animation end
The return value is also within [0, 1] (some elastic/back effects may exceed this range), representing the eased progress.
Linear Interpolation
Easing functions output only normalized progress; combine them with linear interpolation to map it to the target range:
value = start + (end - start) × eased_progress
anim_calc encapsulates this calculation.
2. Easing Constants
Once enabled, the module automatically registers the following 31 global constants. Each is an integer used as the first parameter of anim_effect and anim_calc.
Basic
| Constant | Value | Description |
|---|---|---|
AnimLinear | 1 | Linear (constant speed) |
Quadratic
| Constant | Value | Description |
|---|---|---|
AnimEaseIn | 2 | Ease in (accelerate) |
AnimEaseOut | 3 | Ease out (decelerate) |
AnimEaseInOut | 4 | Ease in/out (accelerate then decelerate) |
Cubic
| Constant | Value | Description |
|---|---|---|
AnimEaseInCubic | 5 | Ease in (accelerate) |
AnimEaseOutCubic | 6 | Ease out (decelerate) |
AnimEaseInOutCubic | 7 | Ease in/out |
Quartic
| Constant | Value | Description |
|---|---|---|
AnimEaseInQuart | 8 | Ease in |
AnimEaseOutQuart | 9 | Ease out |
AnimEaseInOutQuart | 10 | Ease in/out |
Quintic
| Constant | Value | Description |
|---|---|---|
AnimEaseInQuint | 11 | Ease in |
AnimEaseOutQuint | 12 | Ease out |
AnimEaseInOutQuint | 13 | Ease in/out |
Sine
| Constant | Value | Description |
|---|---|---|
AnimEaseInSine | 14 | Ease in |
AnimEaseOutSine | 15 | Ease out |
AnimEaseInOutSine | 16 | Ease in/out |
Exponential
| Constant | Value | Description |
|---|---|---|
AnimEaseInExpo | 17 | Ease in |
AnimEaseOutExpo | 18 | Ease out |
AnimEaseInOutExpo | 19 | Ease in/out |
Circular
| Constant | Value | Description |
|---|---|---|
AnimEaseInCirc | 20 | Ease in |
AnimEaseOutCirc | 21 | Ease out |
AnimEaseInOutCirc | 22 | Ease in/out |
Back
| Constant | Value | Description |
|---|---|---|
AnimEaseInBack | 23 | Ease in (move backward before advancing) |
AnimEaseOutBack | 24 | Ease out (overshoot the end and return) |
AnimEaseInOutBack | 25 | Ease in/out with overshoot |
Elastic
| Constant | Value | Description |
|---|---|---|
AnimEaseInElastic | 26 | Ease in (spring effect) |
AnimEaseOutElastic | 27 | Ease out (spring effect) |
AnimEaseInOutElastic | 28 | Elastic ease in/out |
Bounce
| Constant | Value | Description |
|---|---|---|
AnimEaseInBounce | 29 | Ease in (bouncing ball effect) |
AnimEaseOutBounce | 30 | Ease out (ball landing effect) |
AnimEaseInOutBounce | 31 | Bouncing ease in/out |
3. API Reference
anim_effect(effect, x) → number
Calculates the easing curve value.
Parameters:
| Parameter | Type | Description |
|---|---|---|
effect | integer | Easing constant (such as AnimEaseOutCubic) |
x | number | Normalized progress [0, 1] |
Returns: number — eased progress, usually in [0, 1] (elastic/back effects may exceed this range).
Example:
usemod('anim')
-- Linear: return the input directly
local v1 = anim_effect(AnimLinear, 0.5) -- → 0.5
-- Quadratic ease-out: more progress in the first half
local v2 = anim_effect(AnimEaseOut, 0.5) -- → 0.75
-- Cubic ease-in: less progress in the first half
local v3 = anim_effect(AnimEaseInCubic, 0.5) -- → 0.125
-- Elastic ease-out: may exceed 1.0
local v4 = anim_effect(AnimEaseOutElastic, 0.5) -- → ~1.02
Usage: Use this function when controlling interpolation yourself, for example custom mappings, joining animation segments, or nonlinear blending.
animcalc(effect, start, end, duration, current) → number
Interpolates over a range with easing, encapsulating normalization → easing → linear interpolation.
Parameters:
| Parameter | Type | Description |
|---|---|---|
effect | integer | Easing constant |
start | number | Starting value |
end_ | number | Ending value |
duration | number | Total duration (frames or milliseconds, matching the unit of current) |
current | number | Current progress (same unit, range [0, duration]) |
Returns: number — current eased value between start and end_.
Formula:
progress = current / duration -- Normalize to [0, 1]
eased = anim_effect(effect, progress) -- Apply easing
result = start + (end_ - start) × eased -- Linear interpolation
Example:
-- Ease X from 10 to 200 over 60 frames
local x = anim_calc(AnimEaseOutCubic, 10, 200, 60, 30)
-- At frame 30, cubic ease-out gives progress 0.875
-- x ≈ 10 + (200-10) × 0.875 = 176.25
-- Fade opacity from 255 to 0 over 1000 milliseconds
local alpha = anim_calc(AnimEaseInOut, 255, 0, 1000, 500)
-- At the midpoint (500ms), EaseInOut gives progress 0.5
-- alpha = 255 + (0-255) × 0.5 = 127.5
-- Ease rotation from 0 to 360 degrees with an elastic effect
local angle = anim_calc(AnimEaseOutElastic, 0, 360, 120, 90)
Note: Avoid duration=0; the result is then inf/NaN (without throwing).
4. Easing Curve Quick Reference
The following characteristics help select an appropriate effect:
Choosing by Animation Style
| Style | Recommended effects | Typical use |
|---|---|---|
| Constant speed | AnimLinear | Progress bars, steady scrolling, rotation |
| Natural deceleration | AnimEaseOut, AnimEaseOutCubic | UI elements sliding in, popups appearing |
| Natural acceleration | AnimEaseIn, AnimEaseInCubic | Objects being pushed, button-press feedback |
| Smooth transition | AnimEaseInOut, AnimEaseInOutCubic | Page changes, panel expansion/collapse |
| Emphasized ease in | AnimEaseInQuart, AnimEaseInQuint | Charging effects, loading animations |
| Emphasized ease out | AnimEaseOutQuart, AnimEaseOutQuint | Rapid positioning, inertial stops |
| Gentle curve | AnimEaseInSine ~ AnimEaseInOutSine | Slight acceleration/deceleration, breathing animations |
| Abrupt change | AnimEaseInExpo ~ AnimEaseInOutExpo | Sudden appearance/disappearance, blinking |
| Circular motion | AnimEaseInCirc ~ AnimEaseInOutCirc | Rotation, arc motion |
| Overshoot | AnimEaseOutBack | Buttons popping out, scale bounce |
| Spring effect | AnimEaseOutElastic | Elastic dragging, spring UI |
| Ball bounce | AnimEaseOutBounce | Objects landing, bouncing animations |
Easing Strength Comparison (EaseOut Family)
For the same ease-out style, strength increases with the power:
| Effect | Deceleration | Visual impression |
|---|---|---|
AnimEaseOut (Quad) | Gentle | Natural, comfortable |
AnimEaseOutCubic | Moderate | Smooth, commonly used |
AnimEaseOutQuart | Stronger | Crisp, decisive |
AnimEaseOutQuint | Strong | Abrupt, forceful stop |
AnimEaseOutExpo | Extreme | Sudden hard stop |
5. Complete Examples
Example 1: Visualizing Easing Curves
Draws curves for all easing effects, switching with buttons:
coloX-16
version 1
__lua__
usemod('anim')
local effects = {
'AnimLinear',
'AnimEaseIn',
'AnimEaseOut',
'AnimEaseInOut',
'AnimEaseInCubic',
'AnimEaseOutCubic',
'AnimEaseInOutCubic',
'AnimEaseInQuart',
'AnimEaseOutQuart',
'AnimEaseInOutQuart',
'AnimEaseInQuint',
'AnimEaseOutQuint',
'AnimEaseInOutQuint',
'AnimEaseInSine',
'AnimEaseOutSine',
'AnimEaseInOutSine',
'AnimEaseInExpo',
'AnimEaseOutExpo',
'AnimEaseInOutExpo',
'AnimEaseInCirc',
'AnimEaseOutCirc',
'AnimEaseInOutCirc',
'AnimEaseInBack',
'AnimEaseOutBack',
'AnimEaseInOutBack',
'AnimEaseInElastic',
'AnimEaseOutElastic',
'AnimEaseInOutElastic',
'AnimEaseInBounce',
'AnimEaseOutBounce',
'AnimEaseInOutBounce',
}
local cur = 1
function draw_curve(effname)
cls(0)
-- Title
print(effname, 120 - 8 * #effname / 2, 40, white, 0, 8)
-- Baseline (start and end points)
line(40, 170, 200, 170, white) -- start
line(40, 70, 200, 70, white) -- end
-- Draw the easing curve
color(green)
for i = 0, 160 do
local y = anim_calc(_G[effname], 170, 70, 160, i)
line(40 + i, y)
end
end
draw_curve(effects[cur])
function _draw()
if btnp(4) then
cur = cur + 1
if cur > #effects then cur = 1 end
draw_curve(effects[cur])
end
end
Example 2: UI Slide-In Animation
A button slides in from the right with an ease-out effect:
usemod('anim')
local DURATION = 30 -- Animation duration in frames
local START_X = 240 -- Initial X (outside the right edge)
local END_X = 80 -- Target X (inside the screen)
local Y = 100
local frame = 0
local animating = true
function _draw()
cls(0)
if animating then
frame = frame + 1
if frame >= DURATION then
frame = DURATION
animating = false
end
end
-- Calculate the current X with cubic ease-out
local x = anim_calc(AnimEaseOutCubic, START_X, END_X, DURATION, frame)
-- Draw the button
rect(x, Y, 80, 40, blue)
print("BUTTON", x + 16, Y + 14, white)
end
Example 3: Coordinated Property Animation
Animates position, size, and opacity simultaneously:
usemod('anim')
local DURATION = 45
local frame = 0
function _draw()
cls(0)
if frame < DURATION then
frame = frame + 1
end
-- Position: slide in from the left
local x = anim_calc(AnimEaseOutCubic, -60, 90, DURATION, frame)
-- Size: grow elastically from 0 to 60
local size = anim_calc(AnimEaseOutElastic, 0, 60, DURATION, frame)
-- Opacity: fade in from 0 to 255
local alpha = anim_calc(AnimEaseOut, 0, 255, DURATION, frame)
-- Draw
local color = getcolor(255, 100, 50, alpha)
circ(x + size / 2, 120, size / 2, color)
end
Example 4: Bouncing Effect
Simulates a ball landing and bouncing:
usemod('anim')
local DURATION = 60
local frame = 0
local GROUND_Y = 180
local TOP_Y = 40
function _draw()
cls(0)
-- Ground
line(0, GROUND_Y, 240, GROUND_Y, light_gray)
frame = frame + 1
if frame > DURATION then frame = 0 end
-- Bounce easing: fall quickly, then bounce repeatedly
local y = anim_calc(AnimEaseOutBounce, TOP_Y, GROUND_Y, DURATION, frame)
-- Draw the bouncing ball
circ(120, y, 10, red)
end
Example 5: Property Animation with the VG Module
Combines easing functions with thorVG methods such as set_transform and opacity to animate vector graphics:
usemod('anim')
usemod('vg')
local W, H = 240, 240
local CX, CY = W / 2, H / 2
local c = vg_new(W, H)
-- Helper: build a rotation and scaling transform around (cx, cy)
local function xform(cx, cy, s, angle)
local rad = math.rad(angle)
local cos_a = math.cos(rad)
local sin_a = math.sin(rad)
return
s * cos_a, -s * sin_a, cx - s * (cx * cos_a - cy * sin_a),
s * sin_a, s * cos_a, cy - s * (cx * sin_a + cy * cos_a)
end
-- Background
local bg = vg_rect(0, 0, W, H)
bg:fill(20, 20, 30)
c:add(bg)
-- Circular target
local circle = vg_circle(CX, CY, 30)
circle:fill(255, 120, 50)
c:add(circle)
-- Text
local font = vg_font_load("DIN_NATURE_Bold")
local txt = vg_text("HELLO", CX - 40, CY - 8, font, 20)
txt:set_color(255, 255, 255)
txt:align(0.5, 0.5)
txt:layout(80, 20)
c:add(txt)
-- Animation parameters
local DURATION = 60 -- Total frames
local frame = 0
c:draw(0, 0)
function _draw()
frame = frame + 1
if frame > DURATION * 2 then frame = 0 end
-- First DURATION frames: scale and rotate with an elastic entrance
-- After DURATION frames: hold
local f = math.min(frame, DURATION)
-- Scale: grow elastically from 0 to 1
local scale = anim_calc(AnimEaseOutElastic, 0, 1, DURATION, f)
-- Rotation: rotate from -180 to 0 degrees
local angle = anim_calc(AnimEaseOutCubic, -180, 0, DURATION, f)
-- Opacity: fade in from 0 to 255
local alpha = anim_calc(AnimEaseOut, 0, 255, DURATION, f)
alpha = math.floor(alpha + 0.5)
-- Apply the transform
circle:set_transform(xform(CX, CY, scale, angle))
circle:opacity(alpha)
txt:set_transform(xform(CX, CY, scale, angle))
txt:opacity(alpha)
c:draw(0, 0)
end
Example 6: Custom Interpolation with anim_effect
When custom mapping is needed rather than simple linear interpolation, use anim_effect to obtain eased progress:
usemod('anim')
-- Map eased progress to discrete values (such as color channels)
function ease_color(r1, g1, b1, r2, g2, b2, effect, duration, current)
local t = anim_effect(effect, current / duration)
return
r1 + (r2 - r1) * t,
g1 + (g2 - g1) * t,
b1 + (b2 - b1) * t
end
-- Transition from red to blue over 100 frames with ease-in/out
local DURATION = 100
local frame = 0
function _draw()
frame = frame + 1
if frame > DURATION then frame = 0 end
local r, g, b = ease_color(
255, 50, 30, -- Start: red
30, 80, 255, -- End: blue
AnimEaseInOutCubic,
DURATION,
frame
)
cls(getcolor(r, g, b))
end
API Index
| Function | Parameters | Returns | Description |
|---|---|---|---|
anim_effect(effect, x) | effect: easing constant, x: progress [0,1] | number | Calculate easing curve value |
anim_calc(effect, start, end_, duration, current) | effect: easing constant, start/end_: endpoints, duration: total duration, current: current time | number | Eased range interpolation |
Easing Constant Index
| Constant | Value | Category |
|---|---|---|
AnimLinear | 1 | Basic |
AnimEaseIn | 2 | Quadratic |
AnimEaseOut | 3 | Quadratic |
AnimEaseInOut | 4 | Quadratic |
AnimEaseInCubic | 5 | Cubic |
AnimEaseOutCubic | 6 | Cubic |
AnimEaseInOutCubic | 7 | Cubic |
AnimEaseInQuart | 8 | Quartic |
AnimEaseOutQuart | 9 | Quartic |
AnimEaseInOutQuart | 10 | Quartic |
AnimEaseInQuint | 11 | Quintic |
AnimEaseOutQuint | 12 | Quintic |
AnimEaseInOutQuint | 13 | Quintic |
AnimEaseInSine | 14 | Sine |
AnimEaseOutSine | 15 | Sine |
AnimEaseInOutSine | 16 | Sine |
AnimEaseInExpo | 17 | Exponential |
AnimEaseOutExpo | 18 | Exponential |
AnimEaseInOutExpo | 19 | Exponential |
AnimEaseInCirc | 20 | Circular |
AnimEaseOutCirc | 21 | Circular |
AnimEaseInOutCirc | 22 | Circular |
AnimEaseInBack | 23 | Back |
AnimEaseOutBack | 24 | Back |
AnimEaseInOutBack | 25 | Back |
AnimEaseInElastic | 26 | Elastic |
AnimEaseOutElastic | 27 | Elastic |
AnimEaseInOutElastic | 28 | Elastic |
AnimEaseInBounce | 29 | Bounce |
AnimEaseOutBounce | 30 | Bounce |
AnimEaseInOutBounce | 31 | Bounce |