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.
function setup() {
createCanvas(500, 500);
}
function draw() {
background(237, 34, 93);
fill(0);
if (mouseIsPressed) {
if (mouseButton === LEFT) {
ellipse(height/2, width/2, 100, 100);
}
if (mouseButton === RIGHT) {
rect(height/2, width/2, 100, 100);
}
}
}
|
Output:
Example 2: This example uses mouseButton variable to detect the mouse button.
function setup() {
createCanvas(500, 500);
textSize(40);
}
function draw() {
background(237, 34, 93);
fill(255);
if (mouseIsPressed) {
text(mouseButton, height/2, width/2);
}
}
|
Output:
Reference: https://p5js.org/reference/#/p5/mouseButton
Last Updated :
16 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...