Expand description

A simple library for animation in Rust

Usage

Tweening between two values is done with ease(function, from, to, time). from and to can be any type that implements CanTween, such as f64 or mint::Vector2, while time needs to be a floating-point value between zero and one. function specifies the transition between from and to and is any type that implements EasingFunction.

AnimationSequence can be used to create more complex animations that keep track of keyframes, time, etc. You can create animation sequences with the keyframes![...] macro, from an iterator or from a vector.

Embedded

This library is embedded compatible. As default it uses the alloc crate for dynamic memory usage. So, if the alloc feature is turned off, by setting default-features = false, this crate can be used without dynamic memory usage. Keep in mind that this will disable the feature mint_types as well. This must be enabled by hand again.

Disabled features:

Examples

An example visualizer is included in examples/. Run cargo run --example visualizer --release to start it. (ggez is really slow in debug mode!)

Tweening:

use keyframe::{ease, functions::EaseInOut};

fn example() -> f64 {
    let a = 0.0;
    let b = 2.0;
    let time = 0.5;

    ease(EaseInOut, a, b, time)
}

Animation sequences:

use keyframe::{keyframes, Keyframe, AnimationSequence, functions::Linear};

fn example() {
   // (value, time) or (value, time, function)
   let mut sequence = keyframes![
       (0.5, 0.0), // <-- EaseInOut used from 0.0 to 0.3
       (1.5, 0.3, Linear), // <-- Linear used from 0.3 to 1.0
       (2.5, 1.0) // <-- Easing function here is never used, since we're at the end
   ];

   sequence.advance_by(0.65);

   assert_eq!(sequence.now(), 2.0);
   assert_eq!(sequence.duration(), 1.0);
}

Custom structures:

use keyframe::mint::Point2;
// This macro works with any structure as long as it only consists of types that implement "CanTween"
use keyframe_derive::CanTween;

#[derive(CanTween)]
struct MySubStructure {
    a: f32
}

#[derive(CanTween)]
struct MyStructure {
    a: f64,
    b: Point2<f64>,
    c: f32,
    d: [MySubStructure; N] // Array length matching is guaranteed by the type system
}

// Also works with unnamed structures
#[derive(CanTween)]
struct UnnamedStructure(MyStructure, f64);

Re-exports

pub use mint;
pub use num_traits;

Modules

Definitions for various easing functions

Macros

Creates an animation sequence containing any arguments that can be made into keyframes

Structs

A collection of keyframes that can be played back in sequence

Intermediate step in an animation sequence

Enums

Category of animation sequence error

Traits

Type that can be used with an easing function

Implementation of a 2D curve function for easing between two points

Functions

Returns the value at a specified X position on the curve between point A and point B. Time is limited to a range between 0.0 and 1.0.

Returns the value at a specified X position on the curve between point A and point B. Time is limited to a range between 0.0 and max_time.

Returns the value at a specified X position on the curve between point A and point B. The time argument is expected to stay within a range of 0.0 to 1.0 but bounds checking is not enforced.