I have already written the p5.3D library, for adding 3D text and images into WebGL, but currently this only works for Global Mode.
I’m not sure exactly what needs to be done, or how it needs to be reformatted, to make it work for Instance Mode. Has anyone else on here already converted a library to do this, or understands what is required?
Right okay, but that means that in the instance mode, you’re calling the function and passing in the instance. In other libraries, the function can be called by the instance i.e.
var sketch = p5(function(p){p.setup(){...}; p.draw(){p.function(){}}})
as opposed to
var sketch = p5(function(p){p.setup(){...}; p.draw(){function(p){}}})
I understand how you achieved that, though thank you for explaining, my previous comment was just saying that I don’t want to have to pass in the sketch’s references, and I know there are other ways to do it from what I have seen other libraries use. It is these methods that I want to implement.
Now the solution you’re looking for: Not request the p5 object from your library’s user!
You already know by now that directly instantiating classes using the keyword new can’t consistently get a sketch’s p5 object, unless they require it as their constructor()'s parameter.
There are some workarounds though like p5.instance, p5.Element::pInst, p5.Vector::p5, etc.
However, they’re all circumstantial and don’t work for all cases.
What you need to do instead is adding an instantiator method in the class p5’s prototype{} object.
B/c it’s a p5 method, it automatically receives the p5’s object via the keyword this every time it’s invoked!
That’s exactly how p5 methods like createVector(), createCanvas(), createGraphics(), createElement(), etc. work!
They all instantiate a class in our place passing the p5’s this as 1 of the arguments behind-the-scenes.
You’d only need to advice your library’s users to prefer those createXXX() methods rather than directly instantiating a class via new:
That’s awesome, thank you very much for your help on this. It now works exactly as intended, and the “create” syntax is nicer anyway since it’s closer to what already exists.