Tutorial

Tutorial 4: Animation

Chapter 4 of 5. The complete example is on this page; every line of code compiles as shown.

Declare the target value in the screen and attach a transition to a stable element ID. Toggling expanded changes the card's scale from 0.8 to 1.0; .animate(Prop::Scale, ...) interpolates the visual transform over 300 ms. It does not change the card's layout allocation.

The App's animation clock is explicit. A native host advances it as events and frames arrive. A scripted host calls advance(ms), which is why the headless path can save the same points in the transition without sleeping. The --reduced-motion flag sets the theme's reduced-motion policy, so the same declared transition becomes immediate.

Complete example

Source: tutorial_animation.rs native/ui-host/examples/tutorial_animation.rs.

Rust
//! Run from native/: cargo run --locked -p aster-ui-host --example tutorial_animation
use aster_blueprint::ui::prelude::*;
use aster_native::{Options, RunWindow};

fn screen(s: &Store) -> El {
    col([
        text("Make room for motion").style(Type::Headline),
        switch(s, "expanded", "Expand the card"),
        card([
            text("A moving card").style(Type::Title),
            text("The layout stays put; the visual scale changes.").wrap_fit(),
        ]).id("motion-card")
            .scale(if s.bool("expanded") { 1.0 } else { 0.8 })
            .animate(Prop::Scale, Transition::ms(300, Ease::Linear)),
    ]).gap(5).pad(6)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reduced = std::env::args().any(|a| a == "--reduced-motion");
    let theme = Theme::paper_dark().with_reduced_motion(reduced);
    let mut app = App::new(&theme, Store::new().with("expanded", false))
        .size(680, 400).screen(screen)?;
    if std::env::args().any(|a| a == "--headless") {
        app.write_png("tutorial_animation_before.png")?;
        app.set("expanded", true)?;
        app.advance(150)?;
        app.write_png("tutorial_animation_midway.png")?;
        app.advance(150)?;
        app.write_png("tutorial_animation_after.png")?;
        return Ok(());
    }
    app.run_window_with(Options {
        title: "Aster motion".into(), ..Options::default()
    }.from_env())
}

Run it

From the repository root:

Shell
cd native
cargo check --locked -p aster-ui-host --example tutorial_animation
cargo run --locked -p aster-ui-host --example tutorial_animation

Without a desktop display, run the image-producing path instead:

Shell
cargo run --locked -p aster-ui-host --example tutorial_animation -- --headless

PNG files are written in the command's current directory. For the animation example, append --reduced-motion after -- to compare the reduced-motion policy.

What you will see

Toggle “Expand the card”. Its contents grow or shrink within the same layout space. In headless mode, three files record the before, midway and after states. With reduced motion the intermediate image already has the target state.

Next steps

Try Prop::Opacity on a separate labelled element, or theme motion tokens instead of a literal duration. Keep keyboard focus independent of decorative motion. Next, render a Rust scene on the web.