{"record":{"id":"9f5c62f0e596becb","repo":"iced-rs/iced","slug":"get-window-canvas","errorCode":null,"errorMessage":"Get window canvas","messagePattern":"Get window canvas","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"winit/src/lib.rs","lineNumber":348,"sourceCode":"                                let mut window_attributes = window_attributes;\n\n                                #[cfg(target_os = \"macos\")]\n                                let position = window_attributes.position.take();\n\n                                let window = event_loop\n                                    .create_window(window_attributes)\n                                    .expect(\"Create window\");\n\n                                #[cfg(target_os = \"macos\")]\n                                if let Some(position) = position {\n                                    window.set_outer_position(position);\n                                }\n\n                                #[cfg(target_arch = \"wasm32\")]\n                                {\n                                    use winit::platform::web::WindowExtWebSys;\n\n                                    let canvas = window.canvas().expect(\"Get window canvas\");\n\n                                    let _ = canvas.set_attribute(\n                                        \"style\",\n                                        \"display: block; width: 100%; height: 100%\",\n                                    );\n\n                                    let window = web_sys::window().unwrap();\n                                    let document = window.document().unwrap();\n                                    let body = document.body().unwrap();\n\n                                    let target = target.and_then(|target| {\n                                        body.query_selector(&format!(\"#{target}\"))\n                                            .ok()\n                                            .unwrap_or(None)\n                                    });\n\n                                    match target {\n                                        Some(node) => {","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/iced-rs/iced/blob/d146509d893945de5c304cfaf4301335eee278bd/winit/src/lib.rs#L330-L366","documentation":"On wasm32 targets, the winit runtime calls window.canvas() (WindowExtWebSys) and expects it to return Some. winit's web backend returns None when the browser window has no valid canvas element to associate with the WebGL/WebGPU context (e.g. the Window was created from a closed/invalid context or the DOM state is unexpected).","triggerScenarios":"Running an iced wasm app where event_loop.create_window succeeds but window.canvas() returns None - typically when the HTML page lacks the expected canvas setup or the window was obtained in a worker/off-screen context.","commonSituations":"Serving the app from an HTML template missing the <canvas> or incompatible winit/iced web entry point; opening the page inside an iframe/webview with unusual document.body state; running the wasm in a web worker without DOM access.","solutions":["Ensure the wasm is started on the main thread with DOM access (no web worker entry point).","Check the index.html template matches the winit/iced version's expected structure (a <canvas> or body element must exist).","Verify document.body exists at startup time - wait for DOMContentLoaded before running the app.","Align iced/winit/web-sys versions; the web backend's canvas handling changed across releases.","Test in a plain browser page first to rule out webview/iframe quirks."],"exampleFix":"// before (html)\n<div id=\"app\"></div>\n// after (html)\n<body><canvas id=\"iced-canvas\"></canvas></body>","handlingStrategy":"validation","validationCode":"// ensure a canvas exists before starting the wasm app\nif (!document.querySelector(\"canvas\")) {\n  const c = document.createElement(\"canvas\");\n  document.body.appendChild(c);\n}","typeGuard":"function hasCanvas(): boolean {\n  return typeof document !== \"undefined\"\n    && document.body !== null\n    && document.querySelector(\"canvas\") !== null;\n}","tryCatchPattern":null,"preventionTips":["Start the wasm app on the main thread with DOM access only.","Keep the index.html template matching your iced/winit web backend version.","Wait for DOMContentLoaded/load before mounting.","Test outside webviews/iframes first to isolate environment quirks."],"tags":["wasm","web","winit","canvas","panic"],"backgroundTag":"entity-not-found","analyzedSha":"d146509d893945de5c304cfaf4301335eee278bd","analyzedAt":"2026-09-11T16:54:14.229Z","contentChangedAt":"2026-09-11T16:54:14.229Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}