Public
Edited
Nov 5, 2024
Importers
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
spatialSubdivision = function(H, rnd) {
const nb = 2*(H.length-1)+1;

const H2 = [];
// for each cell in new array
for (let i = 0; i < nb; i++) {
H2[i] = [];
let I = i>>1;
for (let j = 0; j < nb; j++) {
let J = j>>1;
// Interpolate current cells
H2[i][j] = (H[I][J]+H[I+(i&1)][J]+H[I][J+(j&1)]+H[I+(i&1)][J+(j&1)])/4;
// Add randomness on new cells
if (i&1 || j&1) H2[i][j] += rnd();
}
}

return H2;
}
Insert cell
Insert cell
Insert cell
Insert cell
generateHeightmap = (nbIters, seed, { p1 = 0.5, p2 = 1.1 } = {}) => {
const prng = minstRand0(seed);
let H = [
[prng(), prng()],
[prng(), prng()]
];

// Generate heightmap array
let f = p1;
for (let n = 0; n < nbIters; n++) {
let f2 = 2 * f;
H = spatialSubdivision(H, function () {
return prng() * f2 - f;
});
f /= 2 ** p2;
}

const N = H.length;
console.log(N);

// Compute heightmap stats
let [min, max] = [+Infinity, -Infinity];
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
let h = H[i][j];
min = Math.min(min, h);
max = Math.max(max, h);
}
}

for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
H[i][j] -= min;
}
}

return { heightmap: H, max: max - min };
}
Insert cell
viewof heightmap = {
const nbIters = 10;
const { heightmap, max } = generateHeightmap(nbIters, seed, shaping);
const N = heightmap.length - 1;
const { context, dispatch } = viewofCanvas(256, 256);
dispatch({ data: heightmap, hunit: 1, vunit: N / 4 });
return canvas(context, samplePixel(heightmap, 256 / max));
}
Insert cell
Insert cell
{
const palette = colorShades([253, 205, 159], 256);
const colormap = { ...shademap.colormap, palette };
const dims = [width, Math.floor(width * 0.6)];
return orbitalCanvas({ ...shademap, colormap }, dims);
}
Insert cell
({
...shademap,
colormap: { ...shademap.colormap, palette: colorShades([253, 205, 159], 256) }
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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