Skip to content
Advertisement

How can I share methods that call P5.js functions between two different sketches/canvases?

Is there a way to share common methods between two different sketches/canvases? It only works if I’m not referencing any P5.js methods.

In the example below there are two sketches and each is loaded into a different canvas element using the instance mode for P5.js.

I’d like to be able to write a function that uses P5.js methods AND is accessible to each sketch.

Basically I’m trying to avoid duplicating code in two sketches if it isn’t necessary.

Thanks

JavaScript
JavaScript

Advertisement

Answer

The p5 object needs to be an argument of the function:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement