Skip to main content
On this pageFunctions

Canvas

Functions

view

functionsource
/**
 * A virtual DOM `<canvas>` element backed by a declarative scene description.
 * The insert hook captures the 2D context and paints the initial scene; the
 * postpatch hook re-paints on every render. The canvas is a pure function of
 * `shapes`. Same shapes produce the same pixels.
 */
<Message>(config: ViewConfig<Message>): Html

Types

Shape

typesource
/**
 * A drawable scene-graph node. Composing `Group` recursively builds a tree;
 * the painter walks the tree depth-first, applying transforms via
 * `ctx.save` / `ctx.restore`.
 */
type Shape = typeof Rect.Type | typeof Circle.Type | typeof Path.Type | typeof Text.Type | Group

ViewConfig

typesource
/**
 * Configuration for `Canvas.view`. Pointer handlers are optional and
 * receive a `Point` already translated to the canvas's internal coordinate
 * space (the `width` and `height` passed here), independent of how the
 * canvas is sized in CSS.
 */
type ViewConfig = Readonly<{
  className: string
  height: number
  onPointerDown: (point: Point) => Message
  onPointerMove: (point: Point) => Message
  onPointerUp: (point: Point) => Message
  shapes: ReadonlyArray<Shape>
  width: number
}>

Interfaces

Group

interfacesource
/**
 * A scene-graph node that applies a 2D transform and global alpha to a list of
 * child shapes. Transforms compose multiplicatively when groups are nested.
 * 
 * Defined via the `interface` form so the recursion (`shapes: ReadonlyArray<Shape>`
 * where `Shape` includes `Group` itself) resolves under TypeScript's lazy
 * interface evaluation. The matching runtime Schema uses `S.suspend`.
 */
interface Group {
  _tag: "Group"
  opacity: number
  rotate: number
  scale: ReadonlySide<{
    x: Number
    y: Number
  }, "Type">
  shapes: readonly Array<Shape>
  translate: ReadonlySide<{
    x: Number
    y: Number
  }, "Type">
}

Constants

BezierTo

constsource
/** Draw a cubic Bezier curve from the cursor through two control points to an end point. */
const BezierTo: CallableTaggedStruct<"BezierTo", {
  cp1x: Number
  cp1y: Number
  cp2x: Number
  cp2y: Number
  x: Number
  y: Number
}>

Circle

constsource
/** A filled or stroked circle. */
const Circle: CallableTaggedStruct<"Circle", {
  fill: optional<String>
  lineWidth: optional<Number>
  radius: Number
  stroke: optional<String>
  x: Number
  y: Number
}>

Close

constsource
/** Close the current path by drawing a line back to its starting point. */
const Close: CallableTaggedStruct<"Close", {}>

Group

constsource
/** Construct a `Group` shape that wraps its children in a transformed scope. */
const Group: CallableTaggedStruct<"Group", {
  opacity: optional<Number>
  rotate: optional<Number>
  scale: optional<Struct<{
    x: Number
    y: Number
  }>>
  shapes: $Array<Schema<Shape>>
  translate: optional<Struct<{
    x: Number
    y: Number
  }>>
}>

LineCap

constsource
/** Stroke cap style: how the ends of an open stroked subpath are rendered. */
const LineCap: Literals<readonly ["Butt", "Round", "Square"]>

LineJoin

constsource
/** Stroke join style: how two connected stroked segments meet. */
const LineJoin: Literals<readonly ["Miter", "Round", "Bevel"]>

LineTo

constsource
/** Draw a straight line from the cursor to a point. */
const LineTo: CallableTaggedStruct<"LineTo", {
  x: Number
  y: Number
}>

MoveTo

constsource
/** Move the path cursor to a point without drawing. */
const MoveTo: CallableTaggedStruct<"MoveTo", {
  x: Number
  y: Number
}>

Path

constsource
/** A path built from a sequence of `PathInstruction`s. */
const Path: CallableTaggedStruct<"Path", {
  fill: optional<String>
  instructions: $Array<Union<readonly [
    CallableTaggedStruct<"MoveTo", {
      x: Number
      y: Number
    }>,
    CallableTaggedStruct<"LineTo", {
      x: Number
      y: Number
    }>,
    CallableTaggedStruct<"QuadTo", {
      cpx: Number
      cpy: Number
      x: Number
      y: Number
    }>,
    CallableTaggedStruct<"BezierTo", {
      cp1x: Number
      cp1y: Number
      cp2x: Number
      cp2y: Number
      x: Number
      y: Number
    }>,
    CallableTaggedStruct<"Close", {}>
  ]>>
  lineCap: optional<Literals<readonly ["Butt", "Round", "Square"]>>
  lineJoin: optional<Literals<readonly ["Miter", "Round", "Bevel"]>>
  lineWidth: optional<Number>
  stroke: optional<String>
}>

PathInstruction

constsource
/** A single drawing instruction within a `Path` shape. */
const PathInstruction: Union<readonly [
  CallableTaggedStruct<"MoveTo", {
    x: Number
    y: Number
  }>,
  CallableTaggedStruct<"LineTo", {
    x: Number
    y: Number
  }>,
  CallableTaggedStruct<"QuadTo", {
    cpx: Number
    cpy: Number
    x: Number
    y: Number
  }>,
  CallableTaggedStruct<"BezierTo", {
    cp1x: Number
    cp1y: Number
    cp2x: Number
    cp2y: Number
    x: Number
    y: Number
  }>,
  CallableTaggedStruct<"Close", {}>
]>

Point

constsource
/** A 2D point in canvas-local coordinates. */
const Point: Struct<{
  x: Number
  y: Number
}>

QuadTo

constsource
/** Draw a quadratic Bezier curve from the cursor through a control point to an end point. */
const QuadTo: CallableTaggedStruct<"QuadTo", {
  cpx: Number
  cpy: Number
  x: Number
  y: Number
}>

Rect

constsource
/** An axis-aligned rectangle. */
const Rect: CallableTaggedStruct<"Rect", {
  fill: optional<String>
  height: Number
  lineWidth: optional<Number>
  stroke: optional<String>
  width: Number
  x: Number
  y: Number
}>

Text

constsource
/** A single line of text drawn with a font, fill, and optional stroke. */
const Text: CallableTaggedStruct<"Text", {
  align: optional<Literals<readonly ["Left", "Center", "Right", "Start", "End"]>>
  baseline: optional<Literals<readonly ["Top", "Middle", "Bottom", "Alphabetic", "Hanging", "Ideographic"]>>
  content: String
  fill: optional<String>
  font: optional<String>
  lineWidth: optional<Number>
  stroke: optional<String>
  x: Number
  y: Number
}>

TextAlign

constsource
/** Horizontal alignment of a `Text` shape relative to its anchor x coordinate. */
const TextAlign: Literals<readonly ["Left", "Center", "Right", "Start", "End"]>

TextBaseline

constsource
/** Vertical alignment of a `Text` shape relative to its anchor y coordinate. */
const TextBaseline: Literals<readonly ["Top", "Middle", "Bottom", "Alphabetic", "Hanging", "Ideographic"]>

Stay in the update loop.

New releases, patterns, and the occasional deep dive.


Built with Foldkit.

© 2026 Devin Jameson