stalgiag / p5.xr

a library that helps turn p5.js sketches into immersive experiences using WebXR
GNU Lesser General Public License v2.1
117 stars 25 forks source link

createCanvas compatible API #231

Closed TiborUdvari closed 1 month ago

TiborUdvari commented 1 month ago

We should consider an interface similar to the p5.js way of doing things. It would reduce friction if we could take an existing WEBGL sketch and change it to a WEBXR AR or VR sketch by doing something like this:

function setup(){
  createCanvas(1000, 1000, AR);
}

instead of

function preload() {
  createARCanvas();
}

The current p5.js implementation for reference:

/**
 * @method createCanvas
 * @param  {Number} [width]
 * @param  {Number} [height]
 * @param  {HTMLCanvasElement} [canvas]
 * @return {p5.Renderer}
 */
p5.prototype.createCanvas = function(w, h, renderer, canvas) {

It would have to add a parameter to consider starting the sketch automatically as well: #224

stalgiag commented 1 month ago

Yeah this makes sense. The reason that this wasn't done initially is that modifying p5 prototypes directly is not recommended in the library authoring guidelines for p5. That said, I believe we had to do this elsewhere so it wouldn't be the most extreme departure.

TiborUdvari commented 1 month ago

Yes, I think generally it's a good idea, but in think in these cases it would make sense. I think people would expect and interface like this.

I'm adding this feature with #235

This keeps the createARCanvas type functionality still working, although there are now a few lines like p5.instance._incrementPreload(); in the __createButton that are not needed if the preload method is used for initialisation.

A future refactor could/should be done to better differentiate the two.