Save a loader's whole look as one JSON file, share it, and load presets from other people safely with decodePreset.
Engine · 2 min read
A preset is a loader's whole look in one JSON file: the pattern (a name or a full scene), the lattice, the dots, the colors and the motion. Save one, send it, and load it in any app that has the engine.
The file
{
"kind": "dot-matrix-preset",
"version": 1,
"name": "Radar",
"pattern": "radar",
"lattice": "circle",
"dot": "round",
"colors": ["#2BD4E0"]
}kind, version, name and pattern are required. The rest are optional: description, lattice, dot, glow, floor, speed, colors, morph and grid.
Show a preset
A decoded preset fits the look prop as it is. Props you pass win over it.
import { parsePreset } from "@/components/ui/dot-matrix/dot-matrix-preset";
const result = parsePreset(text);
if (result.ok) {
return <DotMatrix look={result.preset} size={32} />;
}
return <p>{result.problems.join("\n")}</p>;Load presets from other people
A preset runs in the page of the person who opens it. So the engine checks every preset before it runs:
parsePresetreads text. It refuses text over 2 MB before it parses it.decodePresetchecks parsed JSON, such as a fetch response.- Both return
{ ok: true, preset }, or{ ok: false, problems }with a JSON path for each problem. - An unknown key is a problem, so a typo never passes without a message.
The limits keep a shared preset from asking for too much work or memory:
| Key or part | Allowed |
|---|---|
name | Text, 1 to 80 characters |
description | Text, up to 400 characters |
pattern | A built-in name, or a scene within the scene limits |
lattice | A layout name, or { cols, rows, mask?, hex? } with 1 to 64 columns and rows |
dot | One of the 9 dot shapes |
glow, floor | 0 to 1 |
speed | 0.05 to 10 |
colors | 1 to 8 plain CSS colors |
morph | A morph name or "none" |
grid | 1 to 64 |
A scene's duration | 100ms to 30,000ms |
A scene's input | 2 to 65 steps |
A scene's most | 3 to 64 |
| An image | A PNG, JPEG, GIF or WebP data URL, up to 600,000 characters |
Colors must be plain CSS colors: hex, a name, or a color function with numbers, such as oklch(). So a preset cannot carry a var() or a url(). Images must be data URLs, so a preset never loads a file from another site.
Write a preset
encodePreset turns a preset into pretty JSON text, for a file or the clipboard.
import {
encodePreset,
PRESET_KIND,
PRESET_VERSION,
} from "@/components/ui/dot-matrix/dot-matrix-preset";
const text = encodePreset({
kind: PRESET_KIND,
version: PRESET_VERSION,
name: "Calm pulse",
pattern: "breathe",
lattice: "circle",
colors: ["#9D7BFF"],
speed: 0.75,
});