Documentation / @ripl/canvas
@ripl/canvas ​
The Canvas 2D rendering context for Ripl: rasterizes the scene graph to an HTML
<canvas>throughCanvasRenderingContext2D.
Features ​
- Ripl's default backend — implements the
Contextabstraction on the native Canvas 2D API, so the same drawing code also runs on SVG, the terminal or a custom context. - Device pixel ratio handled for you — the backing store scales with the display while every coordinate you author, every bounding box and every pointer payload stays in CSS pixels.
- Native paint objects — CSS gradient and pattern strings in
fillandstrokeare parsed and cached intoCanvasGradientandCanvasPatterninstances, keyed so repeated paints reuse them. - Browser hit testing —
isPointInPath/isPointInStrokerun against the browser's own implementation, under both fill rules. Path2Dcaching —supportsPathCachingistruehere, so a shape whose state has not changed replays its built path instead of re-tracing it each frame.- Text along a path and full stroke/fill state: caps, joins, dashes, miter limit, shadows, filters and blend modes.
- Snapshot export — PNG data URL,
Blobobject URL, or rawImageData.
Installation ​
Most browser projects should install @ripl/web instead. It re-exports this package alongside @ripl/core and registers the browser platform bindings, which this context needs for text measurement and frame scheduling.
bash
# npm
npm install @ripl/canvas
# yarn
yarn add @ripl/canvas
# pnpm
pnpm add @ripl/canvasQuick start ​
typescript
import {
createContext,
} from '@ripl/canvas';
import {
createCircle,
} from '@ripl/core';
const context = createContext('.mount-element');
createCircle({
fill: 'linear-gradient(135deg, #3a86ff, #8338ec)',
cx: context.width / 2,
cy: context.height / 2,
radius: 50,
}).render(context);Every context can snapshot its current output through export():
typescript
const snapshot = context.export();
const dataUrl = snapshot.toString(); // PNG data URL
const url = snapshot.toURL(); // PNG object URL
const image = await snapshot.toImage(); // ImageData
snapshot.release(); // revokes the object URLKey API ​
| Export | What it does |
|---|---|
createContext | Binds a CanvasContext to a selector, element or existing canvas |
CanvasContext | The context itself, for typing and subclassing |
CanvasPath | Path2D-backed path builder used by every element |
toCanvasGradient / toCanvasPattern | Paint-string conversion into native canvas paints |
rescaleCanvas | Resizes the backing store for a device pixel ratio |
Related packages ​
@ripl/web— the browser entry point, and what most projects should install@ripl/core— the elements and scene graph this context draws@ripl/svg— the same API, rendering to SVG instead@ripl/3d— a 3D context built on this one
Documentation ​
Guides, live demos and the full API reference are at ripl.run/docs/core/contexts/canvas.
License ​
Classes ​
| Class | Description |
|---|---|
| CanvasContext | Canvas 2D rendering context implementation, mapping the unified API to CanvasRenderingContext2D. |
| CanvasPath | Canvas-specific path implementation backed by a native Path2D object. |
Interfaces ​
| Interface | Description |
|---|---|
| Canvas2DState | The shared CanvasRenderingContext2D state-plumbing surface applied by canvas2DStateMixin: paint, line, shadow, and text accessors mapped directly onto the native 2D context, plus the drawing, transform, measurement, and hit-testing operations common to every canvas-backed context. |
| RescaleResult | Result of a canvas rescale operation containing the updated coordinate scales. |
Type Aliases ​
| Type Alias | Description |
|---|---|
| AbstractConstructor | Constructor type for (possibly abstract) classes, used to compose mixins over a Context base. |
Functions ​
| Function | Description |
|---|---|
| applyCanvasFill | Fills a canvas path or text element, dispatching text-along-path when applicable. |
| applyCanvasStroke | Strokes a canvas path or text element, dispatching text-along-path when applicable. |
| canvas2DStateMixin | Mixin applying the shared CanvasRenderingContext2D state plumbing (see Canvas2DState) to a Context base class. Concrete subclasses assign the protected context backing field (declared here, so constructor assignment is not clobbered by class-field initialization) and may override the protected gradientBounds() hook to change which bounding box gradients resolve against; the default is the current render element's local (untransformed) box. |
| canvasDrawImage | Draws an image onto a canvas context, sizing it to the given width and height. |
| canvasIsPointInPath | Tests whether a point is inside the filled region of a canvas path. |
| canvasIsPointInStroke | Tests whether a point is on the stroked outline of a canvas path. |
| canvasMeasureText | Measures text dimensions using the context's alignment and baseline, and an optional font override. |
| createContext | Creates a Canvas 2D rendering context (a concrete Context) attached to the given DOM target. |
| releaseCanvasPaintCache | Drops every CanvasGradient and CanvasPattern cached against a context, together with the offscreen tile canvases the patterns hold. Call it when the context is torn down. |
| renderTextAlongPath | Renders text character-by-character along a path using fill or stroke. |
| rescaleCanvas | Sizes a canvas element's backing store for the device pixel ratio and returns the coordinate scales mapping logical pixels onto it. |
| setCanvasFill | Sets the fill style on a native canvas context, resolving gradient and pattern strings when applicable. |
| setCanvasStroke | Sets the stroke style on a native canvas context, resolving gradient and pattern strings when applicable. |
| toCanvasGradient | Converts a parsed gradient definition into a native CanvasGradient within the given bounds. |
| toCanvasPattern | Materializes a pattern(...) paint string as a repeating CanvasPattern, drawing the shared tile geometry into an offscreen canvas. Results (including parse failures) are cached per context and string, and released by releaseCanvasPaintCache. |