Many points: the canvas renderer
An SVG chart makes a DOM element for every mark. Fine for thousands, slow for tens of thousands. Add "renderer": "canvas" to the spec and the same chart is drawn on one canvas.
The same 21,000 points, two renderers
Both charts read one dataset and have the same spec except renderer. Zoom, pan, tooltip, legend filter, colors and dark mode work the same. Time the redraws on your machine:
SVG (the default)
Canvas
Press the button. Both charts also zoom with Ctrl + wheel.
Numbers depend on the machine; on the one this page was made on, Chrome redraws these 21,000 points 5 to 6 times faster on the canvas (about 190 ms against 35). A single long line is one path element either way, so it gains little from a canvas: reduce it with the decimate transform instead.
HTML
<script src="/release/kits/full.js"></script>
<script type="application/json" id="bc-data">{"points": {"columns": {"x": {...}, "y": {...}}}} /* two binary columns, see "Ways to hand over data" */</script>
<div class="cols">
<div><p class="muted" style="margin:0 0 4px">SVG (the default)</p><div id="svg-chart"></div></div>
<div><p class="muted" style="margin:0 0 4px">Canvas</p><div id="canvas-chart"></div></div>
</div>
<div class="bar"><button id="bench">Time 20 zoom steps on each</button></div>
<pre class="out" id="result">Press the button. Both charts also zoom with Ctrl + wheel.</pre>
<script>
BC.mount(); // read the data above now, not at the end of the page load
var svg = BC.chart(document.getElementById('svg-chart'), {"data":"points","renderer":"svg","size":[520,360],"scales":{"x":{"type":"linear","nice":true},"y":{"type":"linear","nice":true}},"guides":[{"type":"grid","scale":"y"},{"type":"axis","scale":"x"},{"type":"axis","scale":"y"}],"marks":[{"type":"point","x":"x","y":"y","r":1.6,"opacity":0.35}],"interaction":[{"type":"zoom","scales":["x","y"]},{"type":"tooltip"}]});
var canvas = BC.chart(document.getElementById('canvas-chart'), {"data":"points","renderer":"canvas","size":[520,360],"scales":{"x":{"type":"linear","nice":true},"y":{"type":"linear","nice":true}},"guides":[{"type":"grid","scale":"y"},{"type":"axis","scale":"x"},{"type":"axis","scale":"y"}],"marks":[{"type":"point","x":"x","y":"y","r":1.6,"opacity":0.35}],"interaction":[{"type":"zoom","scales":["x","y"]},{"type":"tooltip"}]});
function frame() { return new Promise(function (done) { requestAnimationFrame(function () { requestAnimationFrame(done); }); }); }
async function time(chart) {
var t0 = performance.now();
for (var i = 0; i < 20; i++) {
var from = (i % 10) * 90, span = 4095 - (i % 10) * 300;
chart.setView('x', [from, from + span]);
chart.setView('y', [from, from + span]);
await frame();
}
var ms = (performance.now() - t0) / 20;
chart.setView('x', null); chart.setView('y', null);
await frame();
return ms;
}
document.getElementById('bench').onclick = async function () {
await svg.ready; await canvas.ready;
var a = await time(svg), b = await time(canvas);
document.getElementById('result').textContent =
'SVG: ' + a.toFixed(0) + ' ms per redraw\nCanvas: ' + b.toFixed(0) + ' ms per redraw (' + (a / b).toFixed(1) + 'x faster)';
};
</script> What is different on a canvas
A canvas has no elements, so a few things that come from elements do not exist: CSS classes on marks, the browser's native hover title, and text selection inside the chart. Colors still come from your CSS variables, and the chart repaints when the color scheme or the container width changes. Charts are sharp on high-density screens (the backing store follows devicePixelRatio, up to 3). The renderer is a block like any other: kits that include it are timeseries and full.