I want to be able to log the position of the mouse when it is clicked on the canvas, I can get it to work with mousePressed but the function mouseClicked() is not working can anyone point me in the right direction?
If you look at the p5js documentation for the mouseClicked() function, it says :
The mouseClicked() function is called once after a mouse button has been pressed and then released.
Browsers handle clicks differently, so this function is only guaranteed to be run when the left mouse button is clicked. To handle other mouse buttons being pressed or released, see mousePressed() or mouseReleased().
So are you pressing your mouse then releasing it? Are you pressing the left mouse button?
Yeah I’m clicking and releasing the left mouse button but it doesn’t seem to be registering the clicks
Would it be better if I put the entirety of my code?
Ok the first thing that I notice is that there’s a lot of repetitions in your code
Most of the time, if you have so much if statements, it’s that your code is not optimized. The whole point of programming is not to repeat yourself, otherwise you can do it by hand.
I suppose that you are displaying images on a grid and you want to detect the location of the mouse on the grid when you click. If it’s what you want to do, we can figure out how to do it.
Next point (this is probably the problem) is that you are using a variable array that you never declared in your code. So accessing array[i][j] should give you an error.
Yeah, I’m pretty new to coding so optimising isn’t my strong point, sorry. Would it be better for me to write a function to check the mouseX and use it each time?
The array I have declared in the head of html code
I suggest you separate your JavaScript file(s) from your HTML file because it’s much more convenient to have separate files for debugging. So declare your global variables in the same file that your script.
Personally I never use mouseClicked() always mousePressed() because it does the same basically.
And if you think about it, all your if statements are just creating a loop through the length of the array and checking if the mouse is between a certaint column and row.
Yeah, my issue will be that I will want to use an AJAX call when the mouse is clicked so I’m worried that if I use mousePressed it will send multiple rather than just the one