Expand description

Bevy Easings

MIT/Apache 2.0 Doc Crate Bevy Tracking CI

Easings on Bevy components using interpolation.

Usage

System setup

Add the plugin to your app:

use bevy::prelude::*;
use bevy_easings::EasingsPlugin;

fn main() {
    App::new()
        .add_plugin(EasingsPlugin);
}

Easing a component to a new value

And then just ease your components to their new state!

use bevy::prelude::*;
use bevy_easings::*;

fn my_system(mut commands: Commands){
    commands
        .spawn_bundle(SpriteBundle {
            ..Default::default()
        })
        .insert(
            Sprite {
                custom_size: Some(Vec2::new(10., 10.)),
                ..Default::default()
            }
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(100., 100.)),
                    ..Default::default()
                },
                EaseFunction::QuadraticIn,
                EasingType::PingPong {
                    duration: std::time::Duration::from_secs(1),
                    pause: Some(std::time::Duration::from_millis(500)),
                },
            ),
        );
}

If the component being eased is not already a component of the entity, the component should first be inserted for the target entity.

Chaining easing

You can chain easings, if they are not set to repeat they will happen in sequence.

use bevy::prelude::*;
use bevy_easings::*;

fn my_system(mut commands: Commands){
    commands
        .spawn_bundle(SpriteBundle {
            ..Default::default()
        })
        .insert(
            Sprite {
                custom_size: Some(Vec2::new(10., 10.)),
                ..Default::default()
            }
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(300., 300.)),
                    ..Default::default()
                },
                EaseFunction::QuadraticIn,
                EasingType::Once {
                    duration: std::time::Duration::from_secs(1),
                },
            )
            .ease_to(
                Sprite {
                    custom_size: Some(Vec2::new(350., 350.)),
                    ..Default::default()
                },
                EaseFunction::QuadraticIn,
                EasingType::PingPong {
                    duration: std::time::Duration::from_millis(500),
                    pause: Some(std::time::Duration::from_millis(200)),
                },
            ),
        );
}

Bundle Supported

Custom component support

To be able to ease a component, it needs to implement the traits Default and Lerp. This trait is re-exported by beavy_easings.

use bevy::prelude::*;
use bevy_easings::*;

#[derive(Default, Component)]
struct CustomComponent(f32);
impl Lerp for CustomComponent {
    type Scalar = f32;

    fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self {
        CustomComponent(interpolation::lerp(&self.0, &other.0, scalar))
    }
}

The basic formula for lerp (linear interpolation) is self + (other - self) * scalar.

Then, the system custom_ease_system::<CustomComponent>.system() needs to be added to the application.

Examples

See examples

sprite_color

sprite_size

transform_rotation

transform_translation

Ease Functions

Many ease functions are available:

  • QuadraticIn
  • QuadraticOut
  • QuadraticInOut
  • CubicIn
  • CubicOut
  • CubicInOut
  • QuarticIn
  • QuarticOut
  • QuarticInOut
  • QuinticIn
  • QuinticOut
  • QuinticInOut
  • SineIn
  • SineOut
  • SineInOut
  • CircularIn
  • CircularOut
  • CircularInOut
  • ExponentialIn
  • ExponentialOut
  • ExponentialInOut
  • ElasticIn
  • ElasticOut
  • ElasticInOut
  • BackIn
  • BackOut
  • BackInOut
  • BounceIn
  • BounceOut
  • BounceInOut
Bevybevy_ninepatch
mainmain
0.60.5
0.50.4

Structs

Wrapper around a type that can be eased.

Component to control a chain of easing

Component to control an easing

Plugin to add systems related to easing

Enums

Describe how eased value should be computed

Control if an easing is played

How should this easing loop repeat

Traits

Trait to mark custom component that can be eased. It will be automatically implemented if the custom component implement Lerp

Trait marking components that can be eased

Describes a type that can linearly interpolate between two points.

Functions

Ease system for custom component. Add this system to your application with your component as a type parameter.