Skip to content

Tweens ​

Tweens animate any numeric property over time.

ts
import { TweenTo } from 'bare-engine';

TweenTo(player, 0.5, { x: 300, rotation: 90 });

TweenTo(object, seconds, props) is shorthand for bare.tween.To(...). Both return a tween with a promise, so you can wait for it:

ts
await TweenTo(door, 1, { y: 0 }).promise;

WARNING

Tweens only run once the scene has started, and a scene starts after its OnStart has finished. So don't await a tween inside a scene's OnStart: it would wait forever. Start the tween there without awaiting it, or await it later, for example from OnUpdate or an event. During development, reading .promise before the scene has started logs an error.

Example: a Platformer coin waits for its fade-out tween before removing itself.

Options ​

Add these to props next to the values you animate:

OptionDescription
easeAn easing function, for example Easing.Back.Out. Default Easing.Quadratic.Out.
delaySeconds to wait before starting. With repeat or yoyo, it also waits before every repeat.
initialDelaySeconds to wait once, before the first pass only. Repeats then wait delay, which is 0 if not set. Useful with yoyo.
yoyoPlay back and forth.
repeatNumber of repeats, -1 for forever.
onStart, onUpdate, onComplete, onRepeat, onStopCallbacks.
onEveryStartCallback at the start of every pass, including repeats.
persistentKeep the tween when the scene is cleared.
ts
import { Easing, TweenTo } from 'bare-engine';

TweenTo(coin, 0.4, { innerY: '+=10', ease: Easing.Sinusoidal.InOut, yoyo: true, repeat: -1 });

Relative values like '+=10' and '-=10' add to the current value.

By default, a new tween on the same object and property replaces the old one. Pass false as the fourth argument to keep both.

Example: the Showcase's tweens demo shows pulsing, spinning, bouncing, fading and swaying.

bare.tween ​

MethodDescription
To(object, seconds, props, overwrite?)Start a tween.
Shake(object, property, duration, …)Shake a property and settle back to 0. See Shake.
SetTimer(seconds, onComplete)Call a function after a delay.
AddChain(...tweens)Play tweens one after another.
GetAllFrom(object)All tweens running on an object.
RemoveAllFrom(object)Stop all tweens on an object.
FinishAllFrom(object) / FinishAll()Jump tweens to their end.
RemoveTimers()Cancel all timers.

Example: the RPG's weapon checks GetAllFrom so it can't slash again mid-swing.

Shake ​

Shake sets a property to a starting value, then springs it back to 0 with a wobble. It suits properties that rest at 0, like innerRotation or an innerPosition axis:

ts
bare.tween.Shake(enemy, 'innerRotation', 0.5, 20);   // a quick wiggle
ParameterDefaultDescription
object, propertyWhat to shake.
durationSeconds until it's back at 0.
initialMove-1The value it starts from.
decay6How fast the wobble dies down.
frequency4How many times it wobbles.
phase0.075Shifts the wobble in time.
amplitude40How far it overshoots.
overwritetrueReplace other tweens on the same property.

Example: the Showcase shakes a heart with Shake(heart.innerPosition, "y", 1.2, -0.5, 3, 4, 0.075, 30).

Bare Engine is open source under the MIT license.