SVG
The SVG renderer draws shapes as SVG elements. It draws every shape, clips, and supports transitions, and its output can be styled, inspected and saved. It's the renderer for charts; for many shapes per frame, use Canvas.
const penguins = (await d3.csv("/data/penguins.csv", d3.autoType)).filter((d) => d.culmen_length_mm > 0);
const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);
root
.data(penguins)
.transform(cm.encode, {x: "culmen_length_mm", y: "culmen_depth_mm", fill: "species"})
.transform(cm.plane, {fill: {legend: true}})
.add(cm.circle, {r: 3});
root.render();
document.body.append(renderer.node());cm.svg([options])
Returns a renderer that draws into an SVG element. Options:
- width — the width in pixels (default
640). - height — the height in pixels (default
480).
A width or height that isn't given is auto: a transform such as cm.plane may resize it, such as to fit the rows of a chart.
const renderer = cm.svg({width: 640, height: 400});renderer.node()
Returns the SVG element, to add to the page.
document.body.append(renderer.node());Drawing
A flow built anew, such as for each frame of an animation, draws over what the last one drew, as on Canvas: start the frame with cm.clear, which on SVG also removes the elements drawn before. When a flow is drawn again, such as for a signal or each frame of a transition, the elements each shape drew are kept and only their changed attributes are written, so that they keep their identity, such as for CSS or event listeners. As on Canvas, a fillOpacity or strokeOpacity replaces the alpha of its color, a text turns around its position in radians, and a path or a stroked text is only filled if given a fill.