xxxxxxxxxx
41
let cols = 10;
let rows = 10;
let cellWidth;
let cellHeight;
let grid = [];
function setup() {
createCanvas(400, 400);
cellWidth = width / cols;
cellHeight = height / rows;
for (let i = 0; i < rows; i++) {
grid[i] = [];
for (let j = 0; j < cols; j++) {
grid[i][j] = false;
}
}
}
function draw() {
background(255);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j]) {
fill(255, 0, 0);
} else {
fill(255);
}
stroke(0);
rect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
}
}
}
function mousePressed() {
let j = floor(mouseX / cellWidth);
let i = floor(mouseY / cellHeight);
if (i >= 0 && i < rows && j >= 0 && j < cols) {
grid[i][j] = !grid[i][j];
}
}