In p5.js, please draw a robot with eyes, mouth, body, arms, and legs. Please colorize it. Please add a button that when pressed will make the eyes flash for 2 seconds.
Assistant
To create the robot and color it, you can use the following p5.js code snippet:
For adding interactivity, you need to implement an event listener which listens to mouse clicks, checks if the mouse click is on the button, and changes the color of the robot's eyes for 2 seconds.
js
let eyesColor = 0;
let flashTimeout = null;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
drawRobot();
drawButton();
}
function drawRobot() {
// All the same code for drawing the robot, replacing the eyes part with this:
//Eyes
fill(eyesColor);
ellipse(175, 90, 15, 15);
ellipse(225, 90, 15, 15);
}
function drawButton() {
// Same code for drawing the button
}
function mouseClicked() {
if (
mouseX >= 175 &&
mouseX <= 225 &&
mouseY >= 350 &&
mouseY <= 375
) {
// If flash is not in progress, start it
if (!flashTimeout) {
// Change the eyes color to flash
eyesColor = 255;
// Reset the eyes color after 2 seconds
flashTimeout = setTimeout(() => {
eyesColor = 0;
clearTimeout(flashTimeout);
flashTimeout = null;
}, 2000);
}
}
}
Now, by clicking the button "FLASH", the robot's eyes will flash white for 2 seconds.