icsharpcode/ILSpy · error · Error

context not found

Error message

context not found

What it means

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.

Source

Thrown at ICSharpCode.ILSpyX/MermaidDiagrammer/html/script.js:774

            const svg = document.querySelector('#output svg');
            if (!svg) {
                throw new Error('svg not found');
            }
            const box = svg.getBoundingClientRect();
            canvas.width = box.width;
            canvas.height = box.height;
            if (imagemodeselected === 'width') {
                const ratio = box.height / box.width;
                canvas.width = userimagesize;
                canvas.height = userimagesize * ratio;
            } else if (imagemodeselected === 'height') {
                const ratio = box.width / box.height;
                canvas.width = userimagesize * ratio;
                canvas.height = userimagesize;
            }
            const context = canvas.getContext('2d');
            if (!context) {
                throw new Error('context not found');
            }
            context.fillStyle = 'white';
            context.fillRect(0, 0, canvas.width, canvas.height);
            const image = new Image();
            image.onload = exporter(context, image);
            image.src = `data:image/svg+xml;base64,${getBase64SVG(svg, canvas.width, canvas.height)}`;
            event.stopPropagation();
            event.preventDefault();
        };

        const getSvgEl = () => {
            const svgEl = document.querySelector('#output svg').cloneNode(true);
            svgEl.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
            const fontAwesomeCdnUrl = Array.from(document.head.getElementsByTagName('link'))
                .map((l) => l.href)
                .find((h) => h.includes('font-awesome'));
            if (fontAwesomeCdnUrl == null) {
                return svgEl;

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Feature-detect the 2D context before drawing and fall back to a user-facing message or SVG-only download.
  2. Enable hardware acceleration / canvas in the host WebView/browser.
  3. If canvas is unavailable, offer the raw SVG (getBase64SVG) as the export instead of PNG.

Example fix

// before
const context = canvas.getContext('2d');
if (!context) {
    throw new Error('context not found');
}

// after
const context = canvas.getContext('2d');
if (!context) {
    // fall back: download the SVG directly instead of rasterizing
    simulateDownload(exportOptions.getFileName('svg'),
        'data:image/svg+xml;base64,' + getBase64SVG(svg, canvas.width, canvas.height));
    event.preventDefault();
    return;
}
Defensive patterns

Strategy: validation

Validate before calling

function canRasterize() {
    const c = document.createElement('canvas');
    return c.getContext('2d') instanceof CanvasRenderingContext2D;
}
if (!canRasterize()) {
    // offer SVG download, not PNG
}

Type guard

function supports2dContext() {
    return document.createElement('canvas').getContext('2d') instanceof CanvasRenderingContext2D;
}

Try / catch

try {
    exportImage(event, downloadImage, 'width', 1024);
} catch (e) {
    if (e.message === 'context not found') {
        alert('PNG export is unavailable in this environment; export SVG instead.');
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13). Data as JSON: /api/errors/917ff3c4d57b4a07. Report an issue: GitHub.