gfx-rs/wgpu · error

Cannot query for canvas

Error message

Cannot query for canvas

What it means

Panic in the GLES/web create_surface: query_selector for the canvas matching the raw handle's data-raw-handle attribute returned None, so the canvas element tied to the window handle could not be located. The faulty input is the canvas id/handle association.

Source

Thrown at wgpu-hal/src/gles/web.rs:156

            }
            .into_iter()
            .collect()
        } 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"
                )))
            }

View on GitHub (pinned to 3e11ff59bf)

Solutions

  1. Ensure the canvas element with the matching data-raw-handle attribute exists in the document
  2. Register the canvas with the windowing system before creating the surface
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at wgpu-hal/src/gles/web.rs:156 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/110f6360edcb3569. Report an issue: GitHub.