Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable right click in p5.js?

I'm making a minesweeper clone, left click reveals the cell and right click is supposed to flag the cell.

I'm using the function mouseClicked() - this allows me to reveal on left click and also reveal on right click.

I tried using

if(mouseIsPressed) {
    if(mouseButton === LEFT) {
        reveal
    }
    if(mouseButton === RIGHT) {
        flag
    }
}

This registers once every frame that the button is held down. I just want a single right click. I imagine there is something simple that I'm missing, but I really can't find anything in the documentation or anyone asking about it online.

TL;DR - I just want to be able to right click in p5.js.

like image 982
ObamoDank Avatar asked Aug 02 '19 08:08

ObamoDank


People also ask

Is p5 JS easy to learn?

It's taught from kindergarten right up to university level, and is a powerful tool for artists, designers and programmers despite having a gentle learning curve that's easily achievable. The only limit to what you can create with p5.

What does p5 JS stand for?

The p5. p5. js is a JavaScript library for creative coding. A collection of pre-written code, it provides us with tools that simplify the process of creating interactive visuals with code in the web browser.

What is push and pop in P5js?

Description. The push() function saves the current drawing style settings and transformations, while pop() restores these settings. Note that these functions are always used together. They allow you to change the style and transformation settings and later return to what you had.


2 Answers

The mouseClicked() event only occurs once when the mouse button is pressed.

Set a state flag when the event occurs:

var rightPressed = false;

function mouseClicked() {
    if(mouseButton === RIGHT) {
        rightPressed = true;
    }
}

Reset the flag when the action which is triggered by the event was handled:

function draw() {

    if (rightPressed) {
        rightPressed = false

        // do somthing
        // ...
    } 
}
like image 113
Rabbid76 Avatar answered Oct 09 '22 08:10

Rabbid76


Use the mouseClicked() function and pass the event.

Then using event.button you can determine if it is a right click like so:

function mouseClicked(event){
  if(event.button === 2){ 
    // Right click 
  }
}
like image 44
Anurag Srivastava Avatar answered Oct 09 '22 10:10

Anurag Srivastava