Published
Edited
Mar 14, 2019
7 forks
38 stars
Insert cell
Insert cell
Insert cell
canvas = {
const context = DOM.context2d(width, height);
let polygons = Array.from(new d3.Delaunay(points).voronoi([0, 0, width, height]).cellPolygons());
context.globalAlpha = 0.15;
context.lineWidth = 0.45;
while (polygons.length) {
context.beginPath();
polygons = polygons.filter(P => {
context.moveTo(...P[0]);
for (let j = 1, m = P.length; j < m; ++j) context.lineTo(...P[j]);
context.closePath();
const p0 = P.shift();
const p1 = P[0];
const dx = p1[0] - p0[0];
const dy = p1[1] - p0[1];
const l = Math.hypot(dx, dy);
const t = Math.min(0.5, 4 / l);
const p2 = [p0[0] + dx * t - dy / (l * 2), p0[1] + dy * t + dx / (l * 2)];
if (Math.hypot(p2[0] - p1[0], p2[1] - p1[1]) > 1) P.push(p2);
return -d3.polygonArea(P) > 1;
});
context.stroke();
yield context.canvas;
}
}
Insert cell
points = replay, Array.from({length: n << 1}, (_, i) => Math.random() * (i & 1 ? height : width))
Insert cell
n = 100
Insert cell
height = Math.max(600, width)
Insert cell
d3 = require("d3-delaunay@4", "d3-polygon@1")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more