Skip to content

⚠ Early release: v0.3.2.sophistry is not ready for production use. Until 1.0, any minor release may break the API; expect missing features and bugs too.Stability and versioning

Without a framework

The core is plain TypeScript. Every component in the Solid and React bindings is generated from a definition in the core or a feature package (Circle wraps circle), so anything the other galleries build with JSX can be built with createScene, definition.create(props) and scene.add(...) instead. These examples are the framework-free versions of the scenes the guides start with: a first scene, tracks and derive, springs, events and dragging, instancing, path batches and a custom shape. Start with FirstScene. Each file default-exports a mount(host, clock) function that builds its scene in host and returns the cleanup; in your own page, call createScene({ host }) and drop the clock, which the site passes so each example runs on its own clock.

A custom shape with defineShape

defineShape turns a props descriptor and a geometry function into a definition like circle; the new rose shape is created, drawn and animated.

CustomShape.ts
12 collapsed lines
import type { Clock, Point, ShapeProps } from "@sophistry/core";
import {
createScene,
defineShape,
draw,
Invalidate,
parallel,
sequence,
shapeProps,
track,
valueTo,
} from "@sophistry/core";
interface RoseProps extends ShapeProps {
center?: Point;
/** Number of petals (an odd count draws as many, an even one twice as many). */
petals: number;
size: number;
}
// Each prop names the cheapest stage it invalidates: a move is a new matrix, never a new outline.
const rose = defineShape<RoseProps>({
name: "Rose",
props: {
...shapeProps,
center: { invalidates: Invalidate.TRANSFORM, instanceable: true },
petals: { invalidates: Invalidate.GEOMETRY },
size: { invalidates: Invalidate.GEOMETRY, instanceable: true },
},
geometry: ({ petals, size }) => ({
closed: true,
polylines: [
Array.from({ length: 360 }, (_, i) => {
const t = (i / 360) * 2 * Math.PI;
const radius = size * Math.cos(petals * t);
return { x: radius * Math.cos(t), y: radius * Math.sin(t) };
}),
],
}),
offset: (props) => props.center,
});
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 10 });
const drawn = track(0);
const size = track(1.5);
const rotation = track(0);
scene.add(
rose.create({
petals: 5,
size,
progress: drawn,
rotation,
stroke: "#cba6f7",
fill: "#cba6f7",
fillOpacity: 0.2,
}),
);
void scene.play(
sequence(
draw(drawn, { duration: 2 }),
parallel(
valueTo(size, 2.6, { duration: 1.2 }),
valueTo(rotation, Math.PI / 5, { duration: 1.2 }),
),
),
);
return () => scene.dispose();
}

Dragging and node events

draggable() makes a handle write a point track; a line and a caption follow it, and node.on() listens for hover and taps.

DragAndEvents.ts
10 collapsed lines
import type { Clock } from "@sophistry/core";
import {
circle,
createScene,
derive,
draggable,
line,
text,
trackPoint,
} from "@sophistry/core";
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 10 });
const handle = trackPoint(2, 1);
scene.add(
line.create({
from: { x: 0, y: 0 },
to: handle,
stroke: "#6c7086",
dash: [0.15, 0.1],
}),
);
const knob = scene.add(
circle.create({
center: handle,
radius: 0.3,
fill: "#89b4fa",
label: "handle", // keyboard focus: arrow keys nudge it
...draggable(handle),
}),
);
// The caption follows the handle: a derived position, no event code.
scene.add(
text.create({
text: derive(handle, (p) => `(${p.x.toFixed(1)}, ${p.y.toFixed(1)})`),
x: derive(handle, (p) => p.x),
y: derive(handle, (p) => p.y + 0.6),
}),
);
// Events on the node itself; each `on` returns its unsubscribe.
knob.on("enter", () => knob.set("fill", "#f9e2af"));
knob.on("leave", () => knob.set("fill", "#89b4fa"));
knob.on("doubletap", () => handle.set({ x: 2, y: 1 }));
scene.add(
text.create({ text: "drag the dot · double-tap to reset", y: -3.2 }),
);
return () => scene.dispose();
}

A first scene, no framework

createScene, a few definitions and two tracks: draw a ring, then move a point along it, from plain TypeScript.

FirstScene.ts
13 collapsed lines
import type { Clock } from "@sophistry/core";
import {
circle,
createScene,
dot,
draw,
line,
moveTo,
sequence,
text,
track,
trackPoint,
} from "@sophistry/core";
/** Builds the scene in `host`, on `clock`; returns its cleanup. */
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 10 });
const drawn = track(0);
const tip = trackPoint(2.4, 0);
// `definition.create(props)` makes a node; a track given as a prop binds.
scene.add(circle.create({ radius: 2.4, progress: drawn }));
scene.add(line.create({ from: { x: 0, y: 0 }, to: tip, stroke: "#89b4fa" }));
scene.add(dot.create({ center: tip }));
scene.add(text.create({ text: "hello", y: -3.2 }));
void scene.play(
sequence(
draw(drawn, { duration: 1.5 }),
moveTo(tip, { x: 0, y: 2.4 }, { duration: 1 }),
),
);
return () => scene.dispose();
}

Instancing: thousands of dots, one node

instances() draws 4,000 circles as one node with per-item channels; changing its data tweens every dot to a new layout with a stagger.

Instancing.ts
2 collapsed lines
import type { Clock } from "@sophistry/core";
import { circle, createScene, instances, text } from "@sophistry/core";
interface Item {
id: number;
x: number;
y: number;
}
const COUNT = 4000;
const COLORS = ["#89b4fa", "#a6e3a1", "#f9e2af", "#f38ba8"];
/** A spiral (sunflower) layout, or a grid. */
function layout(spiral: boolean): Item[] {
return Array.from({ length: COUNT }, (_, id) => {
if (!spiral)
return {
id,
x: ((id % 80) - 39.5) * 0.18,
y: (Math.floor(id / 80) - 24.5) * 0.18,
};
const radius = 0.07 * Math.sqrt(id);
const angle = id * 2.39996;
return { id, x: radius * Math.cos(angle), y: radius * Math.sin(angle) };
});
}
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 16 });
const dots = instances(circle, {
data: layout(true),
key: (item) => item.id,
channels: { center: (item) => item, fill: (item) => COLORS[item.id % 4] },
shared: { radius: 0.06, stroke: "none" },
transition: {
duration: 1.2,
stagger: 0.0002,
enter: { from: { opacity: 0 } },
},
});
scene.add(dots);
scene.add(
text.create({
text: "4,000 dots, one node: click to re-lay them",
y: -4.1,
}),
);
let spiral = true;
const toggle = () => {
spiral = !spiral;
dots.set("data", layout(spiral));
};
host.addEventListener("click", toggle);
return () => {
host.removeEventListener("click", toggle);
scene.dispose();
};
}

PathBatch: many outlines, one node

A field of 600 arrowheads, each its own outline and colour, drawn by one pathBatch node whose items derive from a single phase track.

PathBatch.ts
10 collapsed lines
import type { Clock, PathBatchItem } from "@sophistry/core";
import {
createScene,
derive,
forever,
pathBatch,
text,
track,
valueTo,
} from "@sophistry/core";
/** An arrowhead at `(x, y)` pointing along `angle`. */
function arrow(
x: number,
y: number,
angle: number,
length: number,
): PathBatchItem {
const at = (along: number, across: number) => ({
x: x + along * Math.cos(angle) - across * Math.sin(angle),
y: y + along * Math.sin(angle) + across * Math.cos(angle),
});
const hue = Math.round(((angle / Math.PI) * 180 + 360) % 360);
return {
closed: true,
polylines: [
[
at(length, 0),
at(-length / 2, length / 2),
at(-length / 4, 0),
at(-length / 2, -length / 2),
],
],
fill: `hsl(${hue} 70% 70%)`,
};
}
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 16 });
const phase = track(0);
// One node, 600 distinct outlines, rebuilt from the phase each frame it moves.
const items = derive(phase, (p) =>
Array.from({ length: 600 }, (_, i) => {
const x = (i % 30) * 0.5 - 7.25;
const y = Math.floor(i / 30) * 0.4 - 3.8;
const angle = Math.sin(x * 0.6 + p) + Math.cos(y * 0.8 - p);
return arrow(x, y, angle * 1.5, 0.12 + 0.06 * Math.abs(Math.sin(angle)));
}),
);
scene.add(pathBatch.create({ items, stroke: "none" }));
scene.add(text.create({ text: "600 outlines, one PathBatch node", y: -4.3 }));
void scene.play(
forever(
valueTo(phase, (from) => from + 2 * Math.PI, {
duration: 6,
easing: "linear",
}),
),
);
return () => scene.dispose();
}

Springs: springTo and follow

springTo is a seekable spring animation with an overshoot; follow keeps a second point chasing the first, live, preserving its velocity.

Springs.ts
11 collapsed lines
import type { Clock } from "@sophistry/core";
import {
circle,
createScene,
follow,
sequence,
springTo,
text,
track,
trackPoint,
} from "@sophistry/core";
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 10 });
const leader = trackPoint(-3, 0);
const follower = trackPoint(-3, 0);
const size = track(0.2);
scene.add(
circle.create({
center: follower,
radius: 0.35,
stroke: "#a6e3a1",
strokeWidth: 0.06,
}),
);
scene.add(
circle.create({
center: leader,
radius: size,
fill: "#f38ba8",
stroke: "none",
}),
);
scene.add(text.create({ text: "springTo (pink) · follow (green)", y: -3.2 }));
// A live follower on the scene's clock: retargets keep the velocity.
const stop = follow(
follower,
leader,
{ stiffness: 60, damping: 8 },
{ clock: scene.clock },
);
const bouncy = { stiffness: 180, damping: 9 };
void scene.play(
springTo(size, 0.4, bouncy),
sequence(
springTo(leader, { x: 3, y: 1.5 }, bouncy),
springTo(leader, { x: 0, y: -1.5 }, bouncy),
springTo(leader, { x: -3, y: 0 }, { stiffness: 120, damping: 20 }),
),
);
return () => {
stop();
scene.dispose();
};
}

Tracks, derive and watch

One track drives a dot, a radius, an arc and a caption through derive; watch mirrors it into the page once per frame.

TracksAndDerive.ts
14 collapsed lines
import type { Clock } from "@sophistry/core";
import {
arc,
circle,
createScene,
derive,
dot,
forever,
line,
text,
track,
valueTo,
watch,
} from "@sophistry/core";
const RADIUS = 2.4;
export default function mount(host: HTMLElement, clock?: Clock): () => void {
const scene = createScene({ host, clock, units: 10 });
const turns = track(0);
// Derived values recompute lazily, at most once per change, when read.
const angle = derive(turns, (t) => (t % 1) * 2 * Math.PI);
const tip = derive(angle, (a) => ({
x: RADIUS * Math.cos(a),
y: RADIUS * Math.sin(a),
}));
const degrees = derive(angle, (a) => Math.round((a * 180) / Math.PI));
scene.add(circle.create({ radius: RADIUS, stroke: "#6c7086" }));
scene.add(line.create({ from: { x: 0, y: 0 }, to: tip, stroke: "#89b4fa" }));
scene.add(arc.create({ radius: 0.8, angle, stroke: "#f9e2af" }));
scene.add(dot.create({ center: tip, color: "#f38ba8" }));
scene.add(
text.create({ text: derive(degrees, (d) => `θ = ${d}°`), x: 4, y: -2.2 }),
);
// A side effect outside the scene: runs in the commit phase, coalesced.
const readout = host.appendChild(document.createElement("output"));
const stop = watch(degrees, (d) => {
readout.textContent = `angle: ${d}°`;
});
void scene.play(
forever(valueTo(turns, (from) => from + 0.25, { duration: 1.2 })),
);
return () => {
stop();
readout.remove();
scene.dispose();
};
}