Skip to content

Tween

Tween example showing smooth animation

Overview

Tween is a generic struct that handles smooth transitions between values over time.

Usage

Create a new tween

// Create a new tween starting at 0.0
let mut tween = Tween::new(0.0);

Set a target

// Tween will transition from its current value to 100.0
tween.set(100.0);

Set duration

// Tween will complete in 60 ticks
tween.set_duration(60);

Set easing

// Use quadratic easing
tween.set_ease(Easing::EaseInQuad);

Get current value of tween

// Get the current value of the tween
let current = tween.get();

Check if complete

if tween.done() {
    // Tween has finished
}

Add to target

// Add 10 to the end value
tween.add(10.0);

Complete Example

Create a tween that moves across the screen horizontally.

use turbo::*;
 
#[turbo::game]
struct GameState {
    position: Tween<f32>,
}
impl GameState{
    fn new() -> Self {
        Self {
            position: Tween::new(0.0) // Start value
                .duration(120) // Duration in frames
                .ease(Easing::EaseInOutQuad) // Easing type
                .set(220.0) // End value
        }
    }
    fn update(&mut self) {
        // Get the current value of the tween
        let val = self.position.get();
        // Draw a circle using that value
        circ!(x = val, y = 72, d=8, color = 0x32CD32ff);
    }
}

Tween example showing smooth animation