{"record":{"id":"211f5a7ea518929a","repo":"zed-industries/zed","slug":"creating-text-offscreencanvas-error","errorCode":null,"errorMessage":"creating text OffscreenCanvas: {error:?}","messagePattern":"creating text OffscreenCanvas: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gpui_web/src/canvas_text.rs","lineNumber":31,"sourceCode":"\n#[derive(Clone, Copy, Debug)]\npub(crate) struct CanvasTextMetrics {\n    pub(crate) advance: f32,\n    pub(crate) left: f32,\n    pub(crate) right: f32,\n    pub(crate) ascent: f32,\n    pub(crate) descent: f32,\n}\n\nstruct TextCanvas {\n    canvas: OffscreenCanvas,\n    context: OffscreenCanvasRenderingContext2d,\n}\n\nimpl TextCanvas {\n    fn new() -> Result<Self> {\n        let canvas = OffscreenCanvas::new(1, 1)\n            .map_err(|error| anyhow!(\"creating text OffscreenCanvas: {error:?}\"))?;\n        let options = js_sys::Object::new();\n        // Every rasterized glyph is read back for upload into GPUI's atlas.\n        let assigned = js_sys::Reflect::set(\n            &options,\n            &JsValue::from_str(\"willReadFrequently\"),\n            &JsValue::TRUE,\n        )\n        .map_err(|error| anyhow!(\"setting Canvas readback option: {error:?}\"))?;\n        ensure!(assigned, \"Canvas readback option could not be set\");\n        let context = canvas\n            .get_context_with_context_options(\"2d\", &options)\n            .map_err(|error| anyhow!(\"getting OffscreenCanvas 2D context: {error:?}\"))?\n            .context(\"OffscreenCanvas 2D text rendering is unavailable\")?\n            .dyn_into::<OffscreenCanvasRenderingContext2d>()\n            .map_err(|error| anyhow!(\"unexpected OffscreenCanvas 2D context: {error:?}\"))?;\n        Ok(Self { canvas, context })\n    }\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/zed-industries/zed/blob/916fc2b8cb3a815cbef4a3b40e13081be72036b6/crates/gpui_web/src/canvas_text.rs#L13-L49","documentation":"TextCanvas::new constructs a browser OffscreenCanvas via web_sys. If the JS constructor throws (the JsValue error is captured with {error:?}), it is wrapped in this anyhow error. This typically means the browser/runtime does not support OffscreenCanvas or the width/height arguments were rejected.","triggerScenarios":"Calling TextCanvas::new in a browser without OffscreenCanvas support (e.g. older Safari, non-secure contexts in some engines) or with dimensions the engine refuses.","commonSituations":"Running a Zed web build in an outdated browser or a JS environment (older WebView, worker without OffscreenCanvas) where `new OffscreenCanvas` is undefined/throws.","solutions":["Use a browser that supports OffscreenCanvas (Chrome 69+, Firefox 105+, Safari 16.4+).","Guard with a feature check: `if js_sys::Reflect::has(&window, &\"OffscreenCanvas\".into())` before constructing, and fall back to a DOM canvas.","Verify width/height passed to TextCanvas::new are non-negative integers."],"exampleFix":"// before\nlet canvas = TextCanvas::new(width, height)?;\n// after\nif !has_offscreen_canvas_support() {\n    return Err(anyhow!(\"OffscreenCanvas unsupported; use DOM canvas fallback\"));\n}\nlet canvas = TextCanvas::new(width, height)?;","handlingStrategy":"fallback","validationCode":"function supportsOffscreenCanvas() {\n  return typeof OffscreenCanvas !== \"undefined\";\n}","typeGuard":"fn has_offscreen_canvas(window: &web_sys::Window) -> bool {\n    js_sys::Reflect::has(&window, &js_sys::Symbol::from(\"OffscreenCanvas\"))\n        .unwrap_or(false)\n}","tryCatchPattern":"match TextCanvas::new(width, height) {\n    Ok(canvas) => canvas,\n    Err(e) => {\n        log::warn!(\"OffscreenCanvas unavailable: {e:#}; falling back to DOM canvas\");\n        TextCanvas::from_dom_canvas(width, height)?\n    }\n};","preventionTips":["Feature-detect OffscreenCanvas at startup and choose a rendering backend once.","Test web builds on the oldest browser version you support.","Validate canvas dimensions are non-negative before construction."],"tags":["web","wasm","canvas","browser-compatibility"],"backgroundTag":"unsupported-platform","analyzedSha":"916fc2b8cb3a815cbef4a3b40e13081be72036b6","analyzedAt":"2026-09-19T19:09:50.599Z","contentChangedAt":"2026-09-19T19:09:50.599Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}