Published
Edited
Jul 15, 2020
18 forks
47 stars
Insert cell
Insert cell
canvas = {
const context = DOM.context2d(width, height);
const particles = Array.from({length: n}, () => [Math.random() * width, Math.random() * height]);

function update() {
const delaunay = d3.Delaunay.from(particles);
const voronoi = delaunay.voronoi([0.5, 0.5, width - 0.5, height - 0.5]);
context.clearRect(0, 0, width, height);

context.beginPath();
delaunay.render(context);
context.strokeStyle = "#ccc";
context.stroke();

context.beginPath();
voronoi.render(context);
voronoi.renderBounds(context);
context.strokeStyle = "#000";
context.stroke();

context.beginPath();
delaunay.renderPoints(context);
context.fill();
}

context.canvas.ontouchmove =
context.canvas.onmousemove = event => {
event.preventDefault();
particles[0] = [event.layerX, event.layerY];
update();
};

update();

return context.canvas;
}
Insert cell
height = 600
Insert cell
n = 200
Insert cell
d3 = require("d3-delaunay@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more