Thursday, October 16, 2025
HomeLanguagesJavascriptp5.js | Mouse | mouseIsPressed

p5.js | Mouse | mouseIsPressed

The mouseIsPressed system variable in p5.js is used to store the boolean value. If mouse is pressed then it stores True otherwise store False.

Syntax:

mouseIsPressed

Below programs illustrate the mouseIsPressed variable in p5.js:

Example 1: This example uses mouseIsPressed variable to check mouse is press or not.




function setup() {
   
    // Create canvas of given size
    createCanvas(500, 250);
    
    // Set the text size
    textSize(30); 
}
    
function draw() {
       
    // Set the background color
    background('green');
       
    fill('white');
       
    // If mouse is pressed then if part will 
    // execute otherwise else part will execute
    if (mouseIsPressed) {
        text("Mouse is Pressed", 120, 100);
    }
    else {
        text("Mouse is Released", 120, 100);
    }
}


Output:

Example 2:




function setup() {
  
    // Create Canvas of given size
    createCanvas(300, 150);
}
   
function draw() {
      
    // Set the background color
    background('green');
      
    fill('white');
  
    // Use mouseIsPressed variable
    if (mouseIsPressed) {
        ellipse(50, 50, 50, 50);
    
    else {
        rect(25, 25, 50, 50);
    }
}


Output:

Reference: https://p5js.org/reference/#/p5/mouseIsPressed

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS