Multiple instances of the same p5js sketch?

Hello. I’m looking for a way to put multiple instances of the same p5js sketch on my site. I know the obvious method is copy-and-pasting my sketch.js and changing the IDs for each, but I want to have a dynamic amount of instances of the sketch. Thank you for any help!

This is my sketch: https://hastebin.com/zuhenivaba.js

1 Like

B/c it’s a global mode sketch, you can have at most 1 per page.

As a workaround, you can place other global mode instances of it inside their own <iframe> tag:

Or simply convert it to instance mode, so you can have multiples of it on the same page context:

Here’s an example of 4 instances of the same sketch running on the same page:

Go to the link below for its full source code:

Just click the button “View Source”.

2 Likes