motion-canvas-rs 0.2.4

A high-performance vector animation engine inspired by Motion Canvas, built on Vello and Typst.
Documentation
use crate::core::animation::base::Animation;
use crate::core::animation::AnyAnimation;
use std::time::Duration;

/// An animation that simply waits for a duration.
///
/// `Wait` is a no-op animation that is useful for creating pauses between
/// other animations in a [`Chain`](crate::flows::chain::Chain) or for
/// defining relative start times in an [`Any`](crate::flows::any::Any).
pub struct Wait {
    pub(crate) duration: Duration,
    pub(crate) elapsed: Duration,
}

impl Animation for Wait {
    /// Increments the internal elapsed timer.
    /// Returns `true` if the elapsed time reaches the target duration.
    fn update(&mut self, dt: Duration) -> (bool, Duration) {
        self.elapsed += dt;
        let finished = self.elapsed >= self.duration;
        let leftover = self.elapsed.saturating_sub(self.duration);
        (finished, leftover)
    }

    /// Returns the fixed wait duration.
    fn duration(&self) -> Duration {
        self.duration
    }

    /// Resets the elapsed timer.
    fn reset(&mut self) {
        self.elapsed = Duration::ZERO;
    }
}

/// Creates an animation that waits for a duration.
///
/// Generally used directly as `wait(dur)` in video timelines.
///
/// ### Example
/// ```rust
/// # use motion_canvas_rs::prelude::*;
/// # use std::time::Duration;
/// # let node = Rect::default().with_size(Vec2::new(100.0, 100.0)).with_fill(Color::RED);
/// # let target = Vec2::new(100.0, 100.0);
/// # let dur = Duration::from_secs(1);
/// chain![
///     node.position.to(target, dur),
///     wait(Duration::from_secs(1)), // Pause for 1s
///     node.opacity.to(0.0, dur),
/// ];
/// ```
pub fn wait(duration: Duration) -> AnyAnimation {
    AnyAnimation::Wait(Wait {
        duration,
        elapsed: Duration::ZERO,
    })
}