gfx-rs/wgpu · error
Failed to downcast to canvas type
Error message
Failed to downcast to canvas type
What it means
Panic in the GLES/web create_surface: dyn_into::<HtmlCanvasElement>() on the queried element failed — the node is not a canvas, so the downcast to the canvas type is impossible. The faulty input is the non-canvas DOM element.
Source
Thrown at wgpu-hal/src/gles/web.rs:159
} else {
Vec::new()
}
}
unsafe fn create_surface(
&self,
_display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<Surface, crate::InstanceError> {
let canvas: web_sys::HtmlCanvasElement = match window_handle {
raw_window_handle::RawWindowHandle::Web(handle) => web_sys::window()
.and_then(|win| win.document())
.expect("Cannot get document")
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
.expect("Cannot query for canvas")
.expect("Canvas is not found")
.dyn_into()
.expect("Failed to downcast to canvas type"),
raw_window_handle::RawWindowHandle::WebCanvas(handle) => {
let value: &JsValue = unsafe { handle.obj.cast().as_ref() };
value.clone().unchecked_into()
}
raw_window_handle::RawWindowHandle::WebOffscreenCanvas(handle) => {
let value: &JsValue = unsafe { handle.obj.cast().as_ref() };
let canvas: web_sys::OffscreenCanvas = value.clone().unchecked_into();
return self.create_surface_from_offscreen_canvas(canvas);
}
_ => {
return Err(crate::InstanceError::new(format!(
"window handle {window_handle:?} is not a web handle"
)))
}
};
self.create_surface_from_canvas(canvas)View on GitHub (pinned to 3e11ff59bf)
Solutions
- Ensure the queried selector matches a <canvas> element
- Bind the raw handle to the correct canvas in the windowing layer
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at wgpu-hal/src/gles/web.rs:159 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03).
Data as JSON: /api/errors/27c970b78ad175d0.
Report an issue: GitHub.