How to adapt a library for Instance Mode (P5JS)

  • Not sure if I understood your example above. :confused:
  • Anyways, a p5js library needs to work whether a sketch is using global or instance mode. :construction_worker_man:
  • And a sure way to accomplish such flexibility is for the library to request the sketch’s p5 reference from its user. :books:
  • As an example, take a look at how I instantiate classes Ball & Chamber: :see_no_evil:

https://github.com/GoSubRoutine/Ball-in-the-Chamber/blob/2.0.5/instance/sketch/sketch.js#L30-L31
https://github.com/GoSubRoutine/Ball-in-the-Chamber/blob/2.0.5/instance/sketch/sketch.js#L33-L40

  • Even though the “sketch.js” above is using instance mode, if I decide to convert it to global mode, the 2 code blocks above would be exactly the same! :flushed:
  • That is, classes Ball & Chamber work regardless the current mode a sketch is using, b/c they request the sketch’s reference as their 1st parameter! :partying_face:
1 Like