a-b-street/abstreet · error · anyhow::Error

error getting context for WebGL 2.0

Error message

error getting context for WebGL 2.0: {:?}

What it means

webgl2_glow_context requests a "webgl2" context from the canvas via get_context. If the browser API call itself errors (a JsValue failure rather than simply returning null), it is wrapped as this error. It is distinct from the browser merely not supporting WebGL 2 (that produces a separate message).

Solutions

  1. Enable WebGL (and specifically WebGL 2) in browser settings or privacy extensions.
  2. Ensure the canvas isn't already acquired with another context type (e.g., 2d) before requesting webgl2.
  3. Rely on setup's fallback: it should try webgl2 then webgl1; verify the fallback path executes.
  4. Test in a different browser/driver to isolate environment-specific getContext failures.

Example fix

// before
let maybe_context: Option<_> = canvas
    .get_context("webgl2")
    .map_err(|err| anyhow!("error getting context for WebGL 2.0: {:?}", err))?;
// after: caller handles fallback
let ctx = match webgl2_glow_context(canvas) {
    Ok(c) => c,
    Err(_) => webgl1_glow_context(canvas)?, // graceful degradation
};
Defensive patterns

Strategy: fallback

Validate before calling

// Feature-detect WebGL 2 before requesting it
const hasWebGL2 = (() => {
  const c = document.createElement('canvas');
  return !!(c.getContext('webgl2'));
})();

Try / catch

// Try webgl2, then webgl1, then surface a clear message
let ctx = webgl2_glow_context(canvas)
    .or_else(|_| webgl1_glow_context(canvas))
    .map_err(|e| anyhow!("No usable WebGL context: {}", e))?;

Prevention

When it happens

Trigger: setup() on wasm calls webgl2_glow_context and canvas.get_context("webgl2") rejects — e.g., cross-origin tainted canvases, privacy settings blocking WebGL, or JS exceptions in the binding call.

Common situations: Browsers with WebGL disabled via flags or privacy extensions; canvas already bound to a different context type; corporate policies or headless environments blocking GPU access.

Understand the failure class

Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.

Related errors


AI-assisted analysis of a-b-street/abstreet@0964f29315 (2026-09-13). Data as JSON: /api/errors/75bac1df90bf9c74. Report an issue: GitHub.

Appendix: source

Thrown at widgetry/src/backend_glow_wasm.rs:85

    let mut is_gl2 = true;
    let (gl, program) = webgl2_glow_context(&canvas)
        .and_then(|gl| webgl2_program(gl))
        .or_else(|err| {
            warn!(
                "failed to build WebGL 2.0 context with error: \"{}\". Trying WebGL 1.0 instead...",
                err
            );
            webgl1_glow_context(&canvas).and_then(|gl| {
                is_gl2 = false;
                webgl1_program(gl)
            })
        })
        .unwrap();

    fn webgl2_glow_context(canvas: &web_sys::HtmlCanvasElement) -> Result<glow::Context> {
        let maybe_context: Option<_> = canvas
            .get_context("webgl2")
            .map_err(|err| anyhow!("error getting context for WebGL 2.0: {:?}", err))?;
        let js_webgl2_context =
            maybe_context.ok_or(anyhow!("Browser doesn't support WebGL 2.0"))?;
        let webgl2_context = js_webgl2_context
            .dyn_into::<web_sys::WebGl2RenderingContext>()
            .map_err(|err| anyhow!("unable to cast to WebGl2RenderingContext. error: {:?}", err))?;
        Ok(glow::Context::from_webgl2_context(webgl2_context))
    }

    fn webgl1_glow_context(canvas: &web_sys::HtmlCanvasElement) -> Result<glow::Context> {
        let maybe_context: Option<_> = canvas
            .get_context("webgl")
            .map_err(|err| anyhow!("error getting context for WebGL 1.0: {:?}", err))?;
        let js_webgl1_context =
            maybe_context.ok_or(anyhow!("Browser doesn't support WebGL 1.0"))?;
        let webgl1_context = js_webgl1_context
            .dyn_into::<web_sys::WebGlRenderingContext>()
            .map_err(|err| anyhow!("unable to cast to WebGlRenderingContext. error: {:?}", err))?;
        Ok(glow::Context::from_webgl1_context(webgl1_context))

View on GitHub (pinned to 0964f29315)