Skip to main content

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
  2. Easing constants
  3. API reference
  4. Easing curve quick reference
  5. Complete examples

1. Core Concepts​

Normalized Progress​

All easing functions accept normalized input x in [0, 1]:

  • x = 0 → animation start
  • x = 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​

ConstantValueDescription
AnimLinear1Linear (constant speed)

Quadratic​

ConstantValueDescription
AnimEaseIn2Ease in (accelerate)
AnimEaseOut3Ease out (decelerate)
AnimEaseInOut4Ease in/out (accelerate then decelerate)

Cubic​

ConstantValueDescription
AnimEaseInCubic5Ease in (accelerate)
AnimEaseOutCubic6Ease out (decelerate)
AnimEaseInOutCubic7Ease in/out

Quartic​

ConstantValueDescription
AnimEaseInQuart8Ease in
AnimEaseOutQuart9Ease out
AnimEaseInOutQuart10Ease in/out

Quintic​

ConstantValueDescription
AnimEaseInQuint11Ease in
AnimEaseOutQuint12Ease out
AnimEaseInOutQuint13Ease in/out

Sine​

ConstantValueDescription
AnimEaseInSine14Ease in
AnimEaseOutSine15Ease out
AnimEaseInOutSine16Ease in/out

Exponential​

ConstantValueDescription
AnimEaseInExpo17Ease in
AnimEaseOutExpo18Ease out
AnimEaseInOutExpo19Ease in/out

Circular​

ConstantValueDescription
AnimEaseInCirc20Ease in
AnimEaseOutCirc21Ease out
AnimEaseInOutCirc22Ease in/out

Back​

ConstantValueDescription
AnimEaseInBack23Ease in (move backward before advancing)
AnimEaseOutBack24Ease out (overshoot the end and return)
AnimEaseInOutBack25Ease in/out with overshoot

Elastic​

ConstantValueDescription
AnimEaseInElastic26Ease in (spring effect)
AnimEaseOutElastic27Ease out (spring effect)
AnimEaseInOutElastic28Elastic ease in/out

Bounce​

ConstantValueDescription
AnimEaseInBounce29Ease in (bouncing ball effect)
AnimEaseOutBounce30Ease out (ball landing effect)
AnimEaseInOutBounce31Bouncing ease in/out

3. API Reference​

anim_effect(effect, x) → number​

Calculates the easing curve value.

Parameters:

ParameterTypeDescription
effectintegerEasing constant (such as AnimEaseOutCubic)
xnumberNormalized 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:

ParameterTypeDescription
effectintegerEasing constant
startnumberStarting value
end_numberEnding value
durationnumberTotal duration (frames or milliseconds, matching the unit of current)
currentnumberCurrent 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​

StyleRecommended effectsTypical use
Constant speedAnimLinearProgress bars, steady scrolling, rotation
Natural decelerationAnimEaseOut, AnimEaseOutCubicUI elements sliding in, popups appearing
Natural accelerationAnimEaseIn, AnimEaseInCubicObjects being pushed, button-press feedback
Smooth transitionAnimEaseInOut, AnimEaseInOutCubicPage changes, panel expansion/collapse
Emphasized ease inAnimEaseInQuart, AnimEaseInQuintCharging effects, loading animations
Emphasized ease outAnimEaseOutQuart, AnimEaseOutQuintRapid positioning, inertial stops
Gentle curveAnimEaseInSine ~ AnimEaseInOutSineSlight acceleration/deceleration, breathing animations
Abrupt changeAnimEaseInExpo ~ AnimEaseInOutExpoSudden appearance/disappearance, blinking
Circular motionAnimEaseInCirc ~ AnimEaseInOutCircRotation, arc motion
OvershootAnimEaseOutBackButtons popping out, scale bounce
Spring effectAnimEaseOutElasticElastic dragging, spring UI
Ball bounceAnimEaseOutBounceObjects landing, bouncing animations

Easing Strength Comparison (EaseOut Family)​

For the same ease-out style, strength increases with the power:

EffectDecelerationVisual impression
AnimEaseOut (Quad)GentleNatural, comfortable
AnimEaseOutCubicModerateSmooth, commonly used
AnimEaseOutQuartStrongerCrisp, decisive
AnimEaseOutQuintStrongAbrupt, forceful stop
AnimEaseOutExpoExtremeSudden 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​

FunctionParametersReturnsDescription
anim_effect(effect, x)effect: easing constant, x: progress [0,1]numberCalculate easing curve value
anim_calc(effect, start, end_, duration, current)effect: easing constant, start/end_: endpoints, duration: total duration, current: current timenumberEased range interpolation

Easing Constant Index​

ConstantValueCategory
AnimLinear1Basic
AnimEaseIn2Quadratic
AnimEaseOut3Quadratic
AnimEaseInOut4Quadratic
AnimEaseInCubic5Cubic
AnimEaseOutCubic6Cubic
AnimEaseInOutCubic7Cubic
AnimEaseInQuart8Quartic
AnimEaseOutQuart9Quartic
AnimEaseInOutQuart10Quartic
AnimEaseInQuint11Quintic
AnimEaseOutQuint12Quintic
AnimEaseInOutQuint13Quintic
AnimEaseInSine14Sine
AnimEaseOutSine15Sine
AnimEaseInOutSine16Sine
AnimEaseInExpo17Exponential
AnimEaseOutExpo18Exponential
AnimEaseInOutExpo19Exponential
AnimEaseInCirc20Circular
AnimEaseOutCirc21Circular
AnimEaseInOutCirc22Circular
AnimEaseInBack23Back
AnimEaseOutBack24Back
AnimEaseInOutBack25Back
AnimEaseInElastic26Elastic
AnimEaseOutElastic27Elastic
AnimEaseInOutElastic28Elastic
AnimEaseInBounce29Bounce
AnimEaseOutBounce30Bounce
AnimEaseInOutBounce31Bounce