Published
Edited
Jul 9, 2019
2 forks
22 stars
Insert cell
Insert cell
canvas = {
const context = DOM.context2d(width, height);
while (true) {
context.save();
context.beginPath();
voronoi.render(context);
context.lineWidth = 2;
context.strokeStyle = "white";
context.stroke();
context.lineWidth = 1;
context.strokeStyle = "red";
context.stroke();
context.restore();
yield context.canvas;
for (let i = 0; i < n; ++i) {
const x = i << 1;
const y = x + 1;
positions[x] += velocities[x];
positions[y] += velocities[y];
if (positions[x] < -margin) positions[x] += width + margin * 2;
else if (positions[x] > width + margin) positions[x] -= width + margin * 2;
if (positions[y] < -margin) positions[y] += height + margin * 2;
else if (positions[y] > height + margin) positions[y] -= height + margin * 2;
velocities[x] += 0.2 * (Math.random() - 0.5) - 0.01 * velocities[x];
velocities[y] += 0.2 * (Math.random() - 0.5) - 0.01 * velocities[y];
}
voronoi.update();
}
}
Insert cell
positions = Float64Array.from({length: n * 2}, (_, i) => Math.random() * (i & 1 ? height : width))
Insert cell
velocities = new Float64Array(n * 2)
Insert cell
voronoi = new d3.Delaunay(positions).voronoi([0, 0, width, height])
Insert cell
height = 600
Insert cell
margin = 60
Insert cell
n = 200
Insert cell
d3 = require("d3-delaunay@^5.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