xxxxxxxxxx
32
// Attributes of grid
let numCols = 10; // Number of columns
let numRows = 10; // Number of rows
let w, h; // Width and Height of each cell
function setup() {
createCanvas(400, 400);
// Calculate w and h of each cell in relation to width and height of canvas
w = width/numCols;
h = height/numRows;
}
function draw() {
background(220);
// For every column
for(let c = 0; c < numCols; c++) {
// Calculate x-position of column: How many pixels is it from the left-side of the canvas?
let x = c * w;
// For every row
for(let r = 0; r < numRows; r++) {
// Calculate y-position of row: How many pixels is it from the top-side of the canvas?
let y = r * h;
// Check left-right, top-bottom boundaries of this cell
if(mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h) fill('red');
else fill('white');
// Draw the cell
rect(x, y, w, h);
}
}
}