Right now I am exporting the "singleton" WebGL2RenderingContext as follows:
export default GLHelper.GetGLContext(UIHelper.Canvas);
After that, I use it throughout the application, for example:
import gl from '.'; gl.DrawElements();
The structure of my project has its root in the class app.ts Inside this class, I have instances of other classes, and basically each class uses the above export.
My problem is that as soon as I decide to use multiple canvas elements, I will have several WebGL2RenderingContext objects, so my singleton export will always work on the same canvas.
The only solution for this case that I can think of is to get all the context objects and pass them as a parameter in the whole code. I am thinking of something like:
const ctx1 = GLHelper.GetGLContext("id1"); const ctx2 = GLHelper.GetGLContext("id2"); const app1 = new App(ctx1); const app2 = new App(ctx2);
In the App class, I would need to continue passing the object as follows:
class App { private _ctx: WebGL2RenderingContext; constructor (ctx: WebGL2RenderingContext) { this._ctx = ctx;
What I would like to achieve is one export in the App class, which will automatically be used in other classes by simply importing it. Thus, I would like renderingObj automatically use the context that was exported to the App instance in which it was created. All this would be really cool if I did not have to pass the context as a parameter.
Is there a way to achieve this or maybe solve the problem differently?
source share