Skip to content

ui::SpringAnimation

Header: winui/ui/Animation.hpp.

A spring: a damped harmonic oscillator pulled to final_value (on a scalar property, final_value.x). period is the undamped period, damping_ratio 0..1 from undamped to critically damped (above 1 overdamped). It starts from the property's current value. When it replaces a running spring it also takes that spring's velocity, so a hover spring hands off smoothly.

Example:

// Hover grow: spring to 1.1 on enter, back to 1 on exit.
auto               stack  = std::make_unique<ui::StackControl>(ui::Orientation::Vertical);
ui::ButtonControl* button = stack->add(std::make_unique<ui::ButtonControl>("Hover me"));
button->on_pointer_entered([button] {
    button->start_animation(ui::AnimatedProperty::Scale,
                            ui::SpringAnimation{.final_value = ui::Vector3{1.1f, 1.1f, 1.0f}});
});
button->on_pointer_exited([button] {
    button->start_animation(ui::AnimatedProperty::Scale,
                            ui::SpringAnimation{.final_value = ui::Vector3{1.0f, 1.0f, 1.0f}});
});

Field Type Default
final_value Vector3 {1.0f, 1.0f, 1.0f} The rest value the spring pulls to; a scalar property uses .x.
damping_ratio float 0.6f 0 never settles, 1 is critically damped, above 1 is overdamped.
period Duration {50.0} Undamped oscillation period; shorter is stiffer.
initial_value std::optional<Vector3> Overrides the start value; unset starts from the property's current value.
initial_velocity std::optional<Vector3> Overrides the start velocity (units per second); unset inherits a replaced spring's.
on_completed Callback<void()> Fires once the spring has settled on final_value.