function setup() {
createCanvas(1000, 1000);
squareSize = 10;
noiseSeed(6);
noise_scale = 0.0005;
background(100);
}
function draw() {
noLoop();
// We'll keep track of noise() values in a histogram
hist_plot_bins = 50;
histo_to_plot = Array(hist_plot_bins).fill(0);
// Loop through each row and column of a grid
for (let row = 0; row < height / squareSize; row++) {
for (let col = 0; col < width / squareSize; col++) {
// Calculate the x and y position of the square
let x = col * squareSize;
let y = row * squareSize;
n = noise(x * noise_scale, y * noise_scale);
fill(n * 255);
noStroke();
// Draw the square
rect(x, y, squareSize, squareSize);
// Fill our histogram with noise() values
hist_plot_bin = int(n * hist_plot_bins);
histo_to_plot[hist_plot_bin] += 1;
}
}
// Draw the histogram
{
sum_histo_plot_counts = histo_to_plot.reduce((a, b) => a + b, 0);
max_histo_count = Math.max(histo_to_plot);
histo_w = 400;
histo_h = 100;
noFill();
stroke(255);
strokeWeight(1);
translate(width/8, 3*height/4)
rect(0, 0, histo_w, histo_h);
for (x = 0; x <= hist_plot_bins; x += 1) {
fill(255, 0, 0);
normalized_height = map(histo_to_plot[x], 0, max_histo_count, 0, 1, true) * 100;
rect(
x / hist_plot_bins * histo_w,
histo_h,
histo_w / hist_plot_bins,
-normalized_height
);
}
}
}