SelfscribeDownloadOpen the app

Diagrams that move

Dots travel, switches flip and states change, from a few lines of text.

Interactive · Advanced · uses animation, diagram, buttons

Edit it in the playgroundRead the file

What it shows

Make a diagram move, show itself in steps, and answer clicks.

What you learn

Dots that travel along a link, steps to go through one at a time, circuits with switches to click, state machines with buttons, a panel that remembers its switches in a data block.

Try this

  • Click the switch in the reading lamp's circuit, and the lamp goes dark.
  • Change moving=slow to moving=fast on the solar panel's link in the first diagram, and its dots speed up.
  • Change van.pump, open to van.pump, closed in the panel table, and the water pump starts.
examples/animated-diagrams.self · 233 lines
~~~ selfscribe
# Lesson: Make a diagram move, show itself in steps, and answer clicks.
# You learn: dots that travel along a link, steps to go through one at a time, circuits with switches to click, state machines with buttons, a panel that remembers its switches in a data block.
# Try this: click the switch in the reading lamp's circuit, and the lamp goes dark.
# Try this: change `moving=slow` to `moving=fast` on the solar panel's link in the first diagram, and its dots speed up.
# Try this: change `van.pump, open` to `van.pump, closed` in the panel table, and the water pump starts.
# summary: Dots travel, switches flip and states change, from a few lines of text.
# category: diagrams
# tags: interactive, advanced, ideas
# uses: animation, diagram, buttons
use markdown
use data
set title Diagrams that move
set style sans wide
set heading-font Space Grotesk

# ---- Colours, each with its shade for dark pages ----------------------------------------
palette
  amber   #b45309  dark #fbbf24
  tint    color-mix(in srgb, var(--amber) 12%, transparent)

# ---- "> a line": the line under the title, in the document's own voice -------------------
line  > _      -> p.lede      { margin: -0.3em 0 0.9em; font-size: 1.15em; line-height: 1.5; opacity: 0.82 }  as lede

# ---- "kit a | b | c": the parts of the system, as tags in a row --------------------------
line  kit _    -> p.kit       split "|" span.part  { display: flex; flex-wrap: wrap; gap: 0.4em; margin: 0 0 1.4em }  as kit
class part  { padding: 0.2em 0.75em; border-radius: 999px; background: var(--tint); color: var(--amber); font: 600 0.82em/1.6 var(--doc-font-ui) }

# ---- "```flow" ... "```": a diagram written one line at a time ----------------------------
# "node id "Label" :shape" draws a part, "a -> b" joins two. Moving dots, steps, switches and
# buttons are words added to those lines.
block  ``` flow ... ```  -> flow  verbatim
~~~
# Juniper's 12-volt system

> Where the power comes from and where it goes in our 2014 Transit Custom.

```flow
set direction=LR
node sun "Solar panel\n200 W on the roof" color=yellow
node alt "Engine alternator" color=orange
node hookup "Campsite hook-up\n230 V" :cloud color=purple
node mppt "Solar controller\n20 A" color=yellow
node dcdc "Charger from the engine\n30 A" color=orange
node mains "Mains charger\n15 A" color=purple
node bat "Lithium battery\n100 Ah, 84% full" :database color=green
node fuse "Fuse box\n12 ways" color=grey
node fridge "Fridge\n4.2 A" color=teal
node fan "Roof fan\n2.8 A" color=teal
node usb "USB sockets\n1.5 A" color=teal
node lights "Ceiling lights\noff"
sun -> mppt -> bat moving=slow
alt -> dcdc -> bat moving=fast
hookup -> mains -> bat dash=dotted
bat -> fuse moving
fuse -> fridge moving
fuse -> fan moving=fast
fuse -> usb moving=slow
fuse -> lights
```

kit 200 W solar panel | 100 Ah lithium battery | 30 A charger from the engine | 12-way fuse box | 1,800 km this summer

This is the van on the road on a sunny afternoon. The dots are the current: the faster they run, the more is flowing. The sun and the engine both charge the battery, and the hook-up is dotted because we are nowhere near a campsite.

## How the battery fills on a sunny morning

Use the bar under the diagram to go through the morning one step at a time.

```flow
set direction=LR
node sun "Solar panel" color=yellow
node mppt "Solar controller" color=yellow
node bat "Battery" :database color=green
node fuse "Fuse box" color=grey
node fridge "Fridge" color=teal
node usb "Phones charging" color=teal
sun -> mppt "18.6 V" moving=slow
mppt -> bat "13.9 V, 9 A" moving
bat -> fuse moving
fuse -> fridge moving
fuse -> usb moving=slow
step "06:40 · First light on the panel. The controller wakes up": sun mppt
step "07:15 · The battery starts to fill, from 61%": bat
step "08:00 · The fridge runs on sunshine, not on the battery": fuse fridge
step "08:30 · Two phones charge from the sun as well": usb
```

## The reading lamp over the bed

The switch is closed, so the current runs and the lamp is lit. Click the switch to turn it off.

```flow
node cell "12 V" :battery
node sw "Bed switch" :switch-closed
node bulb "Reading lamp" :lamp
cell -> sw -> bulb -> cell moving
```

## The step light

It lights only when the sliding door is open and its switch is on: two switches, one after the other. Click both.

```flow
node cell "12 V" :battery
node door "Door open" :switch
node key "Step light on" :switch
node bulb "Step light" :lamp
cell -> door -> key -> bulb -> cell moving
```

## The ceiling lights, from the door or the bed

Each switch has its own path to the lights. Close either one and they come on.

```flow
node cell "12 V" :battery
node a :junction
node b :junction
node front "By the door" :switch
node back "By the bed" :switch
node bulb "Ceiling lights" :lamp
cell -> a moving
a -> front -> b moving
a -> back -> b moving
b -> bulb -> cell moving
```

## The water pump

The tap has a small switch inside it. The pump runs only while you hold the tap open, like a doorbell.

```flow
node cell "12 V" :battery
node push "Tap open" :button
node motor "Water pump" :motor
cell -> push -> motor -> cell moving=fast
```

## The night light by the door

An LED needs a resistor in front of it, or 12 volts burns it out in a second.

```flow
node cell "12 V" :battery
node r "470 Ω" :resistor
node led "Green LED" :led color=green
node sw :switch-closed
cell -> sw -> r -> led -> cell moving=slow
```

## The solar controller, stage by stage

The controller fills the battery in three stages. Press the buttons under the diagram to move it from one to the next.

```flow
set start=night
node night "Night: resting" :start
node bulk "Bulk: all the current it can" color=orange
node absorb "Absorption: held at 14.2 V" color=yellow
node float "Float: topped up at 13.5 V" color=green
night -> bulk "sun comes up"
bulk -> absorb "battery reaches 14.2 V"
absorb -> float "current falls below 2 A"
float -> bulk "fridge pulls it below 13.2 V"
float -> night "sun goes down"
```

## The diesel heater

```flow
set start=off
node off "Off" :start
node glow "Starting: glow plug, 2 min" color=orange
node run "Heating to 19 °C" color=red
node cool "Cooling down, 3 min" color=teal
node fail "Failed to start" color=grey
off -> glow "switch on"
glow -> run "flame lit"
glow -> fail "no fuel"
run -> cool "switch off"
cool -> off "fan stops"
fail -> off "reset"
```

## The switch panel by the door

Flip a switch here, close the document and open it again: the panel is as you left it. The main isolator and the lights start closed because the table under it says so.

```flow
diagram van "Switch panel"
set keep=panel
node cell "12 V" :battery
node a :junction
node b :junction
node lights "Lights" :switch
node pump "Pump" :switch
node l1 "Ceiling lights" :lamp
node m1 "Water pump" :motor
node isolator "Main isolator" :switch
cell -> a moving
a -> lights -> l1 -> b moving
a -> pump -> m1 -> b moving
b -> isolator -> cell moving
```

```data panel as hidden
name, value
van.isolator, closed
van.lights, closed
van.pump, open
```

view: panel as table

## How it's made

Every diagram on this page is a `flow` block: `node` lines for the parts and `a -> b` lines for the links. The only rule in the rules block sends those blocks to the flow plugin.

- **Moving dots** come from `moving` at the end of a link. `moving=slow` and `moving=fast` change the pace, and `dash=dotted` draws a link that carries nothing.
- **Steps** are `step` lines. Each names the parts it brings in, and the bar under the diagram goes through them.
- **Circuits** use the shapes of circuit parts: `:battery`, `:switch`, `:switch-closed`, `:button`, `:lamp`, `:led`, `:resistor`, `:motor` and `:junction`. A click flips a switch, and the current and the lamp follow.
- **State machines** start with `set start=`. Each link's words become a button under the diagram. The switch panel has `set keep=panel`, so each click is written into the `panel` table as a row such as `van.pump, closed`.

## Try it yourself

1. In the first diagram, change `moving=slow` on the solar panel's line to `moving=fast`. The dots from the roof speed up.
2. Change `hookup -> mains -> bat dash=dotted` to `hookup -> mains -> bat moving`. The van is plugged in at a campsite.
3. In the morning steps, add `step "11:00 · Full, and held at 13.5 V"` at the end. The bar gains a fifth step.
4. In the reading lamp, change `:switch-closed` to `:switch`. The lamp is dark before you click anything.
5. In the panel table, change `van.pump, open` to `van.pump, closed`. The water pump starts.

**Next:** *One diagram, many views* shows one diagram in several places; *Custom diagram shapes* draws shapes of your own that turn and blink.