0:01 / 0:30
Playback speed

Tween

Pause halfway between the dashed squares. Besides position, which properties are halfway through a change?

#MotionAndFeel

Animating a value from a start to an end over a set time.

You give the two ends and a duration; the game fills in everything in between. Here one tween drives position, size, rotation and colour at once. It’s how menus slide and pickups pop without anyone drawing frames.

Code

src/demos/tween.goose
namespace tween_lesson;
import stage;
import std;
import math;

export "tween" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    artwork("draft");
    let raw = clamp((time % 3.2) / 2.2, 0.0, 1.0);
    let u = ease_in_out(raw);
    save();
    translate(28, 58);
    dash_box(-6, -6, 12, 12, LINE, 2);
    restore();
    save();
    translate(124, 36);
    rotate(PI / 2);
    dash_box(-15, -15, 30, 30, LINE, 2);
    restore();
    mix_square(mix_value(28, 124, u), mix_value(58, 36, u), mix_value(12, 30, u), u * PI / 2, u);
    rect(24, 88, 112, 1, LINE);
    rect(22, 85, 4, 7, WHITE);
    rect(134, 85, 4, 7, WHITE);
    rect(23 + raw * 112, 83, 2, 11, MAG);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.