Expand description
Ranim’s built-in animations
This crate contains the built-in animations for Ranim.
An Animation in ranim is basically a struct that implements the ranim_core::animation::Eval trait:
ⓘ
pub trait Eval<T> {
/// Evaluates at the given progress value `alpha` in range [0, 1].
fn eval_alpha(&self, alpha: f64) -> T;
}Every animation self-contains the evaluation process (the trait impl of ranim_core::animation::Eval::eval_alpha)
and the data that the evaluation process needs (the struct it self). Here is the example of fading::FadeIn animation:
ⓘ
pub trait FadingRequirement: Opacity + Interpolatable + Clone {}
impl<T: Opacity + Interpolatable + Clone> FadingRequirement for T {}
pub struct FadeIn<T: FadingRequirement> {
src: T,
dst: T,
}
impl<T: FadingRequirement> FadeIn<T> {
pub fn new(target: T) -> Self {
let mut src = target.clone();
let dst = target.clone();
src.set_opacity(0.0);
Self { src, dst }
}
}
impl<T: FadingRequirement> Eval<T> for FadeIn<T> {
fn eval_alpha(&self, alpha: f64) -> T {
self.src.lerp(&self.dst, alpha)
}
}In addition, to make the construction of anim for any type that satisfies the requirement, It is recommended to write a trait like this:
ⓘ
/// The methods to create animations for `T` that satisfies [`FadingRequirement`]
pub trait FadingAnim<T: FadingRequirement + 'static> {
fn fade_in(self) -> AnimationCell<T>;
fn fade_out(self) -> AnimationCell<T>;
}
impl<T: FadingRequirement + 'static> FadingAnim<T> for T {
fn fade_in(self) -> AnimationSpan<T> {
FadeIn::new(self.clone())
.into_animation_cell()
.with_rate_func(smooth)
}
fn fade_out(self) -> AnimationSpan<T> {
FadeOut::new(self.clone())
.into_animation_cell()
.with_rate_func(smooth)
}
}