gfx-rs/wgpu · error

canvas context is not a WebGl2RenderingContext

Error message

canvas context is not a WebGl2RenderingContext

What it means

Panic in the GLES/web backend: the object returned by canvas.getContext('webgl2') is not a WebGl2RenderingContext, so it cannot be used to build a glow context. The faulty input is the canvas's context object (wrong type or backend mismatch).

Source

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

                return Err(crate::InstanceError::new(String::from(concat!(
                    "canvas.getContext() returned null; ",
                    "webgl2 not available or canvas already in use"
                ))));
            }
            Err(js_error) => {
                // <https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext>
                // A thrown exception indicates misuse of the canvas state.
                return Err(crate::InstanceError::new(format!(
                    "canvas.getContext() threw exception {js_error:?}",
                )));
            }
        };

        // Not returning this error because it is a type error that shouldn't happen unless
        // the browser, JS builtin objects, or wasm bindings are misbehaving somehow.
        let webgl2_context: web_sys::WebGl2RenderingContext = context_object
            .dyn_into()
            .expect("canvas context is not a WebGl2RenderingContext");

        Ok(Surface {
            canvas,
            webgl2_context,
            srgb_present_program: Mutex::new(None),
            swapchain: RwLock::new(None),
            texture: Mutex::new(None),
            presentable: true,
        })
    }

    fn create_context_options() -> js_sys::Object {
        let context_options = js_sys::Object::new();
        js_sys::Reflect::set(&context_options, &"antialias".into(), &JsValue::FALSE)
            .expect("Cannot create context options");
        context_options
    }
}

View on GitHub (pinned to 3e11ff59bf)

Solutions

  1. Ensure the canvas is fresh (getContext not already called with another type) and webgl2 is available
  2. Check the context type before handing the canvas to wgpu
Defensive patterns

Strategy: type-guard

When it happens

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