← Return to website
Python animation toolkit

cardkit

Write an animation once in Python.
Get a GIF, a WebP, an MP4 and an animated SVG, and they all look the same.

raster · matplotlibvector · native SVG
$ pip install cardkit-anim
cardkit tour, animated SVG
The model

Five ideas is all it takes

1

Coordinates are slide units

y points up, type size in points

ck.Scene(duration=5, size=(9, 9))
2

Anything is a function of time

positions, colours, opacity, even text

Circ(lambda t: (1 + t, 4.5), r=lambda t: 0.2 + 0.04 * t,
     fill=ck.keyframes({0: "#0f9d8a", 7: "#e17d38"}))
Txt(lambda t: f"{int(100 * t / 7)} %", (4.5, 2.4), 56)
3

Five primitives

polygon, line, circle, text, image

Poly  Path  Circ  Txt  Img
4

Groups fade as a whole

no seams, smaller SVGs

with scene.group("step1", ck.fade(0, 3.2)): ...
with scene.group("step2", (3.2, 6.0)): ...
5

Author in scene time

add an intro or reading pauses later

ck.Scene(duration=6, intro=1.2, holds={3: 1.6})
example

Hello, cardkit

One scene, two backends

import numpy as np
import cardkit as ck
from cardkit import Txt, Path, Circ, ramp

scene = ck.Scene(duration=4, size=(10, 6))
p = ramp(0.8, 2.6)             # 0 -> 1 between 0.8 s and 2.6 s

# a title fades in, a curve draws itself
scene.add(Txt("Hello, cardkit", (0.7, 5), 30, op=ramp(0.2, 0.8)))
x = np.linspace(0.8, 9.2, 120)
scene.add(Path(np.c_[x, 2.6 + 0.9 * np.sin(1.6 * x)],
               "#0f9d8a", 4, draw=p))

# a counter, and a dot that pulses forever
scene.add(Txt(lambda t: f"{100 * p(t):.0f} %", (0.7, 0.9), 22))
scene.add(Circ((9, 5.1), r=ck.pulse(1.2, 0.1, 0.2), fill="#0f9d8a"))

scene.save_all("out/hello", "gif", "svg")
vector output
SVG vector · 46 KB
raster output
GIF raster · 262 KB

Raster

  • matplotlib draws frames
  • GIF · WebP · MP4 · PNG

Vector

  • SMIL, no JS, no fonts
  • plays in <img>
Preview

Storyboard before you render

The BrainPFN card: 25 s, four steps, an intro and four pauses.

storyboard contact sheet of the BrainPFN card
scene = ck.Scene(duration=25, size=(9, 11.25), origin=(0, -0.85), intro=3.0,
                 holds={6.05: 0.9, 11.85: 0.9, 17.65: 0.9, 24.65: 0.9})
# ... an intro and four groups: connectivity, mechanisms, meta-training, results

scene.storyboard("board.png", labels=True, cols=6, keys=[
    ("hook", -1.3), ("connectivity", 3.8), ("small N", 5.9),
    ("mechanisms", 11.7), ("meta-training", 17.6), ("results", 24.6)])
scene.check_text(scene.to_display(24.6))    # [check t=30.30] 45 texts, 0 issues
  • Keys are in scene time: the intro sits at negative time
  • No keys? One finished frame per group
  • check_text flags overlapping or clipped labels
  • No GIF or MP4 needed to review the story
cardkit.three

3-D, still vector

from cardkit.three import Camera, box, slab

cam = Camera(origin=(0.5, 3.4))         # fixed oblique camera

# a (5, 5) matrix lies flat, revealed cell by cell
scene.add(slab(cam, (0, 0, 0), fc, 0.6,
               colors=lambda v: ck.mix("#fff", "#0f9d8a", abs(v)),
               reveal=lambda k: ramp(0.3 + 0.03 * k, 0.6 + 0.03 * k)))

# boxes whose height is a function of time
for k, h in enumerate([1.6, 2.6, 2.0, 3.2]):
    z = ramp(1.5 + 0.3 * k, 2.5 + 0.3 * k) * h
    scene.add(box(cam, (4.2 + 0.9 * k, 0.4, 0), (0.7, 0.7, z),
                  "#6c5ce7"))

Plain polygons, Lambert lighting, hidden faces removed once: the animated SVG stays fully vector.

3-D example from this code
This code · 22 KB
three_d.py
three_d.py · BrainPFN card
← → navigate · F fullscreen