{"record":{"id":"90fdb8a728b6a8c6","repo":"a-b-street/abstreet","slug":"unable-to-cast-to-webglrenderingcontext-error","errorCode":null,"errorMessage":"unable to cast to WebGlRenderingContext. error: {:?}","messagePattern":"unable to cast to WebGlRenderingContext\\. error: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"widgetry/src/backend_glow_wasm.rs","lineNumber":102,"sourceCode":"            .get_context(\"webgl2\")\n            .map_err(|err| anyhow!(\"error getting context for WebGL 2.0: {:?}\", err))?;\n        let js_webgl2_context =\n            maybe_context.ok_or(anyhow!(\"Browser doesn't support WebGL 2.0\"))?;\n        let webgl2_context = js_webgl2_context\n            .dyn_into::<web_sys::WebGl2RenderingContext>()\n            .map_err(|err| anyhow!(\"unable to cast to WebGl2RenderingContext. error: {:?}\", err))?;\n        Ok(glow::Context::from_webgl2_context(webgl2_context))\n    }\n\n    fn webgl1_glow_context(canvas: &web_sys::HtmlCanvasElement) -> Result<glow::Context> {\n        let maybe_context: Option<_> = canvas\n            .get_context(\"webgl\")\n            .map_err(|err| anyhow!(\"error getting context for WebGL 1.0: {:?}\", err))?;\n        let js_webgl1_context =\n            maybe_context.ok_or(anyhow!(\"Browser doesn't support WebGL 1.0\"))?;\n        let webgl1_context = js_webgl1_context\n            .dyn_into::<web_sys::WebGlRenderingContext>()\n            .map_err(|err| anyhow!(\"unable to cast to WebGlRenderingContext. error: {:?}\", err))?;\n        Ok(glow::Context::from_webgl1_context(webgl1_context))\n    }\n\n    (\n        PrerenderInnards::new(gl, is_gl2, program, Some(WindowAdapter(winit_window))),\n        event_loop,\n    )\n}\n\nfn webgl2_program(gl: glow::Context) -> Result<(glow::Context, glow::Program)> {\n    let program = unsafe {\n        build_program(\n            &gl,\n            include_str!(\"../shaders/vertex_300.glsl\"),\n            include_str!(\"../shaders/fragment_300.glsl\"),\n        )?\n    };\n    Ok((gl, program))","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/a-b-street/abstreet/blob/0964f29315820c91b171b585eb51e300164e9197/widgetry/src/backend_glow_wasm.rs#L84-L120","documentation":"After obtaining a context object from get_context, the code dyn_into::<WebGlRenderingContext>() casts it to the WebGL 1.0 type. The cast failed, meaning the object returned by the browser is not a WebGL 1.0 rendering context (e.g. it is a WebGL2 context or another type). The library throws because glow was asked for a WebGL1-backed context but got a different JS object.","triggerScenarios":"canvas.get_context(\"webgl\") returned a non-null object whose runtime type is not WebGlRenderingContext — typically because a 'webgl2' context was previously created on the same canvas, or a nonstandard/incorrect object was returned.","commonSituations":"Mixing WebGL2 and WebGL1 initialization on the same canvas (browsers return the existing context type), custom canvas wrappers or frameworks (e.g. three.js) that already grabbed the context, stale/shimmed polyfills.","solutions":["Only create one context per canvas; do not request 'webgl2' before calling this","Create a fresh canvas element for the WebGL 1.0 context","Check that no graphics library (three.js, regl, etc.) initialized the canvas first","Log js_sys::Object::to_string on the returned value to see the actual type"],"exampleFix":"// before: reuse canvas that may already have a webgl2 context\nlet ctx = webgl1_glow_context(&existing_canvas)?;\n// after: use a dedicated canvas\nlet canvas = document.create_element(\"canvas\").unwrap().dyn_into::<web_sys::HtmlCanvasElement>().unwrap();\nlet ctx = webgl1_glow_context(&canvas)?;","handlingStrategy":"type-guard","validationCode":"let kind = canvas.get_context(\"webgl\").ok().flatten().map(|v| js_sys::Object::from(v).to_string().into());\nlog::info!(\"context type: {:?}\", kind); // should be [object WebGLRenderingContext]","typeGuard":"fn is_webgl1(val: &wasm_bindgen::JsValue) -> bool {\n    val.is_instance_of::<web_sys::WebGlRenderingContext>()\n}","tryCatchPattern":"match canvas.get_context(\"webgl\") {\n    Ok(Some(v)) if v.is_instance_of::<WebGlRenderingContext>() => { /* proceed */ }\n    Ok(Some(v)) => error!(\"unexpected context type: {:?}\", v.js_typeof()),\n    _ => error!(\"no webgl context\"),\n}","preventionTips":["Create exactly one context per canvas; never request webgl2 first","Initialize your graphics context before any other library touches the canvas","Use a dedicated, freshly created canvas element"],"tags":["wasm","webgl","type-cast","wasm-bindgen"],"backgroundTag":"incompatible-source-type","analyzedSha":"0964f29315820c91b171b585eb51e300164e9197","analyzedAt":"2026-09-13T18:02:03.421Z","contentChangedAt":"2026-09-13T18:02:03.421Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}