API Reference
For explanations and usage patterns, see the guide.
Method signatures, parameters, and behavior.
Pluton2D
Constructor
const scene = new Pluton2D(svg, {
params: { width: 240, height: 120 },
viewBox: { width: 500, height: 500 },
});
| Param | Type | Description |
|---|
svg | SVGSVGElement | SVG element to render into. Needs CSS width/height or viewBox |
options.params | object | Optional flat object. Keys become reactive params. Nested objects throw at construction |
options.viewBox | { width, height } | Optional coordinate space dimensions |
Params must be flat. Reactivity tracks top-level mutations via Proxy. Keep complex state outside scene.params and sync flat values in your draw callback.
ViewBox priority order:
- Constructor
viewBox parameter
- SVG
viewBox attribute
- SVG pixel dimensions (
getBoundingClientRect)
A larger viewBox gives more coordinate space without affecting visual scale. viewBox: { width: 1000, height: 1000 } on a 500x500px SVG means 1 unit = 0.5 pixels.
Properties
| Property | Type | Description |
|---|
params | P | Reactive params. Mutate properties to trigger redraws. Top-level reassignment throws |
geometry | GeometryLayer | Geometry layer. Call .group() to create groups |
dimensions | DimensionsLayer | Dimensions layer. Call .group() to create groups |
Create groups once at setup, grab builders inside draw callbacks. Groups persist across frames.
Methods
Drawing
| Method | Signature | Description |
|---|
draw | (callback: (params: P) => void) => () => void | Register draw callback. Returns unsubscribe function |
Engine idles when all draw callbacks unsubscribe (unless camera/input is active).
Scene Controls
| Method | Signature | Default | Description |
|---|
enableFilter | (enabled: boolean) => void | false | Hand-drawn SVG filter. Can be slow on Safari with many paths during zoom |
setDisplacementScale | (scale: number) => void | 2.75 | Displacement scale for strokes and fills. Higher = more wobble |
setDisplacementFrequency | (frequency: number) => void | 0.1 | Displacement noise frequency for strokes and fills |
setDisplacementOctaves | (octaves: number) => void | 1 | Displacement noise octaves for strokes and fills |
enableMask | (enabled: boolean) => void | false | Enable or disable the mask for geometry groups and dimension strokes |
setMaskFrequency | (frequency: number) => void | 0.03 | Mask noise frequency. Higher = finer line-break pattern |
setMaskOctaves | (octaves: number) => void | 1 | Mask noise octaves |
setMaskScale | (scale: number) => void | 1.6 | Mask amplitude scale. Higher = denser line breaks (effective threshold lower) |
enableGrid | (enabled: boolean) => void | true | Show/hide graph-paper background |
enableAxes | (enabled: boolean) => void | true | Show/hide center axes |
enableFill | (enabled: boolean) => void | true | Toggle fills globally. When disabled, all fills hidden |
enablePan | (enabled: boolean) => void | false | Enable pan (middle-mouse or shift+click) |
enableZoom | (enabled: boolean) => void | false | Enable wheel zoom (1x-20x) |
Camera
| Method | Signature | Description |
|---|
resetCamera | () => void | Animate camera back to origin |
setViewScale | (scale: number) => void | Scale entire view (0.1x-10x) for responsive layouts. Doesn’t change coordinate system or camera state. Transitions are interpolated |
Fills
| Method | Signature | Description |
|---|
addHatchFill | (color: string, opacity?: number) => string | Create 45-degree hatch pattern. Returns url(#id) for use in path({ fill }) |
Lifecycle
| Method | Signature | Description |
|---|
dispose | () => void | Clean up listeners, DOM, defs |
Utilities
snapshotSvg
import { snapshotSvg } from "pluton-2d";
const svgText = snapshotSvg(svgElement);
| Param | Type | Description |
|---|
svg | SVGSVGElement | Source SVG element to snapshot |
options.width | number | Optional output width. Defaults to rendered width |
options.height | number | Optional output height. Defaults to rendered height |
options.background | string | Optional background fill color for injected base rect |
Returns a standalone SVG XML string with inlined computed styles and normalized local paint references (url(#id)).
Use the returned string to create a Blob and download/upload as needed.
GeometryGroup
Creating a group
const geometryGroup = scene.geometry.group();
Create groups once at setup, outside draw callbacks. Groups persist across frames. Grab builders with path() inside draw callbacks.
Methods
| Method | Signature | Description |
|---|
path | (options?: PathOptions) => PathBuilder | Returns a chainable PathBuilder for this frame |
translate | (x: number, y: number) => void | Offset group in world space. Direct DOM write, no redraw |
scale | (x: number, y: number) => void | Scale group. Direct DOM write, no redraw |
setDrawUsage | (mode: "static" | "dynamic") => void | "static" commits once, "dynamic" (default) every frame |
visible | (visible: boolean) => void | Show/hide the group |
clear | () => void | Remove all paths, reset state and transform |
In static mode, still call path() every frame. The engine detects unchanged geometry and skips DOM writes after the first commit.
PathOptions
| Option | Type | Default | Description |
|---|
className | string | — | CSS class for <path> |
fill | string | default hatch | CSS fill value (from addHatchFill() or manual). Use "none" for stroke-only |
stroke | string | — | CSS stroke value. Overrides default |
fillRule | "evenodd" | "nonzero" | "evenodd" | Use evenodd for cutouts, nonzero for simple fills |
PathBuilder
All methods return this for chaining unless noted otherwise. Relative commands move from the current position, absolute commands move to world coordinates.
Movement
| Method | Signature | Description |
|---|
moveTo | (dx: number, dy: number) => PathBuilder | Relative move |
moveToAbs | (x: number, y: number) => PathBuilder | Absolute move |
Lines
| Method | Signature | Description |
|---|
lineTo | (dx: number, dy: number) => PathBuilder | Relative line |
lineToAbs | (x: number, y: number) => PathBuilder | Absolute line |
Arcs
| Method | Signature | Description |
|---|
arcTo | (dx: number, dy: number, r: number, clockwise?: boolean, largeArc?: boolean) => PathBuilder | Relative arc |
arcToAbs | (x: number, y: number, r: number, clockwise?: boolean, largeArc?: boolean) => PathBuilder | Absolute arc |
| Param | Default | Description |
|---|
clockwise | false | Arc sweep direction in visual space (after Y-flip). true sweeps clockwise on screen |
largeArc | false | Whether the arc spans more than 180 degrees |
r <= 0 | — | Falls back to a straight line |
Cubic Bezier
| Method | Signature | Description |
|---|
cubicTo | (c1dx, c1dy, c2dx, c2dy, dx, dy) => PathBuilder | Relative cubic. Two control points (entry and exit handles) |
cubicToAbs | (c1x, c1y, c2x, c2y, x, y) => PathBuilder | Absolute cubic |
smoothCubicTo | (c2dx, c2dy, dx, dy) => PathBuilder | Relative smooth cubic. First handle mirrors previous exit handle |
smoothCubicToAbs | (c2x, c2y, x, y) => PathBuilder | Absolute smooth cubic |
Quadratic Bezier
| Method | Signature | Description |
|---|
quadTo | (c1dx, c1dy, dx, dy) => PathBuilder | Relative quadratic. One control point |
quadToAbs | (c1x, c1y, x, y) => PathBuilder | Absolute quadratic |
smoothQuadTo | (dx, dy) => PathBuilder | Relative smooth quadratic. Control point inferred from previous segment |
smoothQuadToAbs | (x, y) => PathBuilder | Absolute smooth quadratic |
Smooth commands (S/s, T/t) depend on the previous curve segment. If there’s no compatible previous segment (like after a line or move), SVG treats the inferred control point as the current point, giving a sharp corner instead.
Utility
| Method | Signature | Description |
|---|
close | () => PathBuilder | Close the path |
reset | () => void | Clear all commands. Not chainable |
toString | () => string | Returns the SVG path d attribute string. Not chainable |
DimensionsGroup
Creating a group
const dimensionsGroup = scene.dimensions.group();
Same pattern as geometry groups. Create once, outside draw callbacks.
Methods
| Method | Signature | Description |
|---|
dimension | (options?: DimensionOptions) => DimensionsBuilder | Returns a chainable DimensionsBuilder for this frame |
translate | (x: number, y: number) => void | Offset group in world space. Direct DOM write, no redraw |
setDrawUsage | (mode: "static" | "dynamic") => void | "static" commits once, "dynamic" (default) every frame |
visible | (visible: boolean) => void | Show/hide the group |
clear | () => void | Remove all dimensions, reset state and transform |
DimensionOptions
| Option | Type | Description |
|---|
className | string | CSS class for dimension elements |
DimensionsBuilder
All methods return this for chaining unless noted otherwise. Angles in radians. 0 points right, π/2 points up.
Positioning
| Method | Signature | Description |
|---|
moveTo | (dx: number, dy: number) => DimensionsBuilder | Relative move |
moveToAbs | (x: number, y: number) => DimensionsBuilder | Absolute move |
lineTo | (dx: number, dy: number) => DimensionsBuilder | Relative line |
lineToAbs | (x: number, y: number) => DimensionsBuilder | Absolute line |
Markers
| Method | Signature | Default size | Description |
|---|
arrow | (angleRad: number, size?: number) => DimensionsBuilder | 8 | Open V-arrow. Tip at current position, wings extend back |
arrowFilled | (angleRad: number, size?: number) => DimensionsBuilder | 8 | Filled triangle arrow on separate path layer |
tick | (angleRad: number, size?: number) => DimensionsBuilder | 15 | Architectural tick (crossed lines rotated by angleRad) |
centerMark | (size?: number) => DimensionsBuilder | 10 | Crosshair with filled center dot |
Arc
| Method | Signature | Description |
|---|
arc | (r: number, startAngle: number, endAngle: number) => DimensionsBuilder | Arc centered at current position. Direction inferred from angle order |
Text
| Method | Signature | Description |
|---|
textAt | (dx: number, dy: number, text: string, align?: string, className?: string) => DimensionsBuilder | Relative text placement |
textAtAbs | (x: number, y: number, text: string, align?: string, className?: string) => DimensionsBuilder | Absolute text placement |
align: "start" | "middle" (default) | "end". Controls text anchor.
Text is rendered in world space (Y-up). The engine transforms it to appear upright on screen.
Utility
| Method | Signature | Description |
|---|
close | () => DimensionsBuilder | Close the dimension path |
CSS Variables
Override these on .pluton-root to customize appearance.
| Variable | Default | Description |
|---|
--pluton-grid-minor-stroke | rgba(0, 0, 0, 0.025) | Minor grid line color |
--pluton-grid-major-stroke | rgba(0, 0, 0, 0.12) | Major grid line color |
--pluton-grid-stroke-width | 0.5 | Grid line width |
--pluton-axis-color | rgba(0, 0, 0, 0.2) | Center axis color |
--pluton-axis-stroke-width | 1 | Axis line width |
--pluton-axis-dash | 5 5 | Axis dash pattern |
--pluton-geometry-stroke | rgba(0, 0, 0, 0.7) | Default geometry stroke color |
--pluton-geometry-stroke-width | 1 | Geometry stroke width |
--pluton-hatch-color | rgba(0, 39, 50, 0.14) | Default hatch fill color |
--pluton-dim-color | rgba(0, 0, 0, 0.75) | Dimension line/marker color |
--pluton-dim-stroke-width | 1 | Dimension stroke width |
--pluton-dim-text-color | rgba(0, 0, 0, 0.75) | Dimension text color |
--pluton-dim-font-size | 12px | Dimension text size |
--pluton-dim-font-family | system-ui, sans-serif | Dimension text font |