Transitions
With the transition option of cm.flow, each render animates from what the renderer shows to what the flow draws: the shapes that match update, and the others enter, exit, split or merge. The SVG, Canvas and terminal renderers support transitions; a renderer that doesn't throws an error.
const letters = "ABCDEFGHIJKL".split("");
const renderer = cm.svg({width: 640, height: 300});
const root = cm.flow({transition: {duration: 750}}).renderer(renderer);
function frame() {
const data = letters.filter(() => Math.random() > 0.3).map((id) => ({id, value: 20 + Math.random() * 260}));
root
.data(data)
.transform(cm.bind, {
x: (d, i) => 20 + i * 50,
y: (d) => 300 - d.value,
width: 40,
height: (d) => d.value,
key: (d) => d.id,
})
.append(cm.rect, {fill: "steelblue", enter: {y: 300, height: 0}, exit: {y: 300, height: 0}});
root.render();
}
frame();
setInterval(frame, 1500);
document.body.append(renderer.node());Options
The transition option is true for the defaults, or an object of defaults; transitions are off without it. The first render with transitions on is drawn as it is, unless shapes appear.
- duration — in milliseconds (default
750). - delay — in milliseconds (default
0). - ease — the ease (default
"cubic-in-out"). - direction, iterations — how keyframes play.
Each shape may time its rows with the channels duration, delay and ease, such as delay: (d, i) => i * 30 for a stagger. A row's own timing wins over the flow's.
Numbers interpolate linearly, and colors in RGB; other values, such as a path's d or a text, change at the end. A numeric text with a format counts, as a ticker. Groups move with their translations, carrying their children. A render during a transition starts from the shapes as shown.
Matching
Each primitive drawn matches a primitive shown, drawn by the same shape of the flow under the same parent rows:
- by key, a channel, if it has one, such as
key: (d) => d.id: keyed primitives only match by key; - else by its datum, if it's an object, such as the rows of a bubble sort;
- else by its index.
A primitive matched by one of another type, such as a rect keyed like a circle, morphs into it through their outlines; see the shape morph example.
groupKey splits and merges: a primitive that doesn't match splits from the primitive shown whose key is its group key, and a primitive shown that doesn't match merges into the primitive drawn whose key is its group key. Each takes a piece of the group's box, so that a bar splits into its points and they merge back.
const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow({transition: {duration: 1200}}).renderer(renderer);
const points = Array.from({length: 24}, () => [40 + Math.random() * 560, 40 + Math.random() * 320]);
let split = false;
function frame() {
if (split) {
root
.data(points)
.transform(cm.bind, {x: ([x]) => x, y: ([, y]) => y, r: 8, groupKey: "A"})
.append(cm.circle, {fill: "steelblue"});
} else {
root.append(cm.rect, {x: 220, y: 100, width: 200, height: 260, fill: "orange", key: "A"});
}
root.render();
root.finished().then(() => setTimeout(() => ((split = !split), frame()), 600));
}
frame();
document.body.append(renderer.node());Enter, exit and appear
The options enter and exit of a shape are the states a primitive enters from and exits to, as objects of channels, such as {y: 300, height: 0}, whose values may be functions of the row. Without them, a primitive fades in and out. Clip insets make wipes, such as enter: {clipTop: 1} to rise from the bottom.
On the first render, nothing is shown yet, and shapes are drawn as they are, unless they opt in with appear: true to enter from the enter state, as later; or a state of its own, such as {clipRight: 1}.
Each state may have its own timing, delay, duration and ease, such as enter: {y: 300, delay: 200}, else the shape's, else the flow's. Each state is a channel of its own, such as enterY and enterDelay, so it may be bound and transformed like any other, such as by cm.stackEnter.
root.data(data).append(cm.rect, {
x: (d) => d.x,
y: (d) => d.y,
width: 20,
height: (d) => d.height,
key: (d) => d.id,
appear: true,
enter: {y: 300, height: 0, delay: (d, i) => i * 20},
exit: {fillOpacity: 0, duration: 300},
});Eases
An ease is a function of t from 0 to 1, or the name of a d3-ease function in kebab case, without ease: "linear", "quad-in", "cubic-in-out", "elastic-out", "bounce", and so on. An unknown name throws an error.
cm.keyframe
A shape that draws nothing, like cm.g, whose rows are moments played one after another, in time, rather than drawn at once. The shapes appended to it are drawn for one moment at a time, each animated from the one before, and they match from moment to moment. Sibling keyframes form one sequence; the other shapes beside them are drawn at every moment.
Keyframes play even with transitions off, with the defaults. The first moment is drawn as it is. Options time the transition into each moment:
- duration, delay — functions of the row, or constants;
- ease — a name or a function, the same for every moment, or an array of names, one per moment.
The direction of the flow's transition, "normal" (default) or "alternate", plays the moments in order, or back and forth, iterations times (default 1), which may be Infinity. A render stops a sequence. Keyframes in keyframes aren't supported yet.
const renderer = cm.svg({width: 640, height: 120});
const root = cm.flow({transition: {duration: 600, direction: "alternate", iterations: Infinity}}).renderer(renderer);
root
.data([40, 600, 320])
.append(cm.keyframe)
.append(cm.circle, {x: (d) => d, y: 60, r: 30, fill: (d) => (d === 320 ? "orange" : "steelblue")});
root.render();
document.body.append(renderer.node());See the bar chart race and bubble sort examples.
flow.finished()
Returns a promise that resolves when the transition of the last render ends, or its sequence of keyframes, or at once without one. A render that interrupts it resolves it too.
root.render();
await root.finished();