xxxxxxxxxx
46
const C = 40;
let grid = Array.from(Array(C), () => new Array(C))
function setup() {
createCanvas(400, 400);
background(220);
frameRate(60);
}
function draw() {
if (key == ' ' && keyIsPressed || mouseIsPressed) {
background(220)
for (let i = 0; i < C; i++) {
for (let j = 0; j < C; j++) {
let x = i * width / C;
let y = j * height / C;
if (isInside(x, y, width / C, height / C, mouseX, mouseY)) {
if (mouseIsPressed && grid[j][i] != 1) {
grid[j][i] = 1;
}
fill(255, 0, 0);
} else {
if (grid[j][i] == 1) {
fill(0)
} else {
noFill();
}
}
rect(x, y, width / C, height / C);
}
}
}
}
function isInside(x, y, w, h, X, Y) {
if (X < (x + w) &&
X > x &&
Y < (y + h) &&
Y > y
) {
return true
}
return false
}