Skip to main content

Animation

note

Animation is in alpha and may change. If you have feedback, open a GitHub issue.

Use style.animated to animate a shape or connection on a single board. To show a sequence, use animate-interval to play boards in order. Set animate-duration to control how long a transition takes. Steps can animate styles, variables, and grids. Pencil shading and text decorations also animate between consecutive layers and scenarios. The default duration is 0, so boards change instantly.

This example is an animated SVG you can embed in a GitHub README.

Pulses travel along connections as grid cells appear and disappear.
View D2 source

Configuration​

The 1-second interval includes the transition. Each animated step spends 400 ms changing and holds its final state for the remaining 600 ms.

Export to SVG or GIF. The CLI accepts durations in milliseconds.

Animate styles​

The following properties animate when their values change in a step.

Opacity​

A shape and its connection fade together.
View D2 source

Fill​

A shape changes from amber to green before its label changes.
View D2 source

Set style.fill on the board to animate its background color.

The board background changes from green to amber.
View D2 source

Pencil shading​

In sketch mode, changing a shape’s solid fill shades it in with pencil strokes. Changing the fill to transparent erases it.

A standby volume’s block ranges are shaded in as they are restored from a snapshot.
View D2 source

Stroke​

Set style.stroke to animate the outline of a shape or the color of a connection.

Two connections exchange stroke colors.
View D2 source

Font color​

One label changes from gray to red.
View D2 source

Text decorations​

Keep the same object ID and label text. Set style.underline on a shape or connection to draw an underline, or wrap a Markdown span in ~~ to draw a strikethrough. Removing the decoration retracts the line. Both work in regular and sketch mode.

The v1 webhook endpoint is crossed out as the v2 endpoint is underlined. Both marks retract on rollback.
View D2 source

Stroke width​

One connection thickens as the other thins.
View D2 source

Stroke dash​

A dashed connection becomes solid.
View D2 source

Border radius​

A square upload stays fixed while its matching profile avatar rounds into a circle.
View D2 source

Font size​

A numeric label grows while a second label stays the same size.
View D2 source

Width​

A horizontal bar doubles in width.
View D2 source

Height​

A queue grows taller as its label counts up.
View D2 source

Change a gradient​

Animate a gradient by changing its color stops or angle between steps.

Fill a progress bar​

A sharp color boundary advances across a bar.
View D2 source

Fade one end of a fill​

Build log entries show through an overlay and fade toward the bottom of the log viewer.
View D2 source

Expand a radial gradient​

A wider search radius includes a second log embedding while a third stays outside.
View D2 source

Rotate a gradient​

A highlight travels around a loading ring beside the status “Restoring snapshot”.
View D2 source

Send a pulse along a connection​

A narrow band of color travels along a connection and through its arrowhead.
View D2 source

Animate with variables​

Use the same variable in several places to animate them together.

A percentage label counts down as the filled portion of a bar shrinks.

Count in a label​

A label counts down in whole numbers.
View D2 source

Share a color​

Two shapes change from red to green together.
View D2 source

Move a pinned shape​

With TALA, bind left or top to a numeric variable and change the variable in a step. Setting the coordinate directly in the step moves the shape immediately.

A pinned shape slides horizontally between two positions.
View D2 source

Grid animations​

When a grid changes, D2 matches cells by ID. Cells that keep their IDs can move to new slots. New cells grow into place, and removed cells shrink away.

Reorder cells​

A grid fills in declaration order. To move a cell later in that order, remove it with null and redeclare the same ID in the same step.

Grid cells slide into new slots as one cell leaves and two enter.
View D2 source

Add or remove a row or column​

Insert or remove a complete row or column in a filled grid. Keep the other cells in the same relative order. The row or column grows or shrinks as a group.

A column opens between two existing columns. A second column shrinks away.
View D2 source

Replace a cell in place​

In a grid with at least two cells, replace one plain rectangle with a new ID in the same slot. Keep its size and style, and leave its neighbors in place. Use centered, single-line labels. The label rolls inside the outline. Equal-length monospace labels can keep a shared prefix in place.

The end of a label rolls to a new value inside a fixed cell.
View D2 source

Expand or collapse a cell​

Keep a rectangular cell's ID and give it a larger nested grid. Each child appears when it fits. To collapse the cell, remove its children and restore its original dimensions.

A cell expands to reveal a nested grid, then folds closed.
View D2 source