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
Choose the right tool
Section titled “Choose the right tool”Start from what you want to see:
| You want | Use |
|---|---|
| A node that changes while the mouse is over it | The 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 clicks | A TweenAnimator (Animate a value) |
| A value that chases a target that keeps moving, such as the mouse | UI.lerpByFramerate (Follow a moving target) |
| Several values of your own, in timelines with pauses and listeners | The tween engine: Tween, Timeline, TweenManager (Animate values of your own) |
| A UI that animates when it opens or closes | A transition |
Every tool produces a number; a node turns it into motion by reading it in a setter.
Animate a value
Section titled “Animate a value”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);TweenAnimator.create(0F)starts the value at0F.sequence(600F, 1F, ...)describes a step: reach1Fin 600 ms with theTweenEquations.CUBIC_OUTeasing.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.
Shape the motion with easing
Section titled “Shape the motion with easing”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.
| Feel | Equation |
|---|---|
| Constant speed, for loops that never stop | TweenEquations.LINEAR, the easing of a step without one |
| Fast start, soft landing, for what appears or reacts to the user | QUAD_OUT, CUBIC_OUT, QUART_OUT, from gentle to strong |
| Soft at both ends, for back and forth motion | SINE_IN_OUT, QUAD_IN_OUT |
| Slow start, fast end, for what leaves the screen | QUART_IN, CUBIC_IN |
| Overshoots the target, then settles | BACK_OUT |
| Springs or bounces around the target | ELASTIC_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);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.
Chain, repeat and replay
Section titled “Chain, repeat and replay”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);start() loops the chain forever: repeat plays both steps again after each pause of 200 ms.
repeat(count, delay)plays the chaincountmore times, waitingdelayms between plays;repeatYoyo(count, delay)plays every second time backward.BaseTween.INFINITYrepeats forever.delay(ms)waits before the first play, andspeed(2F)plays twice as fast;speed(0F)holds the value where it is.- Set the repetitions before
start(): a started animation refuses them with anIllegalStateException.
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);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()))Follow a moving target
Section titled “Follow a moving target”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;@Overridepublic 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);}CircleNode.create(0, 520, 80).color(AnimationUI.ACCENT).x(() -> this.followX).attach(this);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.
Animate values of your own
Section titled “Animate values of your own”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;@Overridepublic void update() { final long now = BridgeHandler.CLOCK.get().currentTimeMillis(); this.manager.update(now - this.last); this.last = now;}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);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);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 totarget(...),Tween.from(...)comes fromtarget(...)to the current value,Tween.set(...)appliestarget(...)at once, andTween.call(listener)runs code when a timeline reaches it.targetRelative(...)adds an offset to the start value, andwaypoint(...)adds values to pass through, on a smooth curve, or on straight segments withTweenPaths.LINEARgiven topath(...); a path of your own implementsITweenPath.Timeline.sequence()plays its children one after the other andTimeline.parallel()together;beginSequence(),beginParallel()andend()nest them, andpushPause(ms)waits.- A tween or a timeline has the
repeat,delayand listeners of an animator, andpause(),resume()andkill(). start()without a manager starts a tween that you advance with its ownupdate(delta).TweenAnimator.timelinegives an animator a timeline that you build, such as one with a pause, from tweens made by itstween(ms).
- Pick the tool from the motion: the hover progress for hover, a
TweenAnimatorfor a value from A to B,lerpByFrameratefor 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:
OUTfor what appears or reacts,INfor what leaves,IN_OUTfor back and forth;TweenEquations.offinds one by name. pushchains steps,repeatandrepeatYoyoreplay them, set beforestart();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().
Try it
Section titled “Try it”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);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;