canvasContext = {
const context = DOM.context2d(width, height);
const canvas = context.canvas;
let circles = d3.range(number_of_circles).map((i) => ({
x: Math.random() * (width - radius * 2) + radius,
y: Math.random() * (height - radius * 2) + radius,
weight: Math.random() * radius * 2 + 10,
color: colors[i % 10]
}));
let seed = seededRandom(randomSeeds);
let simulation = d3
.forceSimulation(circles)
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", render);
context.clearRect(0, 0, width, height);
function render() {
context.clearRect(0, 0, width, height);
let sim = d3
.voronoiMapSimulation(circles)
.weight((d) => d.weight)
.prng(seed)
.clip(ellipse)
.convergenceRatio(0.05)
.maxIterationCount(7)
.initialPosition(d3.voronoiMapInitialPositionPie())
.stop();
let state = sim.state();
let polygons = state.polygons;
while (!state.ended) {
sim.tick();
state = sim.state();
}
polygons = state.polygons;
polygons.forEach((points, i) => {
context.beginPath();
points.forEach((point) => {
context.lineTo(point[0], point[1]);
});
context.fillStyle = colors[i];
context.fill();
});
polygons.forEach((points) => {
context.beginPath();
points.forEach((point) => {
context.lineTo(point[0], point[1]);
});
context.strokeStyle = "rgba(255,255,255,0.2)";
context.lineWidth = 1;
context.stroke();
});
}
const dragBehavior = drag(simulation, circles);
render();
return canvas;
}