We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I'm trying to create an interactive page using p5js where you click on various images to change between them. The page starts out with 5 different images I have put in various places, and for each of those 5 images I have 3-4 alternative images to change between upon click. I have all of the different images loaded as different variables, and have tested all of the alternative images to make sure their varying sizes fit nicely in the same (x,y) placement of the original images. I am now stuck trying to make the interactivity happen- I tried starting out changing one of the images using the mousePressed() function, which worked for switching between just 2 images, but I want to randomize between the 3+ images upon click. I then tried making arrays for each of the 5 original images, and in those arrays were the options for the other images. I think this might work with the mousePressed() function as well, but I am having trouble with the syntax of randomizing images in an array upon click... also having trouble with the placement of the mouse for the click because these are not square images. Is there a way I can just have the conditional statement apply to clicking on the image itself rather than the x & y ranges? Also, I might be completely off with everything, should I even use arrays? If anyone has suggestions I would very much appreciate it because this seems like a big undertaking and I only just learned p5js about a month ago!
Answers
That seems daunting... Just had the time to come up w/ an untested example for you to start out.
It assumes you've got a subfolder called "pics/" w/ exactly 25 ".png" images named from "0.png" till "24.png". I guess it's worth a try. Any doubts ask about them here. Good luck: [-O<
I'd personally try this outside of the canvas and do it directly with DOM elements: you can handle click events against these directly which should avoid the need for comparing mouse position in the canvas against image width/height.
Using arrays to store possible images at each location seems fine to me; though it's not clear whether you're happy for the same image to be repeated...