#multiple p5 instance tutorial

6 messages · Page 1 of 1 (latest)

molten wedge
#

Does the incredible Daniel Shiffman offer any tutorials or videos about running multiple instances of p5 on a site/multiple canvasses? I'm running into issues with p5.sound.js.

silver kestrel
#

i have an 8 year old video but not sure if it is too out of date? https://youtu.be/Su792jEauZg

This video looks at p5.js "instance mode". The techniques demonstrated are useful for "namespacing" your sketch (so that there are no conflicts with other libraries or other global variables). It is also useful if you want to have more than one canvas on the same web page.

code: https://github.com/shiffman/Video-Lesson-Materials/tree/master/c...

▶ Play video
molten wedge
# silver kestrel take a look at this https://github.com/processing/p5.js/wiki/Global-and-instance...

Thank you. The video was indeed helpful (as are most of your videos)

But I'm struggling with keyPressed events.

Some context:
I have three different games on an html page, each in their own canvas. I did it the instance way that you showed in your video. Two of those canvases(games) listen for key presses. And for some reason only one of them (canvas1) actually triggers/registers a key press, the other canvas (canvas3) ignores it.

Funnily enough, the mouse business all works fine for all three canvasses. It's just keyboard that's not cooperating. Any insight? 😅

#

If it helps for further context, each canvas sits in a div that can be hidden with a checkbox. So in practice, I'd like for the canvas inside the visible div to receive the key presses and not the canvasses inside the hidden divs.

molten wedge
#

I got it to work after finding a closed issue on p5.js' github.

Even if you use instance mode for your canvases, keyPressed will always be a global event and there can only be one. So that requires a work-around.
In the end I used several keyIsDown() functions inside my draw rather than a global keyPressed()