canvas = {
const context = DOM.context2d(width, height);
context.strokeStyle = "red";
while (true) {
context.clearRect(0, 0, width, height);
context.beginPath();
for (let i = 0; i < n; ++i) {
const x1 = i << 1, y1 = x1 + 1;
const x2 = nearestNeighbor(i) << 1, y2 = x2 + 1;
context.moveTo(positions[x1], positions[y1]);
context.lineTo(positions[x2], positions[y2]);
}
context.stroke();
context.beginPath();
delaunay.renderPoints(context);
context.fill();
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];
}
delaunay.update();
}
}