A simple, generic, and flexible keyframe animation library for Rust.
KramaFrame provides a straightforward way to manage and update multiple animations, map their progress to value ranges, and apply various easing functions for smooth and dynamic transitions.
Features
- Manage multiple animations using string-based class names and numeric IDs.
- A rich set of built-in easing functions:
LinearEaseIn,EaseOut,EaseInOutSteps(n)for stepped animations- Custom
CubicBeziercurves
- Generic over time (
TRES) and progress (PRES) data types for maximum flexibility. - Easily map animation progress (from 0.0 to 1.0) to any numerical output range.
- Play animations forwards or in reverse.
- Update all active animations with a single call in your game or application loop.
Getting Started
Add kramaframe to your project's Cargo.toml:
[]
= "0.2.*"
Or, add it via the command line:
Usage
Here's a basic example of animating a value from 80 to 100 over a set duration.
use ;
Easing Functions (KeyFrameFunction)
KramaFrame uses KeyFrameFunction to control the rate of change of an animation, allowing for more natural and interesting motion.
Available Functions
KeyFrameFunction::Linear: Constant speed.KeyFrameFunction::EaseIn: Starts slow, then accelerates.KeyFrameFunction::EaseOut: Starts fast, then decelerates.KeyFrameFunction::EaseInOut: Starts and ends slow, with acceleration in the middle.KeyFrameFunction::Steps(n): Divides the animation intondiscrete, instantaneous steps.KeyFrameFunction::new_cubic_bezier_f32(x1, y1, x2, y2): Defines a custom animation curve using Cubic Bezier control points, similar to CSScubic-bezier().
Example
You can define multiple animation classes, each with its own easing function.
use ;
let mut kramaframe = default;
// Define different animation classes
kramaframe.extend_iter_classlist;
// Create instances for each animation class with a duration of 1.0
kramaframe.insert_new_id;
kramaframe.insert_new_id;
// ... and so on for the other classes
// In your update loop, you can get values for each:
kramaframe.update_progress; // ~60 FPS
let linear_val = kramaframe.get_value_byrange;
let easein_val = kramaframe.get_value_byrange;
println!;
Macros
KramaFrame provides convenient macros for initializing animation sets using a concise syntax.
ukramaframe! (Stack-allocated)
The ukramaframe! macro is designed for no_std environments or scenarios where you want stack-allocated storage (using UClassList and UFrameList). It requires explicit type parameters for Time Resolution (TRES), Progress (PRES), and ID type.
use ;
// Syntax: <TRES, PRES, ID_TYPE> "class" Function [ID1, ID2] Duration Unit;
let mut krama = ukramaframe!;
// Start an animation
krama.restart_progress;
btkramaframe! (Heap-allocated)
The btkramaframe! macro creates a KramaFrame using BTreeMap for its internal storage. This is the preferred choice for general-purpose applications using std or alloc where the number of animations may vary.
use btkramaframe;
// Uses default BTreeMap storage
let mut krama = btkramaframe!;
// Update and retrieve value
krama.update_progress; // 16ms delta
let val = krama.get_value_byrange;
More Examples
You can find more detailed examples in the /examples directory of the repository:
iterrange.rs: A complete, runnable version of the basic usage example.allkeyframe.rs: Demonstrates and visualizes all availableKeyFrameFunctiontypes.reverse.rs: Demonstrates how to reverse the direction of an animation instance.reverserange.rs: Demonstrates how to reverse the range of an animation instance.generic.rs: Demonstrates how to use generic types withKramaFrame.
Special Example
tuiexample.rs: Demonstrates how to useKramaFramewith a TUI (Text User Interface) library.
https://github.com/user-attachments/assets/965e693f-8cdc-4165-81b5-4c0ef38c1f4a
kramaviz.rs: A higly efficient Visualizer sync with fluid animation. This Visualizer use less CPU and less memory than cava. where cava randomly flicker whereas kramaviz won't.
https://github.com/user-attachments/assets/f2d1cea2-bc01-4e55-860c-7179a6c4351a
License
This project is licensed under
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
If you are interested in adding a special or useful feature, please feel free to open an Issue or Pull Request!