Import it from @grafloria/renderer.
A Canvas2DLike that records what was drawn instead of rasterising it.
The recording is the assertion surface for the canvas backend's unit tests:
"the VNode tree said <rect fill=#fff stroke=#333 stroke-width=2> at
(100,100)" ⇒ "the context received a fill with fillStyle #fff and a stroke
with lineWidth 2 over exactly that path, under exactly that transform".
It faithfully models the parts of the 2D state machine the painter depends on
— the save/restore stack, the current transform, the current path — so a bug
in the painter's state handling (a missing restore, a transform applied
twice) shows up in the recording rather than hiding behind a mock.
tsclass RecordingContext2D implements Canvas2DLike
Properties
| Name | Type | Default | Description |
|---|---|---|---|
calls | DrawCall[] | [] | |
charWidthRatio | 0.6 | Width of one character, used by measureText. Matches the SVG-side estimate. |
Methods
get fillStyle(): unknownset fillStyle(v: unknown)get strokeStyle(): unknownset strokeStyle(v: unknown)get lineWidth(): numberset lineWidth(v: number)get lineJoin(): CanvasLineJoinset lineJoin(v: CanvasLineJoin)get lineCap(): CanvasLineCapset lineCap(v: CanvasLineCap)get globalAlpha(): numberset globalAlpha(v: number)get font(): stringset font(v: string)get textAlign(): CanvasTextAlignset textAlign(v: CanvasTextAlign)get textBaseline(): CanvasTextBaselineset textBaseline(v: CanvasTextBaseline)get filter(): stringset filter(v: string)save(): voidrestore(): voidsetTransform(a: number, b: number, c: number, d: number, e: number, f: number): voidbeginPath(): voidmoveTo(x: number, y: number): voidlineTo(x: number, y: number): voidbezierCurveTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number): voidquadraticCurveTo(x1: number, y1: number, x: number, y: number): voidclosePath(): voidfill(): voidstroke(): voidclip(): voidclearRect(x: number, y: number, w: number, h: number): voidfillRect(x: number, y: number, w: number, h: number): voidsetLineDash(segments: number[]): voidfillText(text: string, x: number, y: number): voidmeasureText(text: string): { width: number }— The same average-glyph estimate the SVG text-block engine uses (text.length * fontSize * 0.6), so headless measurement agrees with the line-breaking the VNode producer already did. A real 2D context measures glyphs properly; this is only the headless stand-in.createLinearGradient(x0: number, y0: number, x1: number, y1: number): unknowncreateRadialGradient( x0: number, y0: number, r0: number, x1: number, y1: number, r1: number ): unknownreset(): void— Drop the recording (keeps the current state — mirrors a fresh frame).paintCalls(): DrawCall[]— Every paint call (ignores bookkeeping ops) — the usual assertion target.
Was this page helpful?