Published
Edited
Mar 20, 2020
2 stars
Insert cell
Insert cell
Insert cell
{
replay;
const context = DOM.context2d(width, height);
context.translate(width / 2, height / 2);

const n = 92; // must be even
const points = [];
for (let i = 0; i < n; ++i) {
const p = coordsFromDeg(i * (360 / n), 300);
points.push(p);
}

const lines = [];
const times = [];
const signs = [];
for (let i = 0; i < n / 2; ++i) {
const l = [points[i], points[n / 2 + i]];
times.push(0);
signs.push(1);
lines.push(l);
}

yield context.canvas;
let numL = 0;
let t = 0;
while (true) {
await Promises.delay(10);
context.clearRect(-width / 2, -height / 2, width, height);

for (let i = 0; i <= numL; ++i) {
line(...lines[i], context, { color: 'rgba(100,100,100,0.3)' });
const q = pointOnLine(lines[i], times[i]);
circle([...q, 3], context);
times[i] += signs[i] * 0.0033;
if (times[i] < 0 || times[i] > 1) signs[i] *= -1;
}

if (numL < lines.length - 1 && t % 20 === 19) {
numL++;
}

t++;
}
}
Insert cell
height = width / 1.5
Insert cell
import { circle, line, pointOnLine, coordsFromDeg } from '@timhau/geometry'
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