Published
Edited
Apr 30, 2019
1 fork
1 star
Insert cell
Insert cell
Insert cell
render = {
const particleContext = DOM.context2d(w, h);
const randomWalkParticles = Array.from({length: activeParticles},
() => [Math.random() * w,
Math.random() * h,
(Math.random() - 0.5),
(Math.random() - 0.5)]);
const minDistance2 = minDistance * minDistance;
const maxDistance2 = maxDistance * maxDistance;
for(let iterations = 0; iterations < maxIterations; iterations++){
particleContext.strokeStyle= colorScale(iterations);


//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();
}
}
}

let c = particleContext.canvas;
c.style.width= "100%";
c.style.height= "100%";
c.style.position= "absolute";
c.style.zIndex= -1;
c.style.top= 0;
c.style.left= 0;
yield c;

for (const p of randomWalkParticles) {
//Random Walk
p[0] += p[2];
p[1] += p[3];
if (p[0] < (0 - maxDistance)) p[0] = w + maxDistance;
else if (p[0] > (width + maxDistance)) p[0] = 0 - maxDistance;
if (p[1] < (0 - maxDistance)) p[1] = h + maxDistance;
else if (p[1] > (h + 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
colorScale = d3.scaleLinear()
.domain([0,
maxIterations*0.182,
maxIterations*0.191,
maxIterations*0.2,
maxIterations*.75,
maxIterations*.9,
maxIterations])//, maxIterations])
.range(["#134870",
"hsl(211, 42%, 52%)",
"hsl(357, 79%, 32%)",
"hsl(211, 42%, 52%)",
"#134870",
"hsl(53, 90%, 52%)",
"hsl(39, 90%, 52%)"]);//, "hsl(53, 90%, 52%)"]);
Insert cell
Insert cell
activeParticles = 9
Insert cell
h = 500//Math.max(document.documentElement.clientHeight, window.innerHeight || 1);
Insert cell
w = 500//Math.max(document.documentElement.clientHeight, window.innerHeight || width);
Insert cell
pxCnt = w*h
Insert cell
Insert cell
Insert cell
minDistance = maxDistance / 10;
Insert cell
maxDistance = sidePerParticle
Insert cell
maxIterations = 1500;
Insert cell
Insert cell
d3 = require("d3-scale@2")
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