1个稳定版本
1.0.0 | 2020年1月4日 |
---|
#32 在 #rectangle
202 每月下载量
在 6 crates 中使用
3KB
keyframe
Rust中动画的简单库
特性
- 包括用户自定义贝塞尔曲线(如CSS中的 cubic-bezier)和可关键帧曲线在内的多种缓动函数
- CSS中的 @keyframes 动画序列
- 为2D/3D/4D支持(点、矩形、颜色等)集成了 mint
用法
使用 keyframe::ease(function, from, to, time)
实现两个值之间的缓动。 from
和 to
可以是任何实现了 CanTween
的类型,如 f64
或 mint::Vector2
,而 time
需要是一个在零到一之间的浮点数。 function
指定了 from
和 to
之间的转换,并且是实现了 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