xxxxxxxxxx
let rows = 12;
let cols = 12;
let cellSize;
let colors = [];
function setup() {
createCanvas(800, 800);
cellSize = width / cols; // Calculate cell size
// Initialize colors array with random colors
for (let i = 0; i < rows * cols; i++) {
colors.push(randomColor());
}
}
function draw() {
background(220); //
// Loop through rows and columns to draw grid
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let index = i * cols + j;
let x = j * cellSize; // Calculate x position
let y = i * cellSize; // Calculate y position
// Set fill color based on colors array
fill(colors[index]);
// Draw rectangle for each cell
stroke(0); // Set stroke color to black
rect(x, y, cellSize, cellSize); // Draw rectangle
}
}
}
function keyPressed() {
if (key === 'r' || key === 'R') {
// Update colors array with new random colors
for (let i = 0; i < rows * cols; i++) {
colors[i] = randomColor();
}
}
}
// Function to generate a random color
function randomColor() {
return color(random(255), random(255), random(255));
}