siyuan-note/siyuan · error · Error

Unable to create a canvas context for the PDF rectangle…

Error message

Unable to create a canvas context for the PDF rectangle annotation

What it means

The rectangle-annotation capture creates an offscreen canvas and obtains a 2D rendering context. If `getContext("2d")` returns null, "Unable to create a canvas context for the PDF rectangle annotation" is thrown. A null context means the browser could not allocate the 2D drawing backend for the canvas.

Solutions

  1. Capture a smaller rectangle, or reduce PDF_RECT_CAPTURE_SCALE
  2. Re-enable GPU/hardware acceleration (or remove --disable-gpu) and restart the app
  3. Check that capture bounds stay within the browser's max canvas size; clamp them
  4. Retry after restarting the app to recover a crashed GPU process

Example fix

// before
const captureCtx = captureCanvas.getContext("2d");
if (!captureCtx) {
    throw new Error("Unable to create a canvas context for the PDF rectangle annotation");
}
// after: retry once with a reduced scale
let captureCtx = captureCanvas.getContext("2d");
if (!captureCtx && captureScale > PDF_RECT_CAPTURE_SCALE) {
    // rebuild the canvas at a lower scale and try again before giving up
}
Defensive patterns

Strategy: fallback

Validate before calling

const maxDim = 16384;
if (captureBounds.width > maxDim || captureBounds.height > maxDim) { throw new Error("Capture area too large"); }

Type guard

const canCreate2DContext = (): boolean => !!document.createElement("canvas").getContext("2d");

Try / catch

try {
    await getRectImgData(pdfObj, pageNumber, position);
} catch (e) {
    showMessage(e.message); // suggest lowering capture scale or re-enabling GPU acceleration
}

Prevention

When it happens

Trigger: Canvas dimensions exceed the browser's maximum canvas area (captureBounds too large), GPU/hardware acceleration is disabled or crashed, or the environment has no canvas 2D support at all.

Common situations: Capturing a very large rectangle on a high-DPI display where scaled canvas size exceeds limits (e.g. Chrome's ~16384px or total-area caps); running with `--disable-gpu`; embedded environments without full canvas support.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/c0b01b8f1081f8b4. Report an issue: GitHub.

Appendix: source

Thrown at app/src/asset/anno.ts:1131

    if (captureScale <= 0) {
        throw new Error("PDF rectangle annotation has invalid coordinates");
    }

    const viewport = pdfPage.getViewport({scale: captureScale, rotation: totalRotation});
    const captureBounds = getCaptureCanvasBounds(viewport.convertToViewportRectangle(position));
    const captureViewport = pdfPage.getViewport({
        scale: captureScale,
        rotation: totalRotation,
        offsetX: -captureBounds.left,
        offsetY: -captureBounds.top,
    });
    const captureCanvas = document.createElement("canvas");
    captureCanvas.width = captureBounds.width;
    captureCanvas.height = captureBounds.height;

    const captureCtx = captureCanvas.getContext("2d");
    if (!captureCtx) {
        throw new Error("Unable to create a canvas context for the PDF rectangle annotation");
    }
    await pdfPage.render({
        canvasContext: captureCtx,
        viewport: captureViewport,
    }).promise;

    const displayViewport = pdfPage.getViewport({scale: PDF_RECT_DISPLAY_SCALE, rotation: totalRotation});
    const displayWidth = Math.min(
        getCaptureDisplayWidth(displayViewport.convertToViewportRectangle(position)),
        captureBounds.width,
    );
    const blob = await new Promise<Blob>((resolve, reject) => {
        captureCanvas.toBlob((result) => {
            if (result) {
                resolve(result);
            } else {
                reject(new Error("Unable to encode the PDF rectangle annotation"));
            }

View on GitHub (pinned to 9f775e8a12)