{"record":{"id":"c0b01b8f1081f8b4","repo":"siyuan-note/siyuan","slug":"unable-to-create-a-canvas-context-for-the-pdf-rect","errorCode":null,"errorMessage":"Unable to create a canvas context for the PDF rectangle annotation","messagePattern":"Unable to create a canvas context for the PDF rectangle annotation","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/asset/anno.ts","lineNumber":1131,"sourceCode":"    if (captureScale <= 0) {\n        throw new Error(\"PDF rectangle annotation has invalid coordinates\");\n    }\n\n    const viewport = pdfPage.getViewport({scale: captureScale, rotation: totalRotation});\n    const captureBounds = getCaptureCanvasBounds(viewport.convertToViewportRectangle(position));\n    const captureViewport = pdfPage.getViewport({\n        scale: captureScale,\n        rotation: totalRotation,\n        offsetX: -captureBounds.left,\n        offsetY: -captureBounds.top,\n    });\n    const captureCanvas = document.createElement(\"canvas\");\n    captureCanvas.width = captureBounds.width;\n    captureCanvas.height = captureBounds.height;\n\n    const captureCtx = captureCanvas.getContext(\"2d\");\n    if (!captureCtx) {\n        throw new Error(\"Unable to create a canvas context for the PDF rectangle annotation\");\n    }\n    await pdfPage.render({\n        canvasContext: captureCtx,\n        viewport: captureViewport,\n    }).promise;\n\n    const displayViewport = pdfPage.getViewport({scale: PDF_RECT_DISPLAY_SCALE, rotation: totalRotation});\n    const displayWidth = Math.min(\n        getCaptureDisplayWidth(displayViewport.convertToViewportRectangle(position)),\n        captureBounds.width,\n    );\n    const blob = await new Promise<Blob>((resolve, reject) => {\n        captureCanvas.toBlob((result) => {\n            if (result) {\n                resolve(result);\n            } else {\n                reject(new Error(\"Unable to encode the PDF rectangle annotation\"));\n            }","sourceCodeStart":1113,"sourceCodeEnd":1149,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/asset/anno.ts#L1113-L1149","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Capture a smaller rectangle, or reduce PDF_RECT_CAPTURE_SCALE","Re-enable GPU/hardware acceleration (or remove --disable-gpu) and restart the app","Check that capture bounds stay within the browser's max canvas size; clamp them","Retry after restarting the app to recover a crashed GPU process"],"exampleFix":"// before\nconst captureCtx = captureCanvas.getContext(\"2d\");\nif (!captureCtx) {\n    throw new Error(\"Unable to create a canvas context for the PDF rectangle annotation\");\n}\n// after: retry once with a reduced scale\nlet captureCtx = captureCanvas.getContext(\"2d\");\nif (!captureCtx && captureScale > PDF_RECT_CAPTURE_SCALE) {\n    // rebuild the canvas at a lower scale and try again before giving up\n}","handlingStrategy":"fallback","validationCode":"const maxDim = 16384;\nif (captureBounds.width > maxDim || captureBounds.height > maxDim) { throw new Error(\"Capture area too large\"); }","typeGuard":"const canCreate2DContext = (): boolean => !!document.createElement(\"canvas\").getContext(\"2d\");","tryCatchPattern":"try {\n    await getRectImgData(pdfObj, pageNumber, position);\n} catch (e) {\n    showMessage(e.message); // suggest lowering capture scale or re-enabling GPU acceleration\n}","preventionTips":["Clamp capture canvas dimensions below browser maxima","Avoid disabling GPU acceleration when using PDF annotations","Reduce PDF_RECT_CAPTURE_SCALE on high-DPI displays"],"tags":["pdf","canvas","gpu"],"backgroundTag":"internal-invariant-violation","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}