{"record":{"id":"d8f59fb07b5f0054","repo":"mozilla/pdf.js","slug":"invalid-canvas-size","errorCode":null,"errorMessage":"Invalid canvas size","messagePattern":"Invalid canvas size","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/display/canvas_factory.js","lineNumber":33,"sourceCode":"\nimport { unreachable } from \"../shared/util.js\";\n\nclass BaseCanvasFactory {\n  #enableHWA = false;\n\n  constructor({ enableHWA = false }) {\n    if (\n      (typeof PDFJSDev === \"undefined\" || PDFJSDev.test(\"TESTING\")) &&\n      this.constructor === BaseCanvasFactory\n    ) {\n      unreachable(\"Cannot initialize BaseCanvasFactory.\");\n    }\n    this.#enableHWA = enableHWA;\n  }\n\n  create(width, height) {\n    if (width <= 0 || height <= 0) {\n      throw new Error(\"Invalid canvas size\");\n    }\n    const canvas = this._createCanvas(width, height);\n    return {\n      canvas,\n      context: canvas.getContext(\"2d\", {\n        willReadFrequently: !this.#enableHWA,\n      }),\n    };\n  }\n\n  reset({ canvas }, width, height) {\n    if (!canvas) {\n      throw new Error(\"Canvas is not specified\");\n    }\n    if (width <= 0 || height <= 0) {\n      throw new Error(\"Invalid canvas size\");\n    }\n    canvas.width = width;","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/display/canvas_factory.js#L15-L51","documentation":"Thrown by BaseCanvasFactory.create(width, height) when width or height is <= 0. Canvases must have positive integer pixel dimensions; zero or negative sizes are meaningless and would produce invalid buffer allocation in the platform's canvas backend. PDF.js validates up front to fail fast.","triggerScenarios":"Calling canvasFactory.create(0, 100), create(-10, 50), or create(NaN, 100) (NaN <= 0 is false but create with NaN propagates downstream — the strict check catches the explicit zero/negative cases). Triggered when viewport dimensions round to zero at extreme zoom-out or for an empty page.","commonSituations":"Rendering at a scale where width/height compute to 0 (very small zoom); passing CSS pixel values that floored to 0; tests with placeholder dimensions.","solutions":["Clamp width/height to at least 1: Math.max(1, Math.round(viewport.width)).","Skip the render entirely when the page is not visible (size 0) instead of calling create.","Validate viewport scale before computing canvas size."],"exampleFix":"// before\nconst { canvas, context } = canvasFactory.create(viewport.width, viewport.height);\n\n// after\nconst w = Math.max(1, Math.round(viewport.width));\nconst h = Math.max(1, Math.round(viewport.height));\nconst { canvas, context } = canvasFactory.create(w, h);","handlingStrategy":"validation","validationCode":"function safeCreate(factory, w, h) {\n  w = Math.max(1, Math.round(w));\n  h = Math.max(1, Math.round(h));\n  return factory.create(w, h);\n}","typeGuard":"const isPositiveSize = (n): n is number => Number.isFinite(n) && n > 0;","tryCatchPattern":"null","preventionTips":["Compute canvas size from viewport.width/height at a supported scale floor.","Skip rendering when a page is collapsed/hidden.","Round and clamp dimensions at the boundary."],"tags":["canvas","validation","rendering"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}