Public
Edited
Feb 1, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
canvas = {
const context = DOM.context2d(width, height);

context.clearRect(0, 0, width, height);

let points = new FastPoissonDiskSampling({
shape: [width, height],
radius: 25,
tries: 1
});
let voronoi = new d3.Delaunay(points.fill().flat()).voronoi([0, 0, width, height]);
context.beginPath();
context.strokeStyle = "teal";
voronoi.delaunay.render(context);
context.stroke();
context.beginPath();
context.strokeStyle = "turquoise";
voronoi.render(context);
context.stroke();
context.beginPath();
voronoi.delaunay.renderPoints(context);
// context.fill();

// yield context.canvas;
// for (let i = 0; i < n; ++i) {
// const x = i << 1;
// const y = x + 1;
// positions[x] += velocities[x];
// positions[y] += velocities[y];
// if (positions[x] < -margin) positions[x] += width + margin * 2;
// else if (positions[x] > width + margin) positions[x] -= width + margin * 2;
// if (positions[y] < -margin) positions[y] += height + margin * 2;
// else if (positions[y] > height + margin) positions[y] -= height + margin * 2;
// velocities[x] += 0.2 * (Math.random() - 0.5) - 0.01 * velocities[x];
// velocities[y] += 0.2 * (Math.random() - 0.5) - 0.01 * velocities[y];
// }
// voronoi.update();
// }
return context.canvas;
}
Insert cell
// d3 = require("d3-delaunay@^5.1")
d3 = require("d3@^6.0")
Insert cell
FastPoissonDiskSampling = require("https://cdn.jsdelivr.net/gh/kchapelier/fast-2d-poisson-disk-sampling@1.0.3/build/fast-poisson-disk-sampling.min.js")
Insert cell
noisejs = import("https://cdn.skypack.dev/noisejs@2.1.0")
Insert cell
d3.Delaunay
Insert cell
height = 600;
Insert cell
n = 100;
Insert cell
positions = Float64Array.from({length: n * 2}, (_, i) => Math.random() * (i & 1 ? height : width))
Insert cell
points = new FastPoissonDiskSampling({
shape: [width, height],
radius: 6,
tries: 20
});

Insert cell
points.fill().flat()
Insert cell
noise = new noisejs.Noise

Insert cell
Insert cell
d3.schemeSpectral[11]
Insert cell
Insert cell
Insert cell
cos_grid = {
const context = DOM.context2d(width, height);
context.clearRect(0, 0, width, height);

let points = new FastPoissonDiskSampling({
shape: [width, height],
radius: 8,
tries: 1
});

let noise = new noisejs.Noise();
noise.seed(d3.randomInt(65536)());
let voronoi = new d3.Delaunay(points.fill().flat()).voronoi([0, 0, width, height]);

let colors = d3.schemeSpectral[11];
let scale = d3.scaleQuantize()
.domain([0, 1])
.range([colors[11],colors[11],colors[10],colors[10],
colors[9],colors[9],colors[8],colors[8],
colors[7],colors[6],colors[5],colors[4],
colors[3],colors[2],colors[1]])

// .range(colors);

let { corners, triangles } = voronoi.delaunay;
let n_tri = triangles.length / 3;

let center_x = width / 2;
let center_y = height / 2;

let dist_factor = 0.5 * Math.sqrt( (width * width) + (height * height));


for (let i = 0; i < n_tri; i++) {
let polygon = voronoi.delaunay.trianglePolygon(i);
let x = polygon[0][0];
let y = polygon[0][1];
let noise_value = (noise.simplex2(x / 512, y / 384));
let noise_value_2 = (noise.simplex2(x / 32, y / 24));

let dist = Math.sqrt( (x - center_x) * (x - center_x) + (y - center_y) * (y - center_y) );
let dist_value = 1 - (dist / dist_factor);

let cos_dist = Math.cos(dist * Math.PI * 0.5 / dist_factor);

let n_val = noise_value > 0 ? noise_value : 0.0;

let comb_value = cos_dist * 0.3 + (n_val) * 0.9 * cos_dist;

if (comb_value <= 0.3) {
context.fillStyle = scale(0.2);
} else if (comb_value <= 0.8) {
context.fillStyle = scale(comb_value);
} else {
context.fillStyle = scale(0.8);
}
context.beginPath();
voronoi.delaunay.renderTriangle(i,context);
context.fill()

}
return context.canvas;
}
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