Animation
An animation writes a value over and over, for a while, one step per animation frame. Everything that depends on the value updates with it, every frame. So animating needs nothing new: animate the value, and the relationships carry the motion through the graphic.
Easing to a target
Animate.real.eased(value, to, duration) moves a number from where it is now to to, in
duration milliseconds. The easing function decides how it gets there:
// Back and forth: every animation starts the next one when it is done
const go = (to: number) => {
if (!div.isConnected) return // the figure is gone: stop
Animate.point.eased(dot, [to, y], 1600, {
easing,
onComplete: () => go(to === 580 ? 180 : 580),
})
}
go(580)EasingFunctions has the usual curves: linear, the Cubic and Sine
families with easeIn (slow start), easeOut (slow end) and easeInOut (both), and
easeOutBack, which overshoots a little. onComplete is called when the target is reached.
Animating data
Because an animation only writes a value, it works through everything that depends on the
value. These bars are attached to the baseline with a Real as their
height; the click animates the values, nothing else:
const heights = [80, 150, 110, 60].map(h => canvas.values.real(h))
heights.forEach((height, i) => {
const bar = canvas.visuals.rectangle("shape")
Attach.rectangleToSegment(bar, height, base, { paddingLeft: 20 + i * 100, paddingRight: 320 - i * 100 })
})
// New data: animate the values, the attachments do the rest
Svg.get(rvg).addEventListener("click", () => {
for (const height of heights)
Animate.real.eased(height, 30 + Math.random() * 180, 700, { easing: EasingFunctions.easeOutCubic })
})This is the usual way to animate a chart: keep the data in values, build the chart from them once, and animate the values when the data changes.
Following a moving target
eased takes a fixed time. To chase something that keeps moving, such as a handle, use follow:
on every frame it moves a part of the remaining way, given by alpha (0 to 1). Large values
follow closely, small values lag behind smoothly:
const handle = canvas.handles.disk("handle", [320, 140])
const near = canvas.visuals.point("dot", [320, 140])
const far = canvas.visuals.point("shape", [320, 140])
// Each moves a part of the remaining way on every frame
Reactive.do([handle], () => {
Animate.point.follow(near, handle, { alpha: 0.25 })
Animate.point.follow(far, handle, { alpha: 0.06 })
})Calling follow again with a new target, as the block does on every move, just changes where it
is heading.
The animators
| Animator | For | Can |
|---|---|---|
Animate.real, Animate.integer |
numbers | eased, follow, set, snap |
Animate.point |
points, also visuals and handles | eased, follow, set, snap |
Animate.pin |
pins, with their direction | the same, plus easedAt to turn around an anchor |
Animate.shape |
all points of a shape | morph, follow |
Animate.string |
texts | animate (types it letter by letter), set, snap |
Animate.bool |
flags | set, snap |
set writes a value after a delay, snap writes it at once.
One animation per value
A value has at most one animation. Start a new one, and it replaces the running one, starting from wherever the value is at that moment. That’s why you can start an animation on every click without thinking about what is still running:
Animate.real.eased(x, 100, 500) // from where x is now to 100, in 500 ms
Animate.real.eased(x, 50, 200) // replaces the first one, from where x is now
x.value = 999 // overwritten on the next frame
Animate.cancel(x) // stops, x keeps its current value
Animate.real.snap(x, 0) // stops and jumps to 0An animation is a writer like an attachment: write the value yourself while it runs, and the next frame overwrites you. Cancel it first.
Timing and sequences
onComplete starts the next step when one is done. set and Animate.after
do things later, in step with the other animations:
// One after the other: move, then type the title
Animate.point.eased(marker, [400, 100], 600, {
onComplete: () => Animate.string.animate(title, "Arrived", 500),
})
// Later: show the marker after one second, call something after two
marker.visible.value = false
Animate.bool.set(marker.visible, true, 1000)
Animate.after(2000, () => console.log("two seconds"))For anything longer than two or three steps, keep the step in a value (a “state”), and start the
animation of each step in a Reactive.do that depends on it. Then you can jump to a step, skip
one, or start over by setting the value.
Shapes
Animate.shape.morph(shape, target, duration) moves every point of a shape to the matching
point of target. follow does the same continuously. Both announce the shape as changed on
every frame, so a Reactive.do([shape], …) or an attachment to the shape runs along.
