Animation
The animation module provides utilities for creating smooth transitions, easing functions, and timing controls for TUI applications.
Overview
This module includes:
- Easing functions for various animation curves
- Generic animation system for numeric types
- FPS counter for performance monitoring
Easing Functions
The Easing struct contains static methods for common easing functions. Each function takes a normalized time value t (0.0 to 1.0) and returns an eased value.
Linear Easing
pub fn linear(t: f32) f32Returns the input value unchanged.
Quadratic Easing
pub fn easeInQuad(t: f32) f32
pub fn easeOutQuad(t: f32) f32
pub fn easeInOutQuad(t: f32) f32Cubic Easing
pub fn easeInCubic(t: f32) f32
pub fn easeOutCubic(t: f32) f32
pub fn easeInOutCubic(t: f32) f32Quartic Easing
pub fn easeInQuart(t: f32) f32
pub fn easeOutQuart(t: f32) f32
pub fn easeInOutQuart(t: f32) f32Quintic Easing
pub fn easeInQuint(t: f32) f32
pub fn easeOutQuint(t: f32) f32
pub fn easeInOutQuint(t: f32) f32Sine Easing
pub fn easeInSine(t: f32) f32
pub fn easeOutSine(t: f32) f32
pub fn easeInOutSine(t: f32) f32Exponential Easing
pub fn easeInExpo(t: f32) f32
pub fn easeOutExpo(t: f32) f32
pub fn easeInOutExpo(t: f32) f32Circular Easing
pub fn easeInCirc(t: f32) f32
pub fn easeOutCirc(t: f32) f32
pub fn easeInOutCirc(t: f32) f32Back Easing
pub fn easeInBack(t: f32) f32
pub fn easeOutBack(t: f32) f32
pub fn easeInOutBack(t: f32) f32Elastic Easing
pub fn easeInElastic(t: f32) f32
pub fn easeOutElastic(t: f32) f32
pub fn easeInOutElastic(t: f32) f32Bounce Easing
pub fn easeInBounce(t: f32) f32
pub fn easeOutBounce(t: f32) f32
pub fn easeInOutBounce(t: f32) f32Animation Types
EasingFn
pub const EasingFn = *const fn (f32) f32;Function pointer type for easing functions.
AnimationState
pub const AnimationState = enum {
idle,
running,
paused,
completed,
};Represents the current state of an animation.
Animation(T)
Generic animation struct for any numeric type T that supports linear interpolation.
Fields
start_value: T- The starting valueend_value: T- The ending valueduration_ms: u32- Duration in millisecondseasing: EasingFn- Easing function (default: linear)progress: f32- Current progress (0.0 to 1.0)elapsed_ms: u32- Elapsed time in millisecondsstate: AnimationState- Current animation stateloop_count: u32- Number of loops (0 = infinite)current_loop: u32- Current loop iterationalternate: bool- Whether to reverse direction on alternate loopson_complete: ?*const fn () void- Completion callback
Methods
init
pub fn init(start_val: T, end_val: T, duration_ms: u32) SelfCreates a new animation instance.
Parameters:
start_val: T- Starting valueend_val: T- Ending valueduration_ms: u32- Duration in milliseconds
Returns: New Animation instance
withEasing
pub fn withEasing(self: Self, easing: EasingFn) SelfSets the easing function.
Parameters:
easing: EasingFn- Easing function to use
Returns: Modified Animation instance
loop
pub fn loop(self: Self, count: u32) SelfSets the loop count.
Parameters:
count: u32- Number of times to loop (0 = infinite)
Returns: Modified Animation instance
loopForever
pub fn loopForever(self: Self) SelfEnables infinite looping.
Returns: Modified Animation instance
withAlternate
pub fn withAlternate(self: Self) SelfEnables alternating (ping-pong) animation.
Returns: Modified Animation instance
start
pub fn start(self: *Self) voidStarts the animation.
pause
pub fn pause(self: *Self) voidPauses the animation if running.
resumeAnimation
pub fn resumeAnimation(self: *Self) voidResumes the animation if paused.
reset
pub fn reset(self: *Self) voidResets the animation to initial state.
update
pub fn update(self: *Self, delta_ms: u32) voidUpdates the animation state.
Parameters:
delta_ms: u32- Time elapsed since last update in milliseconds
getValue
pub fn getValue(self: *Self) TGets the current interpolated value.
Returns: Current animation value
FpsCounter
Utility for tracking frames per second.
Fields
frame_times: [60]u32- Ring buffer of frame timesindex: usize- Current buffer indextotal_time: u32- Sum of all frame times
Methods
init
pub fn init() FpsCounterCreates a new FPS counter initialized with 60 FPS.
Returns: New FpsCounter instance
update
pub fn update(self: *FpsCounter, delta_ms: u32) voidUpdates the FPS counter with a new frame time.
Parameters:
delta_ms: u32- Time taken for the last frame in milliseconds
getFps
pub fn getFps(self: *FpsCounter) u32Calculates the current FPS based on recent frame times.
Returns: Current FPS value
Usage Examples
Basic Animation
const anim = Animation(f32).init(0.0, 100.0, 1000)
.withEasing(&Easing.easeOutCubic);
// Start the animation
anim.start();
// In your update loop
anim.update(delta_ms);
const current_value = anim.getValue();Looping Animation
const anim = Animation(f32).init(0.0, 1.0, 500)
.withEasing(&Easing.easeInOutSine)
.loop(3)
.withAlternate();FPS Monitoring
var fps_counter = FpsCounter.init();
// In your render loop
fps_counter.update(delta_ms);
const fps = fps_counter.getFps();See Also
- Animation Guide
- Source:
src/animation/animation.zig