Skip to main content

Crate ranim_anims

Crate ranim_anims 

Source
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 {
    type Output;

    /// Evaluates at the given progress value `alpha` in range [0, 1].
    fn eval_alpha(&self, alpha: f64) -> Self::Output;
}

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 for FadeIn<T> {
    type Output = T;

    fn eval_alpha(&self, alpha: f64) -> Self::Output {
        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) -> FadeIn<T>;
    fn fade_out(self) -> FadeOut<T>;
}

impl<T: FadingRequirement + 'static> FadingAnim<T> for T {
    fn fade_in(self) -> FadeIn<T> {
        FadeIn::new(self.clone())
    }
    fn fade_out(self) -> FadeOut<T> {
        FadeOut::new(self.clone())
    }
}

Modules§

creation
Creation animation
fading
Fading animation
func
Func animation
lagged
Lagged animation
morph
Morph animation
rotating
Rotating animation