Skip to content

Web worker support #1518

@chemicstry

Description

@chemicstry

Since web workers support canvas via OffscreenCanvas, would it be possible to make winit run in a web worker?

Main issues are these:

  • OffscreenCanvas implements EventTarget so events are not a problem. Although they will have to be forwarded from the main canvas via whatever worker implementation is used. Probably not a winit concern.
  • OffscreenCanvas is different type than HtmlCanvasElement and can not be converted in rust. However, since javascript is an interpreted language, inserting OffscreenCanvas instead of HtmlCanvasElement from js side does not cause a problem. Although browser throws an error due to missing functions (style(), request_fullscreen(), set_attribute()), adding empty stubs fixes the issue and it works happily. This is a bit hacky, but other option would require replacing backend::RawCanvasType with enum, which would require a lot of rewrites.
  • A lot of functions use web_sys::window() and panic on web worker. Could they instead of panicking return default value? Main offenders are scale_factor() and is_fullscreen() which both are not essential. However, if they return default value would it make debugging harder for regular canvas?

Maybe there's no point in running winit in web worker, because the only usable feature without additional glue is the event loop. But I opened this for discussion, as this is relevant for projects that use winit and trying to support wasm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C - needs discussionDirection must be ironed outDS - webAffects the Web backend (WebAssembly/WASM)F - questionThere's no such thing as a stupid one

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions