Animate Directive Examples#

The animate directive extends the plot directive to create animations by varying one or more variables across frames.

Basic Function Animation#

Here’s a simple animation of a sine wave with varying frequency:

Sinusfunksjon med varierende frekvens \(a\) fra 1 til 5.

Rotating Vector#

Animate a vector rotating around the origin:

En vektor som roterer \(360°\) rundt origo.

Moving Point on Parabola#

Animate a point moving along a parabola:

Et punkt beveger seg langs parabelen \(f(x) = x^2\).

Wave Interference#

Two waves with phase difference:

Interferens mellom to bølger med faseforskjell \(\phi\).

Expanding Circle#

A circle that grows in radius:

En sirkel som vokser fra radius \(0.5\) til \(4\).

Tangent Line Animation#

A tangent line moving along a curve:

Tangentlinje til \(f(x) = x^3 - 3x\) i punktet \((a, f(a))\).

Options Reference#

Animation-Specific Options#

  • animate-var: name, start, end, frames - Define animation variable

    • name: Variable identifier (must be valid Python identifier)

    • start: Starting value (supports SymPy expressions like pi, sqrt(2))

    • end: Ending value (supports SymPy expressions)

    • frames: Number of frames (positive integer)

  • fps: 10 - Frames per second (default: 10)

  • duration: 200 - Frame duration in milliseconds (alternative to fps)

  • loop: true - Loop animation infinitely (default: true)

  • format: webp - Output format: webp (default) or gif

All Plot Options Work#

The animate directive supports all options from the plot directive:

  • function, point, vector, circle, ellipse, curve

  • line, line-segment, vline, hline, polygon

  • annotate, text, bar, angle-arc

  • xmin, xmax, ymin, ymax, xstep, ystep

  • grid, ticks, xlabel, ylabel, fontsize

  • width, align, class, alt, name

Tips and Best Practices#

Performance#

  • Keep frame count reasonable (20-50 frames is usually sufficient)

  • Use WebP format for smaller file sizes and better quality

  • Cache is automatic - animations regenerate only when content changes

Variable Substitution#

The animation variable can appear anywhere in plot content:

:::{animate}
animate-var: n, 1, 10, 10
function: x**n
point: 1, 1**n
:::

Variables support mathematical expressions:

  • animate-var: t, 0, 2*pi, 30

  • animate-var: a, -sqrt(5), sqrt(5), 20

  • animate-var: n, 1, E, 15

Combining with Multi-Plot#

Animations work inside multi-plot2 containers:

rows: 1 cols: 2

File Sizes#

  • WebP typically produces 30-50% smaller files than GIF

  • Higher FPS = smoother animation but larger file

  • More frames = longer animation and larger file

  • Reduce fontsize and lw for smaller output

Browser Compatibility#

  • WebP: Modern browsers (Chrome, Firefox, Edge, Safari 14+)

  • GIF: Universal compatibility

  • Both formats support transparency