Help with WGPU and HTML Canvas Integration

Hi everyone,

I'm writing a project in Rust where I need to draw directly on an HTML canvas with wgpu. I'm stuck on error with that code:

let surface = instance.create_surface_from_canvas(&canvas).unwrap();
is not having mercy on me, and I just can't seem to correct it.

I've also tried a window-based approach followed in the wgpu hello_triangle example, but I got another error:

winit::window::WindowBuilder::new();
The error message is:
failed to resolve: could not find WindowBuilder in window
But it should be!

I’ve tried various solutions, but nothing seems to work. Any advice or guidance would be appreciated!

What error is create_surface_from_camvas giving you?

error[E0599]: no method named create_surface_from_canvas found for struct wgpu::Instance in the current scope
--> src\lib.rs:41:28
|
41 | let surface = instance.create_surface_from_canvas(&canvas);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: there is a method create_surface with a similar name
|
41 | let surface = instance.create_surface(&canvas);
| ~~~~~~~~~~~~~~

but this method is public according the documentation in repo

What you're looking at is an api from our internal abstraction layer wgpu-hal. Quite a few versions ago we did have individual entry points, which is what that changelog hit was referring to.

You can pass a HtmlCanvasElement to SurfaceTarget and pass that to Instance::create_surface. Note you need to switch docs.rs's platform to wasm to see the canvas and offscreen canvas variant.

1 Like

Yay! That's works! Thank you much and have a good day!