{"record":{"id":"917ff3c4d57b4a07","repo":"icsharpcode/ILSpy","slug":"context-not-found","errorCode":null,"errorMessage":"context not found","messagePattern":"context not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ICSharpCode.ILSpyX/MermaidDiagrammer/html/script.js","lineNumber":774,"sourceCode":"            const svg = document.querySelector('#output svg');\n            if (!svg) {\n                throw new Error('svg not found');\n            }\n            const box = svg.getBoundingClientRect();\n            canvas.width = box.width;\n            canvas.height = box.height;\n            if (imagemodeselected === 'width') {\n                const ratio = box.height / box.width;\n                canvas.width = userimagesize;\n                canvas.height = userimagesize * ratio;\n            } else if (imagemodeselected === 'height') {\n                const ratio = box.width / box.height;\n                canvas.width = userimagesize * ratio;\n                canvas.height = userimagesize;\n            }\n            const context = canvas.getContext('2d');\n            if (!context) {\n                throw new Error('context not found');\n            }\n            context.fillStyle = 'white';\n            context.fillRect(0, 0, canvas.width, canvas.height);\n            const image = new Image();\n            image.onload = exporter(context, image);\n            image.src = `data:image/svg+xml;base64,${getBase64SVG(svg, canvas.width, canvas.height)}`;\n            event.stopPropagation();\n            event.preventDefault();\n        };\n\n        const getSvgEl = () => {\n            const svgEl = document.querySelector('#output svg').cloneNode(true);\n            svgEl.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');\n            const fontAwesomeCdnUrl = Array.from(document.head.getElementsByTagName('link'))\n                .map((l) => l.href)\n                .find((h) => h.includes('font-awesome'));\n            if (fontAwesomeCdnUrl == null) {\n                return svgEl;","sourceCodeStart":756,"sourceCodeEnd":792,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.ILSpyX/MermaidDiagrammer/html/script.js#L756-L792","documentation":"Thrown by the Mermaid 'exportImage' helper (script.js) when canvas.getContext('2d') returns null. The CanvasRenderingContext2D is unavailable, so the exporter cannot paint the SVG onto the canvas. This is an environment/feature-availability failure, not a data problem.","triggerScenarios":"exportImage runs in a host where the 2D canvas context is unavailable: a WebView/browser with canvas disabled, a hardened/no-GPU environment, or a headless automation context without full canvas support.","commonSituations":"Embedded WebView (Avalonia WebView) with hardware acceleration off; browser privacy/lockdown policies disabling canvas; very old engines; running the page under a test runner that stubs out canvas.","solutions":["Feature-detect the 2D context before drawing and fall back to a user-facing message or SVG-only download.","Enable hardware acceleration / canvas in the host WebView/browser.","If canvas is unavailable, offer the raw SVG (getBase64SVG) as the export instead of PNG."],"exampleFix":"// before\nconst context = canvas.getContext('2d');\nif (!context) {\n    throw new Error('context not found');\n}\n\n// after\nconst context = canvas.getContext('2d');\nif (!context) {\n    // fall back: download the SVG directly instead of rasterizing\n    simulateDownload(exportOptions.getFileName('svg'),\n        'data:image/svg+xml;base64,' + getBase64SVG(svg, canvas.width, canvas.height));\n    event.preventDefault();\n    return;\n}","handlingStrategy":"validation","validationCode":"function canRasterize() {\n    const c = document.createElement('canvas');\n    return c.getContext('2d') instanceof CanvasRenderingContext2D;\n}\nif (!canRasterize()) {\n    // offer SVG download, not PNG\n}","typeGuard":"function supports2dContext() {\n    return document.createElement('canvas').getContext('2d') instanceof CanvasRenderingContext2D;\n}","tryCatchPattern":"try {\n    exportImage(event, downloadImage, 'width', 1024);\n} catch (e) {\n    if (e.message === 'context not found') {\n        alert('PNG export is unavailable in this environment; export SVG instead.');\n    } else {\n        throw e;\n    }\n}","preventionTips":["Detect canvas 2D support at app startup and adjust the export menu accordingly.","Keep an SVG-only export path as a guaranteed fallback."],"tags":["javascript","canvas","ui","export","browser-compat"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}