⚠ 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
Your first scene
In this tutorial you build one small scene, a point travelling around a circle with its angle marked, and give it each of sophistry’s superpowers in turn: it draws itself, you can drag it, and you can scrub its whole script like a video. Every step ends in a live example you can copy.
The steps use the Solid binding (@sophistry/solidjs). The scene graph underneath is plain TypeScript, so the end of the page shows the same scene without a framework and in React. You need a project set up as described in Installation.
-
Mount a stage
A
<Stage>makes a scene (createScenein the core) in a<div>, with a canvas inside. Its children are scene components such asCircleandText. They render no DOM: each one creates a single node in the scene graph, and from then on the core owns it.import { Circle, Stage } from "@sophistry/solidjs";export function Hello() {return (<Stage units={10}><Circle radius={2.4} /></Stage>);}You work in scene units, never pixels.
units={10}means ten units span the stage’s width, and y points up, with the origin in the middle. The stage fills its container, and everything (strokes, fonts, dashes) is measured in units, so the scene looks the same at any size or pixel density. -
Add shapes and labels
Add a radius
Line, anArcfor the angle and aDot. Colours come from the scene’s theme (useScene().theme, a value you canderivefrom), so they follow light and dark mode.Labels are where layout starts. A component returns its node, and
ref={(node) => ...}hands it to you. Nodes measure themselves (node.bounds(),node.localBounds()), andnextToturns two boxes into aplaceprop. Here the label “P” sits up and to the right of the dot.First scene: shapes and labels
Step one of the first-scene tutorial: a ring, a radius, an angle arc and a dot, with a label placed next to the dot by its measured bounds.
FirstSceneShapes.tsx 11 collapsed linesimport type { SceneNode } from "@sophistry/core";import { derive, nextTo, UR } from "@sophistry/core";import {Arc,Circle,Dot,Line,Stage,Text,useScene,} from "@sophistry/solidjs";const R = 2.4;const THETA = 0.9;function Figure() {const theme = useScene().theme;const grid = derive(theme, (t) => t.palette.grid);const first = derive(theme, (t) => t.palette.series[0]);const second = derive(theme, (t) => t.palette.series[1]);const tip = { x: R * Math.cos(THETA), y: R * Math.sin(THETA) };let dot!: SceneNode;return (<><Circle radius={R} stroke={grid} /><Line from={{ x: 0, y: 0 }} to={tip} stroke={first} /><Arc radius={0.8} angle={THETA} stroke={second} /><Text text="θ" x={1.15} y={0.35} color={second} /><Dot ref={(node) => (dot = node)} center={tip} /><Texttext="P"ref={(name) =>name.set("place", nextTo(name.localBounds(), dot.bounds(), UR))}/></>);}export default function FirstSceneShapes() {return (<Stage units={10}><Figure /></Stage>);}A node is the handle: there is no separate handle object. See Layout for
arrange,toEdgeand friends. -
Animate with tracks
Nothing above moves, because nothing is a track yet. Replace the constants with tracks:
track(0)is a small observable value withpeek()andset(). Pass the track itself as a prop (angle={theta}), and usederivefor values computed from it (to={derive(theta, ...)}). The node binds to it: when an animation writes the track, the node recomputes in the next frame, and no Solid code runs per frame.Run the script with
usePlayer(). It gives youplayandtimelinebound to the stage’s clock, and aborts anything still running when the component is removed. It must be called in a component inside the<Stage>, so the scene moves into its own component. Start the script fromonSettled, Solid 2’s after-mount hook.First scene: animate with a player
Step two of the first-scene tutorial: tracks drive the ring, the radius and the angle, and usePlayer runs the script on the stage clock.
FirstSceneAnimated.tsx 12 collapsed linesimport { derive, draw, sequence, track, valueTo } from "@sophistry/core";import {Arc,Circle,Dot,Line,Stage,Text,usePlayer,useScene,} from "@sophistry/solidjs";import { onSettled } from "solid-js";const RADIUS = 2.4;const THETA = 1.1;function Scene() {const theme = useScene().theme;const palette = derive(theme, (t) => t.palette);const { play } = usePlayer();const drawn = track(0);const theta = track(0);const tip = derive(theta, (theta) => ({x: RADIUS * Math.cos(theta),y: RADIUS * Math.sin(theta),}));onSettled(() => {void play(sequence(draw(drawn, { duration: 1.2 }),valueTo(theta, THETA, { duration: 1.6, easing: "smooth" }),),);});return (<><Circleradius={RADIUS}stroke={derive(palette, (p) => p.grid)}progress={drawn}/><Linefrom={{ x: 0, y: 0 }}to={tip}stroke={derive(palette, (p) => p.series[0])}/><Arcradius={0.8}angle={theta}stroke={derive(palette, (p) => p.series[1])}/>{/* On the angle's bisector, fading in as the angle opens. */}<Texttext="θ"x={derive(theta, (theta) => 1.15 * Math.cos(theta / 2))}y={derive(theta, (theta) => 1.15 * Math.sin(theta / 2))}opacity={derive(theta, (theta) => Math.min(1, theta / THETA))}color={derive(palette, (p) => p.series[1])}/><Dot center={tip} /></>);}export default function FirstSceneAnimated() {return (<Stage units={10}><Scene /></Stage>);}draw(drawn)reveals a shape by driving itsprogressprop from 0 to 1.valueTo(theta, 1.1)tweens any number.sequence(a, b)runs them one after the other. The shapes only know their props; they have no idea an animation is running. -
Make it interactive
Because
thetais just a track, anything can write to it. Spreaddraggable(...)onto the dot with anangleParam, which turns the pointer position into an angle around the origin, and now dragging the dot setstheta, the radius and arc follow, and the readout updates.First scene: make it draggable
Step three of the first-scene tutorial: the same theta track that the animation writes is now also written by dragging the dot around the ring.
FirstSceneDraggable.tsx 20 collapsed linesimport {angleParam,derive,draggable,draw,sequence,track,valueTo,} from "@sophistry/core";import {Arc,Circle,Dot,Line,Stage,Text,usePlayer,useTheme,} from "@sophistry/solidjs";import { onSettled } from "solid-js";const R = 2.4;function Scene() {const theme = useTheme();const palette = () => theme().palette;const player = usePlayer();const drawn = track(0);const theta = track(0);const tip = derive(theta, (angle) => ({x: R * Math.cos(angle),y: R * Math.sin(angle),}));const degrees = derive(theta, (angle) => Math.round((angle * 180) / Math.PI));onSettled(() => {void player.play(sequence(draw(drawn, { duration: 1.2 }),valueTo(theta, 1.1, { duration: 1.6, easing: "smooth" }),),);});return (<><Circle radius={R} stroke={palette().grid} progress={drawn} /><Line from={{ x: 0, y: 0 }} to={tip} stroke={palette().series[0]} /><Arc radius={0.8} angle={theta} stroke={palette().series[1]} /><Dotcenter={tip}radius={0.16}label="angle"{...draggable(theta, {param: angleParam({ x: 0, y: 0 }, { radius: R }),})}/>{/* Beside the circle: below it would fall off the 16:9 stage. */}<Texttext={derive(degrees, (value) => `θ = ${value}°`)}x={3.8}y={-2.2}color={palette().muted}/></>);}export default function FirstSceneDraggable() {return (<Stage units={10}><Scene /></Stage>);}The animation and the drag are writing the same track, with no glue between them. The
label="angle"prop also makes the dot keyboard-focusable: Tab to it and use the arrow keys. See Interaction for constraints, drop targets and more. -
Scrub it with a timeline
play()is fire and forget.timeline()wraps the same animation in a controller you can pause and seek:tl.seek(2)jumps to the two-second mark and writes that state to every track, backwards as well as forwards. The timeline’s time is a track too, so a slider reads it like any other value.This version creates the clock itself with
createClock()and gives it to both the<Stage clock>and the timeline, so the controls can live outside the stage.First scene: scrub it with a timeline
Step four of the first-scene tutorial: the whole script becomes a timeline you can seek with a slider, backwards as well as forwards.
FirstSceneTimeline.tsx 20 collapsed linesimport {derive,draw,sequence,type Track,track,valueTo,} from "@sophistry/core";import {Arc,Circle,Dot,Line,Stage,Text,usePlayer,useScene,useTrackValue,} from "@sophistry/solidjs";import { onSettled } from "solid-js";const RADIUS = 2.4;/** The ring, radius and angle, coloured from the stage's theme. */function Ring(props: { drawn: Track<number>; theta: Track<number> }) {const palette = derive(useScene().theme, (t) => t.palette);const tip = derive(props.theta, (theta) => ({x: RADIUS * Math.cos(theta),y: RADIUS * Math.sin(theta),}));return (<><Circleradius={RADIUS}stroke={derive(palette, (p) => p.grid)}progress={props.drawn}/><Linefrom={{ x: 0, y: 0 }}to={tip}stroke={derive(palette, (p) => p.series[0])}/><Arcradius={0.8}angle={props.theta}stroke={derive(palette, (p) => p.series[1])}/><Dot center={tip} /></>);}export default function FirstSceneTimeline() {const drawn = track(0);const theta = track(0);const tl = usePlayer().timeline(sequence(draw(drawn, { duration: 1.2 }),valueTo(theta, 2 * Math.PI, { duration: 3, easing: "smooth" }),),);const time = useTrackValue(tl.time);const playing = useTrackValue(tl.playing);onSettled(() => void tl.play());return (<><Stage units={10} aspect={4 / 3}><Ring drawn={drawn} theta={theta} /><Text text={derive(tl.time, (t) => `t = ${t.toFixed(1)} s`)} y={-3} /></Stage><divstyle={{display: "flex",gap: "0.75rem","align-items": "center","margin-top": "0.5rem",}}><buttontype="button"onClick={() => (playing() ? tl.pause() : void tl.play())}>{playing() ? "Pause" : "Play"}</button><inputtype="range"aria-label="Timeline position"style={{ flex: "1" }}min={0}max={tl.duration}step="any"value={time()}onInput={(e) => {tl.pause();tl.seek(Number(e.currentTarget.value));}}/></div></>);}
The same scene without a framework
Section titled “The same scene without a framework”The components above are generated from core definitions: <Circle> is wrap(circle). Without a framework you use the definitions directly. createScene({ host }) mounts a scene in any element, definition.create(props) makes a node, scene.add(node) puts it in the tree, and scene.play(...) runs animations on the scene’s clock. Tracks bind exactly as in JSX.
A first scene, no framework
createScene, a few definitions and two tracks: draw a ring, then move a point along it, from plain TypeScript.
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();}The site passes each example its own clock; in your app, leave clock out and the scene uses the shared default. scene.dispose() removes the canvas and releases everything the scene holds. More framework-free scenes are in the gallery.
The same scene in React
Section titled “The same scene in React”The React binding has the same components and hooks, generated from the same definitions. Make tracks with useTrack (so they survive re-renders) and start scripts in an effect. A track passed as a prop binds in the core here too, so an animation causes no React renders at all.
3 collapsed lines
import { derive, draw, sequence, valueTo } from "@sophistry/core";import { Arc, Circle, Dot, Line, Stage, usePlayer, useTrack } from "@sophistry/react";import { useEffect, useMemo } from "react";
const RADIUS = 2.4;
function Scene() { const drawn = useTrack(0); const theta = useTrack(0); const tip = useMemo( () => derive(theta, (angle) => ({ x: RADIUS * Math.cos(angle), y: RADIUS * Math.sin(angle) })), [theta], ); const player = usePlayer(); useEffect(() => { void player.play(sequence(draw(drawn, { duration: 1.2 }), valueTo(theta, 1.1, { duration: 1.6 }))); }, [player, drawn, theta]);
return ( <> <Circle radius={RADIUS} progress={drawn} /> <Line from={{ x: 0, y: 0 }} to={tip} /> <Arc radius={0.8} angle={theta} /> <Dot center={tip} /> </> );}
export function FirstScene() { return ( <Stage units={10}> <Scene /> </Stage> );}The site does not run React, so this block is not live. Bindings covers how the two bindings differ.
What you built
Section titled “What you built”You used the four ideas that the rest of the library elaborates:
- A scene is a tree of nodes, each made from a definition; JSX is one way to build it.
- State lives in tracks, and a track given as a prop binds: the node follows it.
- Animations are seekable descriptions run by a clock, so they can be played, paused and scrubbed.
- Anything (a tween, a drag, a slider) can write a track.
Next steps
Section titled “Next steps”- Core concepts: the scene graph, tracks and the frame loop in more depth
- Animation: sequencing, emphasis effects and morphing
- Timelines: seeking, chapters and speed
- Interaction: dragging, constraints, keyboard access
- Recipe: a scrubbable timeline: this step’s slider in detail
- Gallery: live, copyable examples for every feature