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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.