zed-industries/zed · error
creating text OffscreenCanvas
Error message
creating text OffscreenCanvas: {error:?} What it means
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.
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.
Example fix
// before
let canvas = TextCanvas::new(width, height)?;
// after
if !has_offscreen_canvas_support() {
return Err(anyhow!("OffscreenCanvas unsupported; use DOM canvas fallback"));
}
let canvas = TextCanvas::new(width, height)?; Defensive patterns
Strategy: fallback
Validate before calling
function supportsOffscreenCanvas() {
return typeof OffscreenCanvas !== "undefined";
} Type guard
fn has_offscreen_canvas(window: &web_sys::Window) -> bool {
js_sys::Reflect::has(&window, &js_sys::Symbol::from("OffscreenCanvas"))
.unwrap_or(false)
} Try / catch
match TextCanvas::new(width, height) {
Ok(canvas) => canvas,
Err(e) => {
log::warn!("OffscreenCanvas unavailable: {e:#}; falling back to DOM canvas");
TextCanvas::from_dom_canvas(width, height)?
}
}; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Running a Zed web build in an outdated browser or a JS environment (older WebView, worker without OffscreenCanvas) where `new OffscreenCanvas` is undefined/throws.
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
- getting OffscreenCanvas 2D context
- Canvas text raster returned
- setting Canvas readback option
- unexpected OffscreenCanvas 2D context
- Canvas readback option could not be set
AI-assisted analysis of zed-industries/zed@916fc2b8cb (2026-09-19).
Data as JSON: /api/errors/211f5a7ea518929a.
Report an issue: GitHub.
Appendix: source
Thrown at crates/gpui_web/src/canvas_text.rs:31
#[derive(Clone, Copy, Debug)]
pub(crate) struct CanvasTextMetrics {
pub(crate) advance: f32,
pub(crate) left: f32,
pub(crate) right: f32,
pub(crate) ascent: f32,
pub(crate) descent: f32,
}
struct TextCanvas {
canvas: OffscreenCanvas,
context: OffscreenCanvasRenderingContext2d,
}
impl TextCanvas {
fn new() -> Result<Self> {
let canvas = OffscreenCanvas::new(1, 1)
.map_err(|error| anyhow!("creating text OffscreenCanvas: {error:?}"))?;
let options = js_sys::Object::new();
// Every rasterized glyph is read back for upload into GPUI's atlas.
let assigned = js_sys::Reflect::set(
&options,
&JsValue::from_str("willReadFrequently"),
&JsValue::TRUE,
)
.map_err(|error| anyhow!("setting Canvas readback option: {error:?}"))?;
ensure!(assigned, "Canvas readback option could not be set");
let context = canvas
.get_context_with_context_options("2d", &options)
.map_err(|error| anyhow!("getting OffscreenCanvas 2D context: {error:?}"))?
.context("OffscreenCanvas 2D text rendering is unavailable")?
.dyn_into::<OffscreenCanvasRenderingContext2d>()
.map_err(|error| anyhow!("unexpected OffscreenCanvas 2D context: {error:?}"))?;
Ok(Self { canvas, context })
}
View on GitHub (pinned to 916fc2b8cb)