xxxxxxxxxx
let button1, color;
function setup() {
createCanvas(windowWidth, windowHeight);
rectMode(CENTER);
button1 = new Button();
color = true;
}
function draw() {
button1.display();
}
class Button {
constructor() {
this.x = width / 2;
this.y = height / 2;
}
display() {
if (color) {
background(255);
} else {
background(0)
}
rect(this.x, this.y, 50, 50);
}
tap() {
if (mouseX > this.x - 25 &&
mouseX < this.x + 25 &&
mouseY > this.y - 25 &&
mouseY < this.y + 25 &&
color == true) {
color = false;
} else if (mouseX > this.x - 25 &&
mouseX < this.x + 25 &&
mouseY > this.y - 25 &&
mouseY < this.y + 25 &&
color == false) {
color = true;
}
}
}
function mousePressed() {
button1.tap();
}