1个稳定版本

1.0.0 2020年1月4日

#32#rectangle

Download history 52/week @ 2024-03-11 74/week @ 2024-03-18 118/week @ 2024-03-25 112/week @ 2024-04-01 97/week @ 2024-04-08 56/week @ 2024-04-15 68/week @ 2024-04-22 59/week @ 2024-04-29 112/week @ 2024-05-06 49/week @ 2024-05-13 60/week @ 2024-05-20 51/week @ 2024-05-27 44/week @ 2024-06-03 57/week @ 2024-06-10 50/week @ 2024-06-17 48/week @ 2024-06-24

202 每月下载量
6 crates 中使用

MIT 协议

3KB

keyframe

Rust中动画的简单库

Crate Downloads Documentation License

特性

  • 包括用户自定义贝塞尔曲线(如CSS中的 cubic-bezier)和可关键帧曲线在内的多种缓动函数
  • CSS中的 @keyframes 动画序列
  • 为2D/3D/4D支持(点、矩形、颜色等)集成了 mint

用法

使用 keyframe::ease(function, from, to, time) 实现两个值之间的缓动。 fromto 可以是任何实现了 CanTween 的类型,如 f64mint::Vector2,而 time 需要是一个在零到一之间的浮点数。 function 指定了 fromto 之间的转换,并且是实现了 EasingFunction 的任何类型。

可以使用 keyframe::AnimationSequence 创建更复杂的动画,并跟踪关键帧、时间等。您可以使用 keyframes![...] 宏、迭代器或向量来创建动画序列。

示例

示例可视化工具包含在 examples/ 目录中。运行 cargo run --example visualizer --release 命令以启动它。(ggez 在调试模式下运行速度非常慢!)

缓动动画

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

动画序列

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

自定义结构

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

依赖项

~1.5MB
~35K SLoC