6 stable releases

1.1.1 Jul 11, 2022
1.1.0 Jul 6, 2022
1.0.4 Aug 20, 2021
1.0.3 Jul 5, 2020
1.0.2 Jan 4, 2020

#121 in Game dev

Download history 4597/week @ 2025-09-29 4382/week @ 2025-10-06 5180/week @ 2025-10-13 4532/week @ 2025-10-20 4430/week @ 2025-10-27 4436/week @ 2025-11-03 4511/week @ 2025-11-10 5576/week @ 2025-11-17 4773/week @ 2025-11-24 4429/week @ 2025-12-01 3943/week @ 2025-12-08 4758/week @ 2025-12-15 3454/week @ 2025-12-22 3148/week @ 2025-12-29 4312/week @ 2026-01-05 4205/week @ 2026-01-12

16,470 downloads per month
Used in 12 crates (11 directly)

MIT license

43KB
882 lines

keyframe

A simple library for animation in Rust

Crate Downloads Documentation License

Features

  • Several easing functions, including user-defined Bézier curves (like CSS cubic-bezier) and keyframable curves
  • Animation sequences (like CSS @keyframes)
  • mint integration for 2D/3D/4D support (points, rectangles, colors, etc)

Usage

Tweening between two values is done with keyframe::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.

keyframe::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.

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};

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);

Dependencies

~705KB
~14K SLoC