rust-windowing/winit

Canvas ID not unique on a web page

MarcAntoine-Arnaud opened this issue · 3 comments

Generating web component that requires winit to create a canvas, we found an issue as winit generates the Id of the canvas based on an auto-incremental number.
So our 2 canvas has the same data-raw-handle equals to 1, and using wgpu the fetch of the WindowID is the wrong one.

Do you have any suggestion to change the line here:

pub fn generate_id(&self) -> u32 {
and generate an UUID instead of an strictly increasing ID ?

Thanks,
Marc-Antoine

It would have to be done together with raw_window_handle::WebHandle.id.

Alternative solutions:

  • Generate a random id that is 32 bits wide (is that enough entropy?)
  • Put the auto-incremental number on document / as some global JS variable instead, so that different winit "instances" could talk together and avoid using the same one?
  • Add extension API WindowBuilderExtWebSys::with_raw_handle_id or similar for specifying this id, in your application pass global ID down using std::env::args()?

A related issue is that raw-window-handle expects the canvas to be queried through the DOM using this id, which isn't always the case. For instance, when the window is created with an offscreen canvas, or when the canvas is within a Shadow DOM. The latter happened to one of my users in parasyte/pixels#309

I'm not entirely familiar with web-sys and wasm-bindgen, but HtmlCanvasElement implements From<JsValue>. Why isn't that used by WebWindowHandle? The only downside I can think of is that it adds a dependency on web-sys for everything that wants to use raw-window-handle in a web context (e.g. winit and wgpu). On the other hand, it also means that neither crate has to perform a carefully crafted (and obviously error-prone) dance with the DOM.

edit: I see most of the discussion around the current design was in this thread: rust-windowing/raw-window-handle#26 (comment)

I don't think this is really actionable by Winit, outside of assigning random UUIDs ... which I think is unnecessary for most use-cases and the issue described above can be dealt with WindowBuilder::with_canvas().

My stance on this is that this is a larger issue with raw-window-handle in that it doesn't take JsValue but instead relies on the data-raw-handle attribute, which isn't ideal.

Please reopen if WindowBuilder::with_canvas() isn't enough to fix this issue.