⚠ 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
Layout and bounds
Manim mutates a retained mobject (circle.next_to(square, UP)). sophistry’s nodes are retained too, but positioning stays a pure function over boxes: you ask a node where it is (node.bounds()), a helper computes a delta, and you hand it back through the node’s place prop. Because place is the only thing a helper’s answer fits, a layout can never feed back into the size it measured.
This guide shows how to place shapes relative to each other, the frame, and a whole group of siblings.
A diagram laid out by placement
Three boxes in a row, captions under each, arrows between them, a frame around the lot and a title pinned to the corner. No coordinates are typed by hand.
27 collapsed lines
import type { ArrowProps, Placement, SceneNode, SurroundingRectangleProps,} from "@sophistry/core";import { arrange, DOWN, derive, LEFT, nextTo, ORIGIN, RIGHT, toCorner, UL, unionBounds,} from "@sophistry/core";import { Arrow, Rect, Stage, SurroundingRectangle, Text, useScene,} from "@sophistry/solidjs";import { onSettled } from "solid-js";
const STEPS = ["parse", "transform", "render"];
/** Any node with a `place` prop: shapes and labels. */type Placeable = SceneNode<{ place?: Placement }>;
function Diagram() { const scene = useScene(); const series = derive(scene.theme, (theme) => theme.palette.series); const muted = derive(scene.theme, (theme) => theme.palette.muted); const border = derive(scene.theme, (theme) => theme.palette.border);
// The nodes are the handles: once they exist, measure them and place them. const boxes: Placeable[] = []; const captions: Placeable[] = []; const arrows: SceneNode<ArrowProps>[] = []; let frame!: SceneNode<SurroundingRectangleProps>; let title!: Placeable;
onSettled(() => { const row = arrange( boxes.map((box) => box.localBounds()), RIGHT, { gap: 1.2 }, ); for (const [i, box] of boxes.entries()) { box.set("place", row[i]); const caption = captions[i]; caption.set( "place", nextTo(caption.localBounds(), box.bounds(), DOWN, { gap: 0.3 }), ); } // Another shape's bounds may feed geometry props (arrow ends, a frame). for (const [i, arrow] of arrows.entries()) { arrow.update({ from: boxes[i].pointAt(RIGHT) ?? ORIGIN, to: boxes[i + 1].pointAt(LEFT) ?? ORIGIN, }); } frame.set( "target", unionBounds(...[...boxes, ...captions].map((node) => node.bounds())), ); title.set( "place", toCorner(title.localBounds(), scene.frame.peek(), UL, { margin: 0.4 }), ); });
return ( <> {STEPS.map((step, i) => ( <> <Rect ref={(node) => boxes.push(node)} width={2.2} height={1.2} cornerRadius={0.2} stroke={derive(series, (s) => s[i])} fill={derive(series, (s) => s[i])} fillOpacity={0.25} /> <Text ref={(node) => captions.push(node)} text={step} fontSize={0.34} /> </> ))} {[0, 1].map(() => ( <Arrow ref={(node) => arrows.push(node)} from={ORIGIN} to={ORIGIN} strokeWidth={0.06} stroke={muted} /> ))} <SurroundingRectangle ref={(node) => (frame = node)} target={null} padding={0.4} cornerRadius={0.2} dash={[0.15, 0.1]} stroke={border} /> <Text ref={(node) => (title = node)} text="A tiny pipeline" fontSize={0.4} fontWeight={600} /> </> );}
export default function LabelledDiagram() { return ( <Stage units={12}> <Diagram /> </Stage> );}Scene units and the frame
Section titled “Scene units and the frame”Layout works in scene units, y up. scene.frame is the visible box in those units, as a readable that follows resizes (useScene().frame in a component). Pin things against it with toEdge and toCorner:
2 collapsed lines
import { derive, toCorner, UL } from "@sophistry/core";import { Text, Stage, useScene } from "@sophistry/solidjs";
function Title() { const scene = useScene(); return ( <Text text="Top left" ref={(title) => { const size = title.localBounds(); // `place` never changes it, so measure once title.set("place", derive(scene.frame, (frame) => toCorner(size, frame, UL, { margin: 0.4 }))); }} /> );}
export const Scene = () => ( <Stage units={10}> <Title /> </Stage>);useScene() needs the stage’s context, so call it in a child of <Stage>, as above. Without a framework the same is title.set("place", derive(scene.frame, ...)) on the node text.create(...) returned.
Handles and bounds
Section titled “Handles and bounds”The node is the handle. A component returns its node and ref={(node) => ...} receives it; in the core, create returns it. Every node measures itself in the stage’s scene units:
import { Circle, Text, Stage } from "@sophistry/solidjs";
export function Measured() { return ( <Stage units={8}> <Circle radius={1.5} ref={(circle) => console.log(circle.width())} /> <Text text="width: 3.0" y={-2.4} /> </Stage> );}| Member | Meaning |
|---|---|
bounds() |
The box after the node’s own place/x/y/rotation/scale, where it appears. |
localBounds() |
The box before those props: how big the node is and where it sat before anyone moved it. |
center() |
Center of bounds(). |
pointAt(direction) |
A point on the bounds() box: UP is the top edge’s midpoint, UR the top-right corner, { x: 0, y: 0 } the center. |
width(), height() |
Size of bounds(); 0 when the node has no points. |
Boxes are measured on demand: a call brings the node (and its parents) up to date first, so a box read right after a write is already right, and they come out in the stage’s scene units however many groups sit in between. They are null when a node has no points to bound.
A box is a value, not a subscription. To keep a placement in step with something that moves, derive it from what moves it: node.readable("center") is a readable of any node prop (including one bound to a track). See Following a moving target.
The split between the two boxes is what keeps layout from eating its own tail. A node placed with nextTo gets its place from a box, and place feeds only the node’s local transform. So the thing being placed reads its own localBounds() (which place can’t change), and the thing it is placed against is read through bounds().
Placement helpers
Section titled “Placement helpers”Each helper takes boxes and returns a Placement, a translation in scene units that you give to place. They follow Manim’s vocabulary:
| Helper | What it does |
|---|---|
nextTo(self, target, direction, { gap, align }) |
Put self beside target, with a gap. align flushes an edge on the other axis. |
alignTo(self, target, direction) |
Flush one edge with target’s; only the named axis moves. |
centerAt(self, point) |
Center self on a point. |
toEdge(self, frame, direction, { margin }) |
Pin against an edge of frame, inset by margin. |
toCorner(self, frame, direction, { margin }) |
Same with a diagonal direction (UL, UR, DL, DR). |
arrange(items, direction, { gap, center }) |
A row (or column) of items, equal gaps. One placement per item. |
arrangeInGrid(items, { columns, rows, gap, columnGap, rowGap, center }) |
Row-major grid; columns and rows size to their widest and tallest member. |
The direction constants are UP, DOWN, LEFT, RIGHT, the diagonals UL, UR, DL, DR, and ORIGIN.
Spacing has three names, used the same way everywhere (each defaults to DEFAULT_GAP, 0.25):
gap: the space between two separate things (nextTo,arrange, a brace and its target).padding: the space inside a surround (surround,SurroundingRectangle,LabeledDot).margin: the inset from a frame edge (toEdge,toCorner,Title).
Combine placements with shiftBy(placement, { x, y }), sumShifts(a, b), onlyX(p) and onlyY(p). x and y stay plain numbers for hand-written positions and simply add to place.
3 collapsed lines
import type { SceneNode } from "@sophistry/core";import { nextTo, UP } from "@sophistry/core";import { Circle, Text, Stage } from "@sophistry/solidjs";
export function Captioned() { let circle!: SceneNode; return ( <Stage units={8}> <Circle ref={(node) => (circle = node)} radius={1.5} /> <Text text="unit circle" ref={(caption) => caption.set("place", nextTo(caption.localBounds(), circle.bounds(), UP))} /> </Stage> );}The circle’s ref runs first because it comes first in the tree. The same in the core:
import { circle, createScene, text, nextTo, UP } from "@sophistry/core";
const scene = createScene({ host: document.body, units: 8 });const ring = scene.add(circle.create({ radius: 1.5 }));const caption = scene.add(text.create({ text: "unit circle" }));caption.set("place", nextTo(caption.localBounds(), ring.bounds(), UP));arrange takes one box per item and returns one placement per item, so it fits a handful of siblings collected through their refs (measure them once they all exist, in onSettled):
Labels and positioning
Line three shapes up with arrange(), then hang a caption beneath one with nextTo(), using each shape's measured bounds.
4 collapsed lines
import type { Placement, SceneNode } from "@sophistry/core";import { arrange, DOWN, nextTo, RIGHT } from "@sophistry/core";import { Circle, Square, Stage, Star, Text } from "@sophistry/solidjs";import { onSettled } from "solid-js";
/** Any node with a `place` prop: shapes and labels. */type Placeable = SceneNode<{ place?: Placement }>;
export default function LabelsAndPositioning() { // The nodes are the handles: once they exist, measure them and place them. const shapes: Placeable[] = []; let caption!: Placeable; onSettled(() => { const row = arrange( shapes.map((shape) => shape.localBounds()), RIGHT, { gap: 0.8 }, ); for (const [i, shape] of shapes.entries()) shape.set("place", row[i]); caption.set( "place", nextTo(caption.localBounds(), shapes[1].bounds(), DOWN, { gap: 0.6 }), ); });
return ( <Stage units={10}> <Circle ref={(node) => shapes.push(node)} radius={0.7} /> <Square ref={(node) => shapes.push(node)} size={1.4} /> <Star ref={(node) => shapes.push(node)} outerRadius={0.8} /> <Text ref={(node) => (caption = node)} text="arrange(RIGHT), then nextTo(DOWN)" /> </Stage> );}Why Placement only fits place
Section titled “Why Placement only fits place”center, from/to and points are what localBounds() measures, so feeding a placement back into them would make a shape’s input depend on its own output, a loop that never settles. A Placement is therefore opaque: it fits place and nothing else, and the mistake is a compile error.
Another node’s Bounds can feed geometry props, though. That’s how the arrows in the diagram above find their ends (arrow.update({ from: box.pointAt(RIGHT) })) and how a SurroundingRectangle follows its target: the box belongs to something else, so there is no loop.
Following a moving target
Section titled “Following a moving target”A placement computed once stays put. When the target moves (its center is a track an animation or a drag writes), derive the placement from the target’s prop. The node pulls it in the commit, measures the target there, and the caption moves in the same frame:
import { createScene, derive, dot, text, nextTo, trackPoint, UR } from "@sophistry/core";
const scene = createScene({ host: document.body, units: 10 });const position = trackPoint(0, 0);const point = scene.add(dot.create({ center: position }));const caption = scene.add(text.create({ text: "P" }));const size = caption.localBounds();caption.set("place", derive(point.readable("center"), () => nextTo(size, point.bounds(), UR)));position.set({ x: 2, y: 1 }); // the caption followsMeasure the placed node’s own localBounds() outside the derive, as above: it does not depend on place, and reading a node from inside its own binding is a loop. Often a derived position is simpler still: text.create({ x: derive(position, (p) => p.x), y: derive(position, (p) => p.y + 0.5) }). The dragging example does that.
Bounds algebra
Section titled “Bounds algebra”The pure box helpers come from @sophistry/core:
2 collapsed lines
import type { Bounds } from "@sophistry/core";import { boundsCenter, padBounds, unionBounds } from "@sophistry/core";
export function framed(a: Bounds, b: Bounds) { const both = unionBounds(a, b); // skips null/undefined entries if (!both) return null; const padded = padBounds(both, 0.3); // grow by 0.3 on every side return { box: padded, centre: boundsCenter(padded) };}The rest of the set is boundsWidth, boundsHeight, boundsFromCenter(center, width, height) and pointOn(bounds, direction).
surround(target, { padding }) is the padded box that Manim’s SurroundingRectangle is made of. It answers with a Bounds (not a Placement), so it goes into another shape’s geometry:
import { boundsCenter, boundsHeight, boundsWidth, circle, createScene, rect, surround } from "@sophistry/core";
const scene = createScene({ host: document.body, units: 8 });const ring = scene.add(circle.create({ radius: 1, x: 1 }));const box = surround(ring.bounds(), { padding: 0.3 });if (box) { scene.add(rect.create({ center: boundsCenter(box), width: boundsWidth(box), height: boundsHeight(box) }));}Usually you want a matcher instead: SurroundingRectangle, BackgroundRectangle, Cross and Underline take the node itself as target and follow it; see Shapes and styling.
Items that change slots
Section titled “Items that change slots”nextTo and arrange place things by measured size. When a collection of items changes order (a sort, a queue, a heap) the question is different: which key sits in which slot, and how does an item get to its new one? createSlots owns just that. You give it keys and a slot index -> point layout (rowSlots, gridSlots, circleSlots, or your own function), and it gives each item a position track (plus opacity and scale), so identity survives reordering.
3 collapsed lines
import { createSlots, rowSlots } from "@sophistry/core";import { Rect, Stage } from "@sophistry/solidjs";import { For } from "solid-js";
const slots = createSlots(["a", "b", "c"], { layout: rowSlots({ spacing: 1.5 }) });// Seekable animation: a and c trade slots over half a second.export const swap = slots.plan().swap("a", "c");
export const Row = () => ( <Stage units={8}> <For each={slots.items.peek()}> {(item) => <Rect center={item.position} width={1} height={1} />} </For> </Stage>);slots.items and slots.order are readables; the items list only changes on insert and remove.
Operations (swap, move, permute, rotate, insert, remove) return ordinary animations, so they compose and scrub like any other. The motion is a function you supply (an eased straight move by default, arcMotion for arcs), and commit: "start" | "end" decides when a reordering flips slots.order. See an algorithm visualizer for planning, scrubbing and the model in depth.
Pitfalls
Section titled “Pitfalls”- Nodes that don’t exist yet. A
refruns when its component creates the node, in tree order, so a sibling earlier in the tree cannot read a later one in its ownref. Measure inonSettled(or in the later node’sref). Every helper tolerates missing boxes (null,undefined) and answers with a zero shift. - Bounds mix units inside transformed groups. Node boxes are in the stage’s scene units, but
place,x,yand geometry props are stated in the units of the enclosing<Group>. Inside a plain translated group that is the same thing; inside a scaled or rotated group the units differ, so convert (with the inverse of the group’s matrix) before mixing them. A box is also always axis-aligned in stage units, so measuring something in a rotated group gives its upright bounding box, not a tilted frame. - Matcher depth. A matcher is an ordinary sibling, so nothing ties its depth to what it wraps.
BackgroundRectangledefaults tozIndex: -1; if the covered shape has an explicitzIndex, sort them by hand. See paint order. - Two
nextTos on one axis summed withsumShiftsgive neither of them. UseonlyX/onlyYto take one axis from each. - Don’t put a placement in a geometry prop. TypeScript will stop you; use
place.
Next steps
Section titled “Next steps”- Shapes and styling for the
place,x,y,rotationandscaleprops and<Group>. - Coordinate systems for placing things by data value instead.
- Extending to make a custom shape that measures and places like the built-ins.
- An algorithm visualizer for items that change slots.