RobLoach / pntr_app

Application wrapper for pntr.
https://robloach.github.io/pntr_app/
zlib License
5 stars 1 forks source link

web: canvas sharing #89

Open konsumer opened 9 months ago

konsumer commented 9 months ago

In some code somewhere, the global canvas is being used, which requires this in host code, if you are not following all the assumptions emscripten makes:

window.canvas = canvas
// do stuff that interacts with pntr and hope that nothing else is doing same at same time

This shows up especially, if users are using mjs target, since it self-contains all the scope. A better way to do it, in my opinion, is to always use Module which is a global in single-wasm code, but exposed inside the closure-scope in emscripten-modules. For example, here, Module.canvas is better than canvas.

Essentially, in module-mode, it's possible to have multiple instances of the wasm, with their own memory and screen loading async, but in the current system, there is a potential race-condition, if 2 instances need canvas at the same time (because it's a global.)

You can see an example of that here. Even though each instance has it's own canvas, they cross on init, so it's possible they will use the same canvas instead of their own, and sometimes, when loading 2 web-components on the same page, it will grab the wrong canvas to draw on.

Related:

konsumer commented 9 months ago

I think this is actually a problem with pntr_app. Essentially any use of canvas in a EM_JS should be Module.canvas.

RobLoach commented 7 months ago

:+1: