Canvas
Declarative 2D native SwiftUI vector canvas supporting 9 drawing primitives, linear/radial/angular gradients, dashed strokes, pivot rotations, and spatial click callbacks (onClickAt) — built for analog dials, custom gauges, compasses, and visualizers.
Signature#
SIGNATURE
<Canvas
viewWidth={160}
viewHeight={160}
ops={[
{ op: "circle", cx: 80, cy: 80, r: 72,
stroke: "theme.divider",
lineWidth: 2 },
{ op: "line", x1: 80, y1: 80,
x2: 80, y2: 26,
stroke: "theme.accent",
lineWidth: 3, lineCap: "round",
rotateDeg: 45,
pivotX: 80, pivotY: 80 },
]}
/>
viewBox 160 × 160 · 2 ops
Canvas Props#
opsCanvasOp[]Required- Ordered list of vector operations rendered bottom-to-top. Memoize with
useMemowhen drawing many ops. viewWidthnumberOptional- Virtual coordinate width (like SVG
viewBox); scales cleanly to any rendered size. viewHeightnumberOptional- Virtual coordinate height paired with
viewWidth. onClickAtfunctionOptional- Inherited from
StyleProps: receives({ x, y, width, height })in local points for interactive dials or grids.
9 Supported CanvasOp Primitives#
Each entry in ops is a plain object choosing a shape via op:
rectRectangleop: "rect"- Fields:
x,y,w,h. roundedRectRounded rectop: "roundedRect"- Fields:
x,y,w,h,r(corner radius). circleCircleop: "circle"- Fields:
cx,cy(center),r(radius). ellipseEllipseop: "ellipse"- Fields:
cx,cy,rx(horizontal radius),ry(vertical radius). arcArcop: "arc"- Fields:
cx,cy,r,startDeg,endDeg, optionalanticlockwise(0°at 3 o'clock, increasing clockwise). lineLineop: "line"- Fields:
x1,y1,x2,y2; pairs cleanly withrotateDeg,pivotX,pivotY, andlineCap: "round"for clock hands. polylinePolylineop: "polyline"- Fields:
points([[x, y], ...]), optionalclosed: trueto connect back to the start. polygonPolygonop: "polygon"- Fields:
points([[x, y], ...]), closed filled or stroked polygon. textVector textop: "text"- Fields:
x,y,text, optionalfontSize,fontWeight,fontDesign,italic,align.
Shared Drawing & Transform Fields (on any op)#
fillstringOptional- Solid fill color token or hex string.
fillGradientGradientSpecOptional- Gradient fill
{ kind?: "linear" | "radial" | "angular", colors, stops?, x1?, y1?, x2?, y2?, cx?, cy?, r? }. strokestringOptional- Stroke color token or hex string.
strokeGradientGradientSpecOptional- Gradient stroke (same shape as
fillGradient). lineWidthnumberOptional- Stroke thickness in points.
lineCapstringOptional- Line cap style:
"butt","round", or"square". dashnumber[]Optional- Dash pattern array, e.g.
[4, 4]. dashPhasenumberOptional- Dash phase offset.
rotateDegnumberOptional- Clockwise rotation in degrees.
pivotXnumberOptional- Rotation pivot X coordinate (paired with
rotateDeg). pivotYnumberOptional- Rotation pivot Y coordinate (paired with
rotateDeg). opacitynumberOptional- Per-op opacity (
0–1).
Example: Vector Analog Watch Face#
TSX
const ops: CanvasOp[] = [
{ op: "circle", cx: 80, cy: 80, r: 70,
fill: "theme.faint",
stroke: "theme.divider" },
...hourTicks(12),
{ op: "line", x1: 80, y1: 80,
x2: 80, y2: 44, stroke: "theme.ink",
lineWidth: 4, rotateDeg: 305 },
{ op: "line", x1: 80, y1: 80,
x2: 80, y2: 28, stroke: "theme.ink",
lineWidth: 2.5, rotateDeg: 56 },
{ op: "line", x1: 80, y1: 92,
x2: 80, y2: 24, stroke: "theme.accent",
lineWidth: 1.5, rotateDeg: 180 },
{ op: "circle", cx: 80, cy: 80, r: 4,
fill: "theme.accent" },
];
<Canvas
viewWidth={160}
viewHeight={160}
ops={ops}
/>
Vector Analog Watch Face