Published
Edited
Mar 11, 2019
Fork of !V
1 fork
Insert cell
Insert cell
Insert cell
render = {
const particleContext = DOM.context2d(width, height);
particleContext.fillStyle = 'white';
particleContext.fillRect(0,0,width,height);
const randomWalkParticles = Array.from({length: activeParticles},
() => [Math.random() * width,
Math.random() * height,
(Math.random() - 0.5),
(Math.random() - 0.5)]);
const minDistance2 = minDistance * minDistance;
const maxDistance2 = maxDistance * maxDistance;
while(true){
particleContext.globalAlpha = 0.002
particleContext.fillRect(0,0,width,height);
particleContext.strokeStyle= 'hsl(220, 53%, 28%)';
//Connections
for (let i = 0; i < activeParticles; ++i) {
for (let j = i + 1; j < activeParticles; ++j) {
const pi = randomWalkParticles[i];
const pj = randomWalkParticles[j];
const dx = pi[0] - pj[0];
const dy = pi[1] - pj[1];
const d2 = dx * dx + dy * dy;
if (d2 < maxDistance2) {
particleContext.globalAlpha = d2 > minDistance2 ? (maxDistance2 - d2) / (maxDistance2 - minDistance2) : 1;
particleContext.beginPath();
particleContext.moveTo(pi[0], pi[1]);
particleContext.lineTo(pj[0], pj[1]);
particleContext.stroke();
}
}
}


yield particleContext.canvas;

for (const p of randomWalkParticles) {
//Random Walk
p[0] += p[2];
p[1] += p[3];
if (p[0] < (0 - maxDistance)) p[0] = width + maxDistance;
else if (p[0] > (width + maxDistance)) p[0] = 0 - maxDistance;
if (p[1] < (0 - maxDistance)) p[1] = height + maxDistance;
else if (p[1] > (height + maxDistance)) p[1] = 0 - maxDistance;
p[2] += 0.3 * (Math.random() - 0.5) - .01 * p[2];
p[3] += 0.3 * (Math.random() - 0.5) - .01 * p[3];
}
}
}
Insert cell
Insert cell
activeParticles = Math.max(Math.floor(width*height/(minDistance*minDistance+maxDistance*maxDistance)),3);
Insert cell
Insert cell
minDistance = width * 0.05
Insert cell
maxDistance = minDistance * 5
Insert cell
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