web: Add Instance::create_surface_from_canvas (fix #1837) #1932
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add specialized methods on web for creating a wgpu Surface directly
from a
web_sys::HTMLCanvasElement
andweb_sys::OffscreenCanvas
.Instance::create_surface_from_canvas
Instance::create_surface_from_offscreen_canvas
Connections
Fixes #1837.
Description
Currently
Instance::create_surface
requires aHasRawWindowHandle
. The web backend uses this handle to search for a canvas with adata-raw-handle
attribute.This is handy for full-screen demos or games, or quickly compiling winit apps to both desktop and web. However, this isn't ideal for apps that are part of a larger site, or usable as a JS library. For example, the user may want to instantiate wgpu without attaching the canvas to the DOM, or the canvas may be buried inside a shadow DOM. Additionally, it'd be nice to use
OffscreenCanvas
. See #1837 for more info.This adds web-specific
Instance::create_surface_from_canvas
andInstance::create_surface_from_offscreen_canvas
methods that directly acceptweb_sys::HtmlCanvasElement
andweb_sys::OffscreenCanvas
. These methods are only compiled for thewasm32
architecture. This was modeled after theInstance::create_surface_from_core_animation_layer
method for macOS.This does add
web_sys
types to the externally visible API. However,web_sys
is already a dependency of the project, and as WebGPU is a web-based API, wgpu should work directly with standard web types.