emilk/egui · error

Failed to query about WebGL2 context

Error message

Failed to query about WebGL2 context

What it means

init_webgl1 calls canvas.get_context("webgl") and unwraps with a (misleadingly worded) 'Failed to query about WebGL2 context' message. get_context only returns Err when the browser fails to query the context creation path itself (JS exception path), not when WebGL is merely unsupported — that returns Ok(None) which the subsequent `?` handles.

Source

Thrown at crates/eframe/src/web/web_painter_glow.rs:123

        // Trying WebGl2 first
        WebGlContextOption::BestFirst => init_webgl2(canvas).or_else(|| init_webgl1(canvas)),
        // Trying WebGl1 first (useful for testing).
        WebGlContextOption::CompatibilityFirst => {
            init_webgl1(canvas).or_else(|| init_webgl2(canvas))
        }
    };

    if let Some(result) = result {
        Ok(result)
    } else {
        Err("WebGL isn't supported".into())
    }
}

fn init_webgl1(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static str)> {
    let gl1_ctx = canvas
        .get_context("webgl")
        .expect("Failed to query about WebGL2 context");

    let gl1_ctx = gl1_ctx?;
    log::debug!("WebGL1 selected.");

    let gl1_ctx = gl1_ctx
        .dyn_into::<web_sys::WebGlRenderingContext>()
        .unwrap();

    let shader_prefix = if webgl1_requires_brightening(&gl1_ctx) {
        log::debug!("Enabling webkitGTK brightening workaround.");
        "#define APPLY_BRIGHTENING_GAMMA"
    } else {
        ""
    };

    let gl = glow::Context::from_webgl1_context(gl1_ctx);

    Some((gl, shader_prefix))

View on GitHub (pinned to 441971a776)

Solutions

  1. Ensure the canvas is attached and the browser supports/permits WebGL (enable hardware acceleration).
  2. Replace .expect with graceful handling: match on the Result and fall back to WebGL2 or show an error message.
  3. Check browser settings (chrome://gpu, --disable-gpu flags, corporate policies) that block context creation.

Example fix

// before
let gl1_ctx = canvas.get_context("webgl").expect("Failed to query about WebGL2 context");
// after
let gl1_ctx = canvas.get_context("webgl").map_err(|e| log::error!("WebGL query failed: {e:?}")).ok().flatten()?;
Defensive patterns

Strategy: try-catch

Validate before calling

let can_query = canvas.is_connected(); // plus feature-detect: js_sys::eval("!!document.createElement('canvas').getContext('webgl')")

Type guard

fn webgl_queryable(canvas: &HtmlCanvasElement) -> bool { canvas.get_context("webgl").is_ok() }

Try / catch

// Replace expect: canvas.get_context("webgl").map_err(log).ok().flatten() and fall back to WebGL2 or an error UI.

Prevention

When it happens

Trigger: Calling init_glow_context_from_canvas on a canvas whose getContext('webgl') throws or cannot be queried — e.g. a detached/invalid canvas, a canvas in a weird state, or a browser where the WebGL query path raises an exception.

Common situations: Embedded webviews with GPU access disabled; hardware acceleration blocked by browser flags or policy; canvases created before document attachment in some engines; corrupted GPU driver state causing context queries to fail.

Related errors


AI-assisted analysis of emilk/egui@441971a776 (2026-09-12). Data as JSON: /api/errors/a8f25a652dc5cad6. Report an issue: GitHub.