xxxxxxxxxx
31
let redd;
let lastredd;
function setup() {
createCanvas(600, 400);
redd = false; // if it is red
lastredd = false; // if it is red before
}
function draw() {
background(220);
// draw 2 rect
fill(255);
rect(200, 0, 200, 400);
rect(400, 0, 200, 400);
// set condition
if (mouseX <= 200 && mouseX >= 0 && !lastredd) { // if the mouse is inside of the box and it is red before
redd = !redd; // no red fill
lastredd = true; // it is red before
} else if (mouseX > 200 || mouseX < 0) { // mouse outside of the first box
lastredd = false; // reset state
}
// link redd to fill red
if (redd) {
fill(255, 0, 0);
} else {
fill(255);
}
rect(0, 0, 200, 400);
}