Skip to content

Animation

JOID animates on its own clock, so an animation takes the same time at 30 or at 240 frames per second. It offers a few tools, each made for one kind of motion. This page helps you pick the right one, then shows how to use it.

You will learn

  • Which tool fits which motion
  • How to animate a value with an animator, and shape it with easing
  • How to chain, repeat and replay an animation
  • How to follow a moving target
  • How to animate values of your own with the tween engine

Start from what you want to see:

You wantUse
A node that changes while the mouse is over itThe hover progress of the node (Hover, tooltips and cursors)
A value that goes from A to B: once, in a loop, or when the user clicksA TweenAnimator (Animate a value)
A value that chases a target that keeps moving, such as the mouseUI.lerpByFramerate (Follow a moving target)
Several values of your own, in timelines with pauses and listenersThe tween engine: Tween, Timeline, TweenManager (Animate values of your own)
A UI that animates when it opens or closesA transition

Every tool produces a number; a node turns it into motion by reading it in a setter.

A TweenAnimator moves one float over time:

final TweenAnimator appear = TweenAnimator.create(0F).sequence(600F, 1F, TweenEquations.CUBIC_OUT).start();
RectNode
.create(160, 0, 300, 180)
.color(AnimationUI.ACCENT.withAlpha(appear.get()))
.y(140D - 60D * appear.get())
.attach(this);

Full file: AnimationUI.java

The value goes from 0 to 1 in 600 ms; the opacity and the position read it.
  1. TweenAnimator.create(0F) starts the value at 0F.
  2. sequence(600F, 1F, ...) describes a step: reach 1F in 600 ms with the TweenEquations.CUBIC_OUT easing.
  3. start() starts it: JOID advances it on every frame until the step ends.

An animator is a signal of its own: get() reads its value, and an expression that reads it, such as y(140D - 60D * appear.get()), follows it. Prefer that expression to a lambda: JOID computes it only while the animator moves, where a lambda runs on every frame, even after the animation ends. It is read-only: set(...) throws, TweenAnimator.value puts a value at once and drops the steps, and TweenAnimator.clear stops it and puts it back to 0F.

An easing equation decides how the value travels from its start to its target. Each family has three variants: IN starts slowly, OUT ends softly and IN_OUT does both. The catalog draws the curves of every family.

FeelEquation
Constant speed, for loops that never stopTweenEquations.LINEAR, the easing of a step without one
Fast start, soft landing, for what appears or reacts to the userQUAD_OUT, CUBIC_OUT, QUART_OUT, from gentle to strong
Soft at both ends, for back and forth motionSINE_IN_OUT, QUAD_IN_OUT
Slow start, fast end, for what leaves the screenQUART_IN, CUBIC_IN
Overshoots the target, then settlesBACK_OUT
Springs or bounces around the targetELASTIC_OUT, BOUNCE_OUT

BackTweenEquation and ElasticTweenEquation take settings. This square overshoots its target on each trip, further than the default BACK_OUT:

final TweenAnimator pop = TweenAnimator
.create(0F)
.sequence(800F, 1F, TweenEquations.BACK_OUT.overshoot(3F))
.push(800F, 0F, TweenEquations.BACK_OUT.overshoot(3F))
.repeat(BaseTween.INFINITY, 400F)
.start();
RectNode
.create(160, 760, 520, 60)
.color(AnimationUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(AnimationUI.ELEMENT, 1.5F))
.body(track -> {
RectNode.create(10, 10, 40, 40).color(AnimationUI.ACCENT).x(110D + 260D * pop.get()).attach(track);
})
.onDetach(event -> pop.clear())
.attach(this);

Full file: AnimationUI.java

BACK_OUT.overshoot(3F) carries the square a quarter of the trip beyond each end before it settles.

overshoot(...) returns a changed copy and leaves TweenEquations.BACK_OUT as it is, and TweenEquations.ELASTIC_OUT.amplitude(...).period(...) changes the spring. TweenEquations.of finds an easing by its name, such as "Quad.OUT" or "Back.IN_OUT", to read it from a settings file; it returns null for an unknown name. The name of a curve is its toString(), settings included, such as "Back.OUT(2.5)", so a curve written to a file comes back the same. For any other curve, extend TweenEquation and return the eased progress from compute(t), where t goes from 0F to 1F.

push(...) adds a step after the previous one, and repeat(...) plays the whole chain again:

final TweenAnimator loop = TweenAnimator
.create(0F)
.sequence(600F, 1F, TweenEquations.QUAD_OUT)
.push(600F, 0F, TweenEquations.QUAD_IN)
.repeat(BaseTween.INFINITY, 200F)
.start();
RectNode
.create(620, 120, 320, 60)
.color(AnimationUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(AnimationUI.ELEMENT, 1.5F))
.body(track -> {
RectNode.create(10, 10, 40, 40).color(AnimationUI.ACCENT).x(10D + 260D * loop.get()).attach(track);
})
.onDetach(event -> loop.clear())
.attach(this);

Full file: AnimationUI.java

One start() loops the chain forever: repeat plays both steps again after each pause of 200 ms.
  • repeat(count, delay) plays the chain count more times, waiting delay ms between plays; repeatYoyo(count, delay) plays every second time backward.
  • BaseTween.INFINITY repeats forever.
  • delay(ms) waits before the first play, and speed(2F) plays twice as fast; speed(0F) holds the value where it is.
  • Set the repetitions before start(): a started animation refuses them with an IllegalStateException.

To play an animation on demand, call sequence(...) again: it replaces the running steps and starts from the current value, so a click in the middle of the motion turns it around smoothly.

final IntegerSignal plays = new IntegerSignal(0);
final TweenAnimator toggle = TweenAnimator.create(0F).onComplete(tween -> plays.increment());
RectNode
.create(1100, 120, 320, 60)
.color(AnimationUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(AnimationUI.ELEMENT, 1.5F))
.body(track -> {
RectNode.create(10, 10, 40, 40).color(AnimationUI.ACCENT).x(10D + 260D * toggle.get()).attach(track);
})
.attach(this);
RectNode
.create(1100, 220, 200, 60)
.color(AnimationUI.ACCENT)
.onClick(event -> {
final float target = toggle.peek() < 0.5F ? 1F : 0F;
toggle.sequence(400F, target, TweenEquations.QUART_OUT).start();
})
.label(Text.create("Toggle", label))
.attach(this);

Full file: AnimationUI.java

Each click starts a 400 ms step toward the other end, and onComplete counts the plays that reached their end.

onStart, onIterationEnd, onComplete and the other listeners of TweenAnimator belong to the animator and run for every sequence that follows. Give them once, where you create the animator: given in the onClick that starts a sequence, a listener adds up, and the tenth play runs it ten times. Once its steps end, the animator drops them: start() alone then throws an IllegalStateException until the next sequence(...).

Two values that move apart need two animators, each read by its own setter. To update several properties from one animator, animate(animator) on a node calls Node.onAnimate on each frame where the value changed:

node.animate(slide).onAnimate(event -> event.getNode().x(260D * event.getValue()).y(40D * event.getValue()))

An animation with a fixed duration fits a target that stays put. When the target keeps moving, move the value a share of the remaining distance on each frame with UI.lerpByFramerate, in the update() of the UI:

private double followX = 160D;

Full file: AnimationUI.java

@Override
public void update() {
final double target = Math.max(160D, Math.min(920D, super.getMouseX() - 40D));
this.followX = super.lerpByFramerate(this.followX, target, 0.1D, 0.5D);
}

Full file: AnimationUI.java

CircleNode.create(0, 520, 80).color(AnimationUI.ACCENT).x(() -> this.followX).attach(this);

Full file: AnimationUI.java

The disc covers a share of the distance to the pointer on every frame, so it slows down as it arrives.

lerpByFramerate(value, target, speed, snapDiff) returns the next value. speed is the share of the distance covered in one frame at 60 frames per second, from 0 to 1; JOID scales it by the real frame time, so the motion keeps its pace at any frame rate. Once the distance falls to snapDiff or under, the value jumps to the target.

TweenAnimator is built on a tween engine that you can use directly, to animate any float you can read and write. A Tween takes a getter, a setter and a duration, a Timeline arranges tweens, and a TweenManager advances them when you call its update(...):

private final TweenManager manager = new TweenManager();
private float x;
private float y;
private long last;

Full file: TweenUI.java

@Override
public void update() {
final long now = BridgeHandler.CLOCK.get().currentTimeMillis();
this.manager.update(now - this.last);
this.last = now;
}

Full file: TweenUI.java

this.manager.killAll();
this.x = 0F;
this.y = 0F;
this.last = BridgeHandler.CLOCK.get().currentTimeMillis();
Timeline
.sequence()
.push(Tween.to(() -> this.x, value -> this.x = value, 800F).target(1F).easing(TweenEquations.QUAD_IN_OUT))
.pushPause(200F)
.push(Tween.to(() -> this.y, value -> this.y = value, 600F).target(1F).easing(TweenEquations.BACK_OUT))
.repeatYoyo(BaseTween.INFINITY, 400F)
.start(this.manager);

Full file: TweenUI.java

RectNode
.create(660, 340, 600, 400)
.color(TweenUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(TweenUI.ELEMENT, 1.5F))
.body(area -> {
RectNode
.create(0, 0, 80, 80)
.color(TweenUI.ACCENT)
.x(() -> 20D + 480D * this.x)
.y(() -> 20D + 260D * this.y)
.attach(area);
})
.attach(this);

Full file: TweenUI.java

repeatYoyo plays every second pass backward, so one timeline of two tweens gives the whole round trip.

killAll() comes first because init() runs again at each opening and reload, and the previous timeline would keep playing next to the fresh one. update() advances the manager by the time since its previous call, read on BridgeHandler.CLOCK; init() resets last, so the time that the UI stays closed does not count.

  • Tween.to(getter, setter, ms) goes from the current value to target(...), Tween.from(...) comes from target(...) to the current value, Tween.set(...) applies target(...) at once, and Tween.call(listener) runs code when a timeline reaches it.
  • targetRelative(...) adds an offset to the start value, and waypoint(...) adds values to pass through, on a smooth curve, or on straight segments with TweenPaths.LINEAR given to path(...); a path of your own implements ITweenPath.
  • Timeline.sequence() plays its children one after the other and Timeline.parallel() together; beginSequence(), beginParallel() and end() nest them, and pushPause(ms) waits.
  • A tween or a timeline has the repeat, delay and listeners of an animator, and pause(), resume() and kill().
  • start() without a manager starts a tween that you advance with its own update(delta).
  • TweenAnimator.timeline gives an animator a timeline that you build, such as one with a pause, from tweens made by its tween(ms).
  • Pick the tool from the motion: the hover progress for hover, a TweenAnimator for a value from A to B, lerpByFramerate for a moving target, the tween engine for values of your own.
  • An animator is a read-only signal that JOID advances after start(); setters follow it in an expression.
  • Easing: OUT for what appears or reacts, IN for what leaves, IN_OUT for back and forth; TweenEquations.of finds one by name.
  • push chains steps, repeat and repeatYoyo replay them, set before start(); sequence(...) again replays from the current value.
  • An animator that repeats forever runs until you clear() it.
  • The tween engine needs tweens on a getter and a setter, a timeline to arrange them, and a manager advanced in update().

Pulse forever. Make a circle grow from 1 to 1.2 times its size and back, forever, soft at both ends.

Solution
final TweenAnimator pulse = TweenAnimator
.create(1F)
.sequence(600F, 1.2F, TweenEquations.SINE_IN_OUT)
.repeatYoyo(BaseTween.INFINITY, 0F)
.start();
CircleNode
.create(200, 200, 100)
.color(PulseUI.ACCENT)
.anchor(Align.CENTER)
.width(100D * pulse.get())
.height(100D * pulse.get())
.onDetach(event -> pulse.clear())
.attach(this);

Full file: PulseUI.java

The size follows the animator through the expression, and Node.anchor with Align.CENTER keeps the circle centered on its position while it grows; onDetach(...) stops the animator with the UI. A TransformNodeEffect scales the node without touching the layout instead (Effects).

Read the easing from a file. A settings file holds the name of the easing of a menu, such as "Cubic.OUT", and a missing or wrong name must fall back to QUAD_OUT.

Solution
final TweenEquation found = TweenEquations.of(name);
final TweenEquation easing = found != null ? found : TweenEquations.QUAD_OUT;

Report a problem with this page