Skip to content

Conversation

@Toniman575
Copy link
Contributor

@Toniman575 Toniman575 commented Feb 28, 2023

Added methods to create Surface from a HtmlCanvasElement or OffscreenCanvas following wgpu's example https://github.com/gfx-rs/wgpu/blob/v0.15.1/wgpu/src/lib.rs#L1570-L1640

My use case required this to render in a web worker.

Edit: See this for a future alternative implementation rust-windowing/raw-window-handle#102

Comment on lines +238 to +246
/// Creates a new instance of this struct, using the provided [`HtmlCanvasElement`](web_sys::HtmlCanvasElement).
#[cfg(target_arch = "wasm32")]
pub fn from_canvas(canvas: web_sys::HtmlCanvasElement) -> Result<Self, SoftBufferError> {
let imple = SurfaceDispatch::Web(web::WebImpl::from_canvas(canvas)?);

Ok(Self {
surface_impl: Box::new(imple),
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general pattern in winit is to make this an extension trait on the existing type. Could we do that here?

/// Creates a new instance of this struct, using the provided [`HtmlCanvasElement`](web_sys::OffscreenCanvas).
#[cfg(target_arch = "wasm32")]
pub fn from_offscreen_canvas(
offscreen_canvas: web_sys::OffscreenCanvas,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather avoid exposing a public dependency on web-sys if at all possible (in case we decide to move to stdweb or something). Is there a way of representing these types without web-sys?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know of a way to avoid that. Even Winit does it.

As far as I know stdweb is dead, but if you know of an alternative to the wasm-bindgen ecosystem I would be very interested!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention, it is actually possible to represent these types without web-sys: using JsValue.
But this would still require wasm-bindgen as a public dependency, which doesn't really help if we want to change to an alternative later.

@Toniman575
Copy link
Contributor Author

Closing this PR in favor of #76

@Toniman575 Toniman575 closed this Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants