Renderers
A mark produces plain-data scene nodes: a circle at cx,cy, a rect, a path. The engine hands the whole scene to a renderer, which draws the nodes and reports gestures back. The renderer is the only link between the engine and the screen, so marks, edits, scales, guides and constraints behave the same under any renderer. Set it with renderer: on the spec.
The default is renderer: "svg", an SVG backend (D3Renderer). renderer: "canvas" (CanvasRenderer) implements the same contract (render(context) in, onEvent out) and draws to a <canvas>. A JSON spec takes the same two names. Any object that meets the contract works as well, given as an instance. The examples below run on canvas, and every pointer edit behaves as it does under SVG, because the interaction model lives in the engine. What each renderer supports lists where the two differ.
Direct-pick drag, on canvas
Under SVG, the browser picks the element under the pointer. A canvas is a single element, so the canvas renderer finds the touched mark geometrically, with the same distance maths as the nearest pick. It then dispatches the same move edit. Drag a bar and the gesture inverts through the y scale and rewrites the value, as it does under SVG.
Drag a value (canvas)
renderer: 'canvas'. y carries edit: move(); the direct-pick target is hit-tested geometrically against the scene nodes.
const chart = Elicit({width: 380, height: 260,renderer: "canvas",margins: { top: 14, right: 14, bottom: 26, left: 30 },data: [{ x: "A", y: 20 }, { x: "B", y: 45 },{ x: "C", y: 30 }, { x: "D", y: 60 },],schema: {x: { type: "categorical", domain: ["A", "B", "C", "D"] },y: { type: "quantitative", domain: [0, 100] },},marks: [bar({fill: "#50AEB7",channels: {x: { field: "x" },y: { field: "y", edit: move() },},}),],});mount(chart.dom());
Plane gestures, on canvas
Plane-driven edits (sweep, draw, nearest, create) resolve their target from the pointer coordinates. The canvas renderer feeds those coordinates through the same pointer state machine, so a you-draw-it sweep works unchanged.
Sweep a line (canvas)
A plane-pick sweep: drag across the plot to redraw the line. It works from pointer coordinates through the scale, with no node identity needed.
const chart = Elicit({width: 420, height: 260,renderer: "canvas",margins: { top: 16, right: 16, bottom: 40, left: 44 },data: [{ month: 0, demand: 50 }, { month: 1, demand: 50 },],schema: {month: { type: "quantitative", domain: [0, 5] },demand: { type: "quantitative", domain: [0, 100] },},axes: { x: { title: "Month" }, y: { title: "Demand" } },marks: [lineY({stroke: "#315228", strokeWidth: 3, curve: "catmullRom",channels: {x: { field: "month" },y: { field: "demand", edit: move({ pick: "sweep", guide: true }) },},}),],});mount(chart.dom());
What each renderer supports
Both renderers draw every mark, element, guide and effect, and report every pointer gesture. A renderer declares what it does beyond that in supports. A spec that asks for something its renderer does not declare logs a warning.
supports | Covers | D3Renderer | CanvasRenderer |
|---|---|---|---|
keyboard | Tab focus on editable marks, arrow-key nudges, focusOutline | yes | no |
textEditor | Typing into a mark with edit.editText() | yes | no |
filter | The filter prop of an effect | yes | in browsers whose 2D context has filter |
overflow | overflow: "visible" | yes | no |
shaders | Shader looks | no | in browsers with WebGL |
Canvas differs in three more ways, none of which a spec can ask for:
- It finds the mark under the pointer geometrically, with a 4px tolerance around every shape. SVG tests the drawn shape exactly.
- It picks a path along its
points, so a custom mark that emits onlyddraws and takes no press. - It draws pixels, so a mark has no element to style with CSS, select in a test, or expose to a screen reader.
A custom renderer sets supports to what it implements; unset declares nothing. It can also define destroy(), which chart.destroy() calls so the renderer can release its listeners and caches.