Tweening animation plugin for Bevy.
Add the tweening plugin to your app:
rust
App::default()
.add_default_plugins()
.add_plugin(TweeningPlugin)
.run();
Animate the transform position of an entity:
rust
commands
// Spawn a Sprite entity to animate the position of
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::RED,
custom_size: Some(Vec2::new(size, size)),
..Default::default()
},
..Default::default()
})
// Add an Animator component to perform the animation
.insert(Animator::new(
// Use a quadratic easing on both endpoints
EaseFunction::QuadraticInOut,
// Loop animation back and forth over 1 second, with a 0.5 second
// pause after each cycle (start -> end -> start).
TweeningType::PingPong {
duration: Duration::from_secs(1),
pause: Some(Duration::from_millis(500)),
},
// The lens gives access to the Transform component of the Sprite,
// for the Animator to animate it. It also contains the start and
// end values associated with the animation ratios 0. and 1.
TransformPositionLens {
start: Vec3::new(0., 0., 0.),
end: Vec3::new(1., 2., -4.),
},
));
The naming scheme for predefined lenses is "<TargetName><FieldName>Lens", where <TargetName> is the name of the target component or asset which is queried, and <FieldName> is the field which is mutated in place.
| Target Component | Animated Field | Lens |
|---|---|---|
| Sprite | color | SpriteColorLens |
| Transform | translation | TransformPositionLens |
| | rotation | TransformRotationLens |
| | scale | TransformScaleLens |
| Style | position | UiPositionLens |
| Text | TextStyle::color | TextColorLens |
| Target Asset | Animated Field | Lens |
|---|---|---|
| ColorMaterial | color | ColorMaterialColorLens |
To be able to animate some fields of a custom component, a custom lens need to be implemented for that component, which linearly interpolates the field(s) of that component.
```rust
struct CustomComponent(f32);
struct CustomLens { start: f32, end: f32, }
impl Lens
This process can also be used to interpolate fields of existing Bevy built-in components for which a predfined lens is not provided.
The basic formula for lerp (linear interpolation) is either of:
start + (end - start) * scalarstart * (1.0 - scalar) + end * scalarThe two formulations are mathematically equivalent, but one may be more suited than the other depending on the type interpolated and the operations available, and the potential floating-point precision errors.
Then, the system component_animator_system::<CustomComponent> needs to be added to the application.
The process is similar to custom components, creating a custom lens for the custom asset. The system to add is asset_animator_system::<CustomAsset>.
See the examples/ folder.
sprite_colorrust
cargo run --example sprite_color --features="bevy/bevy_winit"
![]()
transform_rotationrust
cargo run --example transform_rotation --features="bevy/bevy_winit"

transform_translationrust
cargo run --example transform_translation --features="bevy/bevy_winit"

colormaterial_colorrust
cargo run --example colormaterial_color --features="bevy/bevy_winit"

Many ease functions are available:
bevy_easingsThe bevy_tweening library started as a fork of the bevy_easings library by François Mocker, with the goals to:
bevy_easings does, and instead mutate the assets (and, by similarity, the components too) in-place without making a copy. The in-place mutation also allows a more optimal interpolation limited to modifying the fields of interest only, instead of creating a new copy of the entire component each tick.