Published
Edited
Nov 13, 2020
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
n = 8000 // don't be shy, try 30k!
Insert cell
Insert cell
transport = {
let dirs = 4,
strength = .8;
const w = width / 2,
h = (height * (w / width)) | 0;

let indices = [],
projection,
deltas;

const sample = Uint32Array.from(
d3.range(w * h).filter(k => bitmap((k % w) / w, Math.floor(k / w) / h))
);
const indicesq = Uint32Array.from(d3.range(sample.length));
const xs = Float32Array.from(indicesq, k => sample[k] % w);
const ys = Float32Array.from(indicesq, k => Math.floor(sample[k] / w));
const projectionq = new Float32Array(indicesq.length);

const pool = [];

// 137.51°, see https://observablehq.com/@fil/phyllotaxis-explained
const baseAngle = tau / (1.5 + Math.sqrt(1.25));
let a = 0;

return points => {
const n = points.length / 2;
if (n !== indices.length) {
indices = Uint32Array.from(d3.range(n));
projection = new Float32Array(n);
deltas = new Float32Array(2 * n);
}

deltas.fill(0);

// create a new projection
if (pool.length < 100) {
a += baseAngle;
const sa = Math.sin(a),
ca = Math.cos(a);
for (let i = 0; i < indicesq.length; i++)
projectionq[i] = (ca * xs[i] + sa * ys[i]) * (width / w);
indicesq.sort((i, j) => projectionq[i] - projectionq[j]);

pool.unshift({ a, sa, ca, p: projectionq.slice(), o: indicesq.slice() });
}

for (let d = 0; d < Math.min(dirs, pool.length); d++) {
const { a, sa, ca, p, o } = pool[d];

for (let i = 0; i < n; i++)
projection[i] = ca * points[2 * i] + sa * points[2 * i + 1];
indices.sort((i, j) => projection[i] - projection[j]);

let i, ideal, delta;
for (let k = 0; k < n; k++) {
i = indices[k];
ideal = p[o[Math.floor(((k + 1) / (n + 1)) * o.length)]];
delta = ideal - projection[i];
deltas[2 * i] += ca * delta;
deltas[2 * i + 1] += sa * delta;
}
}
for (let i = 0; i < points.length; i++)
points[i] += (deltas[i] / dirs) * strength;

d3.shuffle(pool);
};
}
Insert cell
points = {
const random = d3.randomNormal(0, 10);
return Float32Array.from(
{ length: 2 * n },
(_, i) => (i % 2 ? height : width) / 2 + random()
);
}
Insert cell
bitmap = {
const w = 400,
h = ((im.height / im.width) * w) | 0;
const context = DOM.context2d(w, h, 1);
context.drawImage(im, 0, 0, w, h);
const pixels = context
.getImageData(0, 0, w, h)
.data.filter((_, i) => i % 4 === 0);
return (x, y) =>
255 - pixels[Math.floor(x * w) + w * Math.floor(y * h)] > 127;
}
Insert cell
height = ((im1.height / im1.width) * width) | 0
Insert cell
im1 = FileAttachment("conformal-dymaxion.png").image()
Insert cell
im = {
if (image === "Earth") return im1;

const context = DOM.context2d(width, height, 1);
context.fillStyle = "white";
context.fillRect(0, 0, width, height);
const n = 3 + Math.random() * 8;
for (let i = 0; i < n; i++) {
context.beginPath();
context.arc(
Math.random() * width,
Math.random() * height,
30 + Math.random() * 60,
0,
tau
);
context.fillStyle = "black";
context.fill();
}
return context.canvas;
}
Insert cell
d3 = require("d3@6")
Insert cell
tau = 2 * Math.PI
Insert cell
import { checkbox, select } from "@jashkenas/inputs"
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