⚠ 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
3D
@sophistry/3d projects 3D into 2D: a <Camera3D> (from @sophistry/solidjs/3d) inside a normal <Stage> turns points into screen positions, and 3D primitives paint onto the same canvas, so tracks, animations and themes work unchanged. The camera eye is a plain track, which is why rotateBy can spin it and useOrbitControls3D can drag it. 3D charts come from @sophistry/charts/3d.
Start with OrbitScene, then HelixCurve and SurfacePlot3D. CameraMotion shows animating the camera.
Orbit a 3D scene
Drag to orbit a cube and a sphere; the camera eye is a plain track.
9 collapsed lines
import { track } from "@sophistry/core";import { Stage } from "@sophistry/solidjs";import { Axes3D, Camera3D, Cube3D, Sphere3D, useOrbitControls3D,} from "@sophistry/solidjs/3d";
function Scene() { const eye = track({ x: 6, y: 4, z: 8 }); // Dragging empty space writes `eye`; the camera is bound to it. useOrbitControls3D({ eye });
return ( <Camera3D eye={eye}> <Axes3D length={3} /> <Cube3D center={{ x: -1.5, y: 0.5, z: 0 }} size={1} /> <Sphere3D center={{ x: 1.5, y: 0.7, z: 0 }} radius={0.8} /> </Camera3D> );}
export default function OrbitScene() { return ( <Stage units={10}> <Scene /> </Stage> );}Helix curve
A parametric 3D curve drawn in by draw(); drag to orbit around the helix.
9 collapsed lines
import { draw, track } from "@sophistry/core";import { Stage, usePlayer } from "@sophistry/solidjs";import { Axes3D, Camera3D, Curve3D, useOrbitControls3D,} from "@sophistry/solidjs/3d";import { onSettled } from "solid-js";
function Helix() { const { play } = usePlayer(); const eye = track({ x: 6, y: 3, z: 7 }); const drawn = track(0); useOrbitControls3D({ eye });
onSettled(() => { void play(draw(drawn, { duration: 3 })); });
return ( <Camera3D eye={eye}> <Axes3D length={3} /> <Curve3D samples={200} progress={drawn} function={(t) => ({ x: 1.5 * Math.cos(t * 6 * Math.PI), y: t * 4 - 2, z: 1.5 * Math.sin(t * 6 * Math.PI), })} /> </Camera3D> );}
export default function HelixCurve() { return ( <Stage units={10}> <Helix /> </Stage> );}Surface plot
A rippling surface y = f(x, z) sampled from a function of two parameters; drag to orbit around it.
8 collapsed lines
import { track } from "@sophistry/core";import { Stage } from "@sophistry/solidjs";import { Axes3D, Camera3D, SurfacePlot3D, useOrbitControls3D,} from "@sophistry/solidjs/3d";
function Ripple() { const eye = track({ x: 5, y: 4, z: 7 }); useOrbitControls3D({ eye });
return ( <Camera3D eye={eye}> <Axes3D length={3} /> <SurfacePlot3D resolution={32} function={(u, v) => { const x = (u - 0.5) * 5; const z = (v - 0.5) * 5; return { x, y: Math.cos(1.6 * Math.hypot(x, z)) * 0.8, z }; }} /> </Camera3D> );}
export default function SurfacePlot3DExample() { return ( <Stage units={10}> <Ripple /> </Stage> );}3D bar chart
A 3 by 3 grid of bars whose heights come from one array; each box is anchored at its corner and rises along y.
4 collapsed lines
import { track } from "@sophistry/core";import { Stage } from "@sophistry/solidjs";import { Axes3D, Camera3D, useOrbitControls3D } from "@sophistry/solidjs/3d";import { BarChart3D } from "@sophistry/solidjs/charts/3d";
const HEIGHTS = [1, 2, 1.5, 2.5, 3, 2, 1, 1.8, 0.8];const TARGET = { x: 1.5, y: 1, z: 1.5 };
function Bars() { const eye = track({ x: 6, y: 5, z: 8 }); // Orbit around the camera's target, the middle of the grid. useOrbitControls3D({ eye, target: TARGET });
return ( <Camera3D eye={eye} target={TARGET}> <Axes3D length={4} /> <BarChart3D data={HEIGHTS} x={(_, i) => (i % 3) * 1.3} y={0} z={(_, i) => Math.floor(i / 3) * 1.3} height={(height) => height} /> </Camera3D> );}
export default function BarChart3DExample() { return ( <Stage units={10}> <Bars /> </Stage> );}3D stem plot
Points on a circle with drop lines falling along y to the floor, each stem as tall as its y value.
4 collapsed lines
import { track } from "@sophistry/core";import { Stage } from "@sophistry/solidjs";import { Axes3D, Camera3D, useOrbitControls3D } from "@sophistry/solidjs/3d";import { Stem3D } from "@sophistry/solidjs/charts/3d";
const ANGLES = Array.from({ length: 16 }, (_, i) => (i * Math.PI) / 8);
function Stems() { const eye = track({ x: 5, y: 4, z: 7 }); useOrbitControls3D({ eye });
return ( <Camera3D eye={eye}> <Axes3D length={3} /> <Stem3D data={ANGLES} x={(a) => 2 * Math.cos(a)} y={(a) => 1.5 + Math.sin(2 * a)} z={(a) => 2 * Math.sin(a)} markerRadius={0.1} /> </Camera3D> );}
export default function StemPlot3D() { return ( <Stage units={10}> <Stems /> </Stage> );}Camera motion
The camera eye is derived from an angle track, so a plain rotateBy() loop turns the camera around the scene.
4 collapsed lines
import { derive, rotateBy, track } from "@sophistry/core";import { Stage, usePlayer } from "@sophistry/solidjs";import { Axes3D, Camera3D, Cube3D, Sphere3D } from "@sophistry/solidjs/3d";import { onSettled } from "solid-js";
export default function CameraMotion() { const { play, signal } = usePlayer(); const angle = track(0.6); const eye = derive(angle, (angle) => ({ x: 9 * Math.cos(angle), y: 4, z: 9 * Math.sin(angle), }));
onSettled(() => { void (async () => { while (!signal.aborted) { await play( rotateBy(angle, 2 * Math.PI, { duration: 10, easing: "linear" }), ); } })(); });
return ( <Stage units={10}> <Camera3D eye={eye}> <Axes3D length={3} /> <Cube3D center={{ x: -1.5, y: 0.5, z: 0 }} size={1} /> <Sphere3D center={{ x: 1.5, y: 0.7, z: 0 }} radius={0.8} /> </Camera3D> </Stage> );}The platonic solids
All five regular solids side by side, outlined so their faces read. Drag to orbit.
11 collapsed lines
import { derive, track } from "@sophistry/core";import { Stage, useScene } from "@sophistry/solidjs";import { Camera3D, Cube3D, Dodecahedron3D, Icosahedron3D, Octahedron3D, Tetrahedron3D, useOrbitControls3D,} from "@sophistry/solidjs/3d";
const at = (x: number) => ({ x, y: 0, z: 0 });
function Solids() { const eye = track({ x: 2, y: 4, z: 10 }); useOrbitControls3D({ eye }); // Derived from the scene's theme, so the colours follow it. const theme = useScene().theme; const color = (i: number) => derive(theme, (t) => t.palette.series[i]); const edges = { stroke: derive(theme, (t) => t.palette.foreground), strokeWidth: 0.015, }; return ( <Camera3D eye={eye}> <Tetrahedron3D center={at(-5)} edgeLength={2} fill={color(0)} {...edges} /> <Cube3D center={at(-2.6)} size={1.6} fill={color(1)} {...edges} /> <Octahedron3D center={at(0)} edgeLength={1.6} fill={color(2)} {...edges} /> <Icosahedron3D center={at(2.6)} edgeLength={1.2} fill={color(3)} {...edges} /> <Dodecahedron3D center={at(5.3)} edgeLength={0.8} fill={color(4)} {...edges} /> </Camera3D> );}
export default function PlatonicSolids() { return ( <Stage units={12}> <Solids /> </Stage> );}Surface from coordinate grids
A saddle z = x^2 - y^2 given as three matrices (xs, ys, zs, like numpy's meshgrid), drawn as shaded quads with a wireframe on top. viewHeight sets how large the scene projects.
4 collapsed lines
import { track } from "@sophistry/core";import { Stage } from "@sophistry/solidjs";import { Camera3D, Grid3D, useOrbitControls3D } from "@sophistry/solidjs/3d";import { SurfaceGrid3D, Wireframe3D } from "@sophistry/solidjs/charts/3d";
const axis = Array.from({ length: 21 }, (_, i) => -2 + i * 0.2);// Row index runs along z, column index along x; height (world y) is the saddle.const xs = axis.map(() => axis);const zs = axis.map((z) => axis.map(() => z));const ys = zs.map((row, i) => row.map((z, j) => 0.35 * (xs[i][j] ** 2 - z * z)),);
function Saddle() { const eye = track({ x: 6, y: 5, z: 7 }); useOrbitControls3D({ eye });
return ( <Camera3D eye={eye} viewHeight={10}> <Grid3D extent={3} offset={-1.5} /> <SurfaceGrid3D xs={xs} ys={ys} zs={zs} /> <Wireframe3D xs={xs} ys={ys} zs={zs} stroke="#ffffff" /> </Camera3D> );}
export default function SurfaceGridWireframe() { return ( <Stage units={10}> <Saddle /> </Stage> );}