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 },
});
ParamTypeDescription
svgSVGSVGElementSVG element to render into. Needs CSS width/height or viewBox
options.paramsobjectOptional 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:

  1. Constructor viewBox parameter
  2. SVG viewBox attribute
  3. 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

PropertyTypeDescription
paramsPReactive params. Mutate properties to trigger redraws. Top-level reassignment throws
geometryGeometryLayerGeometry layer. Call .group() to create groups
dimensionsDimensionsLayerDimensions layer. Call .group() to create groups

Create groups once at setup, grab builders inside draw callbacks. Groups persist across frames.

Methods

Drawing

MethodSignatureDescription
draw(callback: (params: P) => void) => () => voidRegister draw callback. Returns unsubscribe function

Engine idles when all draw callbacks unsubscribe (unless camera/input is active).

Scene Controls

MethodSignatureDefaultDescription
enableFilter(enabled: boolean) => voidfalseHand-drawn SVG filter. Can be slow on Safari with many paths during zoom
setDisplacementScale(scale: number) => void2.75Displacement scale for strokes and fills. Higher = more wobble
setDisplacementFrequency(frequency: number) => void0.1Displacement noise frequency for strokes and fills
setDisplacementOctaves(octaves: number) => void1Displacement noise octaves for strokes and fills
enableMask(enabled: boolean) => voidfalseEnable or disable the mask for geometry groups and dimension strokes
setMaskFrequency(frequency: number) => void0.03Mask noise frequency. Higher = finer line-break pattern
setMaskOctaves(octaves: number) => void1Mask noise octaves
setMaskScale(scale: number) => void1.6Mask amplitude scale. Higher = denser line breaks (effective threshold lower)
enableGrid(enabled: boolean) => voidtrueShow/hide graph-paper background
enableAxes(enabled: boolean) => voidtrueShow/hide center axes
enableFill(enabled: boolean) => voidtrueToggle fills globally. When disabled, all fills hidden
enablePan(enabled: boolean) => voidfalseEnable pan (middle-mouse or shift+click)
enableZoom(enabled: boolean) => voidfalseEnable wheel zoom (1x-20x)

Camera

MethodSignatureDescription
resetCamera() => voidAnimate camera back to origin
setViewScale(scale: number) => voidScale entire view (0.1x-10x) for responsive layouts. Doesn’t change coordinate system or camera state. Transitions are interpolated

Fills

MethodSignatureDescription
addHatchFill(color: string, opacity?: number) => stringCreate 45-degree hatch pattern. Returns url(#id) for use in path({ fill })

Lifecycle

MethodSignatureDescription
dispose() => voidClean up listeners, DOM, defs

Utilities

snapshotSvg

import { snapshotSvg } from "pluton-2d";

const svgText = snapshotSvg(svgElement);
ParamTypeDescription
svgSVGSVGElementSource SVG element to snapshot
options.widthnumberOptional output width. Defaults to rendered width
options.heightnumberOptional output height. Defaults to rendered height
options.backgroundstringOptional 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

MethodSignatureDescription
path(options?: PathOptions) => PathBuilderReturns a chainable PathBuilder for this frame
translate(x: number, y: number) => voidOffset group in world space. Direct DOM write, no redraw
scale(x: number, y: number) => voidScale group. Direct DOM write, no redraw
setDrawUsage(mode: "static" | "dynamic") => void"static" commits once, "dynamic" (default) every frame
visible(visible: boolean) => voidShow/hide the group
clear() => voidRemove 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

OptionTypeDefaultDescription
classNamestringCSS class for <path>
fillstringdefault hatchCSS fill value (from addHatchFill() or manual). Use "none" for stroke-only
strokestringCSS 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

MethodSignatureDescription
moveTo(dx: number, dy: number) => PathBuilderRelative move
moveToAbs(x: number, y: number) => PathBuilderAbsolute move

Lines

MethodSignatureDescription
lineTo(dx: number, dy: number) => PathBuilderRelative line
lineToAbs(x: number, y: number) => PathBuilderAbsolute line

Arcs

MethodSignatureDescription
arcTo(dx: number, dy: number, r: number, clockwise?: boolean, largeArc?: boolean) => PathBuilderRelative arc
arcToAbs(x: number, y: number, r: number, clockwise?: boolean, largeArc?: boolean) => PathBuilderAbsolute arc
ParamDefaultDescription
clockwisefalseArc sweep direction in visual space (after Y-flip). true sweeps clockwise on screen
largeArcfalseWhether the arc spans more than 180 degrees
r <= 0Falls back to a straight line

Cubic Bezier

MethodSignatureDescription
cubicTo(c1dx, c1dy, c2dx, c2dy, dx, dy) => PathBuilderRelative cubic. Two control points (entry and exit handles)
cubicToAbs(c1x, c1y, c2x, c2y, x, y) => PathBuilderAbsolute cubic
smoothCubicTo(c2dx, c2dy, dx, dy) => PathBuilderRelative smooth cubic. First handle mirrors previous exit handle
smoothCubicToAbs(c2x, c2y, x, y) => PathBuilderAbsolute smooth cubic

Quadratic Bezier

MethodSignatureDescription
quadTo(c1dx, c1dy, dx, dy) => PathBuilderRelative quadratic. One control point
quadToAbs(c1x, c1y, x, y) => PathBuilderAbsolute quadratic
smoothQuadTo(dx, dy) => PathBuilderRelative smooth quadratic. Control point inferred from previous segment
smoothQuadToAbs(x, y) => PathBuilderAbsolute 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

MethodSignatureDescription
close() => PathBuilderClose the path
reset() => voidClear all commands. Not chainable
toString() => stringReturns 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

MethodSignatureDescription
dimension(options?: DimensionOptions) => DimensionsBuilderReturns a chainable DimensionsBuilder for this frame
translate(x: number, y: number) => voidOffset group in world space. Direct DOM write, no redraw
setDrawUsage(mode: "static" | "dynamic") => void"static" commits once, "dynamic" (default) every frame
visible(visible: boolean) => voidShow/hide the group
clear() => voidRemove all dimensions, reset state and transform

DimensionOptions

OptionTypeDescription
classNamestringCSS class for dimension elements

DimensionsBuilder

All methods return this for chaining unless noted otherwise. Angles in radians. 0 points right, π/2 points up.

Positioning

MethodSignatureDescription
moveTo(dx: number, dy: number) => DimensionsBuilderRelative move
moveToAbs(x: number, y: number) => DimensionsBuilderAbsolute move
lineTo(dx: number, dy: number) => DimensionsBuilderRelative line
lineToAbs(x: number, y: number) => DimensionsBuilderAbsolute line

Markers

MethodSignatureDefault sizeDescription
arrow(angleRad: number, size?: number) => DimensionsBuilder8Open V-arrow. Tip at current position, wings extend back
arrowFilled(angleRad: number, size?: number) => DimensionsBuilder8Filled triangle arrow on separate path layer
tick(angleRad: number, size?: number) => DimensionsBuilder15Architectural tick (crossed lines rotated by angleRad)
centerMark(size?: number) => DimensionsBuilder10Crosshair with filled center dot

Arc

MethodSignatureDescription
arc(r: number, startAngle: number, endAngle: number) => DimensionsBuilderArc centered at current position. Direction inferred from angle order

Text

MethodSignatureDescription
textAt(dx: number, dy: number, text: string, align?: string, className?: string) => DimensionsBuilderRelative text placement
textAtAbs(x: number, y: number, text: string, align?: string, className?: string) => DimensionsBuilderAbsolute 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

MethodSignatureDescription
close() => DimensionsBuilderClose the dimension path

CSS Variables

Override these on .pluton-root to customize appearance.

VariableDefaultDescription
--pluton-grid-minor-strokergba(0, 0, 0, 0.025)Minor grid line color
--pluton-grid-major-strokergba(0, 0, 0, 0.12)Major grid line color
--pluton-grid-stroke-width0.5Grid line width
--pluton-axis-colorrgba(0, 0, 0, 0.2)Center axis color
--pluton-axis-stroke-width1Axis line width
--pluton-axis-dash5 5Axis dash pattern
--pluton-geometry-strokergba(0, 0, 0, 0.7)Default geometry stroke color
--pluton-geometry-stroke-width1Geometry stroke width
--pluton-hatch-colorrgba(0, 39, 50, 0.14)Default hatch fill color
--pluton-dim-colorrgba(0, 0, 0, 0.75)Dimension line/marker color
--pluton-dim-stroke-width1Dimension stroke width
--pluton-dim-text-colorrgba(0, 0, 0, 0.75)Dimension text color
--pluton-dim-font-size12pxDimension text size
--pluton-dim-font-familysystem-ui, sans-serifDimension text font