p5.js | Mouse | mouseButton

The mouseButton variable in p5.js is used to get automatically which kind of key is pressed on mouse. The value of the system variable mouseButton is either LEFT, RIGHT, or CENTER depending on which button was pressed last.

Syntax:

mouseButton

Below programs illustrate the mouseButton variable in p5.js:



Example 1: This example uses mouseButton variable to detect the mouse button.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create canvas
    createCanvas(500, 500);
}
   
function draw() {
      
    // Set background color
    background(237, 34, 93);
      
    // Fill color
    fill(0);
   
    if (mouseIsPressed) {
        if (mouseButton === LEFT) {
            ellipse(height/2, width/2, 100, 100);
        }
        if (mouseButton === RIGHT) {
            rect(height/2, width/2, 100, 100);
        }
    }
}

chevron_right


Output:

Example 2: This example uses mouseButton variable to detect the mouse button.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create canvas
    createCanvas(500, 500);
      
    // Set the text size
    textSize(40); 
}
   
function draw() {
      
    // Set background color
    background(237, 34, 93);
      
    // Set the text color
    fill(255);
      
    // Check the status of the mouse
    if (mouseIsPressed) {
        text(mouseButton, height/2, width/2);
    }
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


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