canvasContext = {
const context = DOM.context2d(width, height);
const canvas = context.canvas;
function render() {
context.clearRect(0, 0, width, height);
const voronoi = d3.Delaunay.from(
circles,
(d) => d.x,
(d) => d.y
).voronoi([0, 0, width, height]);
context.strokeStyle = "#ccc";
context.lineWidth = 1;
context.beginPath();
voronoi.render(context);
context.stroke();
for (const { x, y, color, active } of circles) {
context.beginPath();
context.moveTo(x + radius, y);
context.arc(x, y, radius, 0, Math.PI * 2);
context.fillStyle = color;
context.fill();
if (active) {
context.strokeStyle = "black";
context.lineWidth = 1;
context.stroke();
}
}
}
d3.select(canvas).call(
drag().on("start.render drag.render end.render", render)
);
render();
return canvas;
}