xxxxxxxxxx
37
let fillColor = false; //toggle white or red
let mouseposition = true; //if mouse has left the block
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
// Draw middle block
fill(255);
rect(width / 3, 0, width / 3, height);
// Draw right block
rect((width / 3) * 2, 0, width / 3, height);
// Interaction with left block
if (mouseX < width / 3 && mouseX > 0) {
//if mouse enters the left block
if (mouseposition) {
fillColor = !fillColor;
mouseposition = false;
}
} else { //if it leaves the block
mouseposition = true;
}
// Draw left block
if (fillColor) {
fill(255, 0, 0); // Red when toggled on
} else {
fill(255); // white when toggled off
}
rect(0, 0, width / 3, height);
}