Build your own loader as a JSON scene, one step at a time, from a still dot to an orbit with a trail.
Engine · 4 min read
A pattern is a scene: a JSON object that says how bright each dot is at each moment. Pass it to pattern in place of a built-in name. The built-in patterns are scenes too.
The smallest scene
A scene needs two keys. duration is the length of one loop, in milliseconds. light says how bright each dot is, from 0 to 1.
paint lights the dots inside a shape. The shape here is a circle. The space runs from -1 to 1 on each axis, with 0 in the middle. So a radius of 0.45 is a little under half the matrix.
Make it move
Any number can change over time. pulse goes from low to high and back once in each loop.
Move a shape
place moves a shape to x and y. Here spin gives an angle that makes one full turn in each loop. cos and sin of that angle trace a circle.
Add a trail
trail draws the light of a field a little earlier in the loop, fading with age. length is how far back it goes, as a part of the loop.
Three kinds of node
Every node is one of three kinds. Each kind has its own key.
| Kind | Key | What it gives | Examples |
|---|---|---|---|
| Value | fn | A number, at each dot and time | pulse, spin, keys, sin, mix, noise, ref |
| Form | form | A shape | circle, box, star, arc, polygon, morph, place, repeat |
| Field | op | The light of every dot | paint, trail, brightest, text, particles, sparkle, life |
A plain number is a value too. So "radius": 0.45 and "radius": { "fn": "pulse", … } both work.
Read time and place
{ "fn": "ref", "name": … } reads a value from the loop, the dot or the copy:
| Name | What it is |
|---|---|
t | Time in the loop, from 0 to 1 |
u, v | The dot's place, from -1 to 1. v grows down |
r, angle | The dot's distance from the middle, and its angle (0 at the top, clockwise) |
width, height, unit | Half the lattice width and height, and the distance between two dots |
index, col, row | The dot's number, grid column and grid row |
input | The value prop, from 0 to 1 (see Loading states) |
i, count, seed | Inside repeat or particles: the copy number, the number of copies, and a random number for the copy |
Keys of a scene
| Key | Default | What it does |
|---|---|---|
duration | (required) | One loop, in milliseconds, from 100 to 30,000 |
light | (required) | The light of each dot |
scale, angle | none | The size (up to 1.6 times) and the turn of each dot, from 0 to 1 |
tint | none | A place in the palette for each dot (see Colors) |
once | false | Play one time and hold the last frame |
outcome | none | "success" or "failure", for screen readers |
input | none | Bake the value prop in this many steps, from 2 to 65 |
most | 9 | The most dots along the height that the pattern reads well at, from 3 to 64 |
The Scene reference lists every node with its parameters and defaults.
Use a scene in code
Type the scene as ScenePattern. TypeScript then checks each node and parameter.
import { DotMatrix } from "@/components/ui/dot-matrix/dot-matrix";
import type { ScenePattern } from "@/components/ui/dot-matrix/dot-matrix-scene";
const GROW: ScenePattern = {
duration: 1600,
light: {
op: "paint",
form: { form: "circle", radius: { fn: "pulse", low: 0.15, high: 0.65 } },
},
};
export function Grow() {
return <DotMatrix pattern={GROW} lattice="circle" />;
}Define the scene outside the component. The engine knows a scene by its object. A new object on each render bakes the pattern again.
A scene from a file or a paste goes through decodePreset first (see Presets). It names each unknown key and wrong value with its JSON path, so a typo never passes without a message.