boolean isMousePressed = false; void setup() { size(400, 400); // Set canvas size background(220); // Set background color } void draw() { // This function is intentionally left empty } void mousePressed() { // Change the background color when the mouse is pressed background(random(255), random(255), random(255)); // Set the mouse pressed state to true isMousePressed = true; // Show text "Clicked" at the center of the canvas showText("Clicked"); } void mouseReleased() { // If the mouse was pressed, change the background color and show "Released" if (isMousePressed) { background(random(255), random(255), random(255)); showText("Released"); } // Set the mouse pressed state to false isMousePressed = false; } void showText(String message) { fill(0); // Set text color to black textSize(20); // Set text size textAlign(CENTER, CENTER); // Center align the text text(message, width / 2, height / 2); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter