Skip to content

Context

A Context is the rendering target for all Ripl elements. It abstracts away the differences between rendering backends (Canvas, SVG, etc.) behind a unified API. You create a context, draw elements to it, and Ripl handles the rest.

Creating a Context

Use createContext to create a context attached to a DOM element. By default, Ripl creates a Canvas context:

ts
import {
    createContext,
} from '@ripl/core';

// Pass a CSS selector or an HTMLElement
const context = createContext('.my-container');

To create an SVG context instead, import from @ripl/svg:

ts
import {
    createContext,
} from '@ripl/svg';

const context = createContext('.my-container');

The context automatically fills its parent container and responds to resize events.

Properties

PropertyTypeDescription
typestringThe context type ('canvas' or 'svg')
widthnumberCurrent width of the rendering area (in CSS pixels)
heightnumberCurrent height of the rendering area (in CSS pixels)
elementElementThe underlying DOM element (e.g. <canvas> or <svg>)
rootHTMLElementThe parent container element
bufferbooleanWhether rendering is buffered (batched)

Options

ts
interface ContextOptions {
    buffer?: boolean; // Default: false for canvas, true for SVG
}

The buffer option controls whether rendering operations are batched. The SVG context uses buffering by default to minimize DOM mutations.

Drawing State

The context maintains a drawing state stack, similar to the Canvas 2D API. You can save and restore state to isolate style changes:

ts
context.save();
context.fillStyle = '#ff0000';
// ... draw red elements ...
context.restore(); // fillStyle reverts to previous value

The full set of state properties mirrors the Canvas 2D API:

PropertyTypeDefault
fillStylestring'#000000'
strokeStylestring'#000000'
lineWidthnumber1
lineCap'butt' | 'round' | 'square''butt'
lineJoin'bevel' | 'miter' | 'round''miter'
lineDashnumber[][]
lineDashOffsetnumber0
globalAlphanumber1
fontstring'10px sans-serif'
textAlignstring'start'
textBaselinestring'alphabetic'
shadowBlurnumber0
shadowColorstring'rgba(0, 0, 0, 0)'
shadowOffsetXnumber0
shadowOffsetYnumber0
filterstring'none'
direction'inherit' | 'ltr' | 'rtl''inherit'

Key Methods

save() / restore()

Push and pop the drawing state stack. Always pair these calls to avoid state leaks.

clear()

Clear the entire rendering area.

markRenderStart() / markRenderEnd()

Bracket a render pass. These are used internally by elements and scenes to track render depth. The SVG context uses these markers to know when to flush its virtual DOM.

TIP

You typically don't need to call markRenderStart/markRenderEnd yourself — elements and scenes handle this automatically.

batch(callback)

A convenience method that wraps a callback in save()/restore():

ts
context.batch(() => {
    context.fillStyle = '#ff0000';
    circle.render(context);
});
// fillStyle is automatically restored here

destroy()

Remove the context's DOM element and clean up all event listeners.

Events

The context emits a resize event whenever its container changes size:

ts
context.on('resize', () => {
    console.log(`New size: ${context.width} x ${context.height}`);
});

Demo