{"record":{"id":"a00c818accee5eef","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-clearr","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.clearRect","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.clearRect","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":1147,"sourceCode":"  /**\n   * Clears the specified pixels within a given rectangle\n   *\n   * @name clearRect\n   * @function\n   * @param x {Number} The x-coordinate of the upper-left corner of the rectangle\n   * @param y {Number} The y-coordinate of the upper-left corner of the rectangle\n   * @param w {Number} The width of the rectangle to clear, in pixels\n   * @param h {Number} The height of the rectangle to clear, in pixels\n   * @description We cannot clear PDF commands that were already written to PDF, so we use white instead. <br />\n   * As a special case, read a special flag (ignoreClearRect) and do nothing if it is set.\n   * This results in all calls to clearRect() to do nothing, and keep the canvas transparent.\n   * This flag is stored in the save/restore context and is managed the same way as other drawing states.\n   *\n   */\n  Context2D.prototype.clearRect = function(x, y, w, h) {\n    if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {\n      console.error(\"jsPDF.context2d.clearRect: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.clearRect\");\n    }\n    if (this.ignoreClearRect) {\n      return;\n    }\n\n    this.fillStyle = \"#ffffff\";\n    this.fillRect(x, y, w, h);\n  };\n\n  /**\n   * Saves the state of the current context\n   *\n   * @name save\n   * @function\n   */\n  Context2D.prototype.save = function(doStackPush) {\n    doStackPush = typeof doStackPush === \"boolean\" ? doStackPush : true;\n    var tmpPageNumber = this.pdf.internal.getCurrentPageInfo().pageNumber;","sourceCodeStart":1129,"sourceCodeEnd":1165,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L1129-L1165","documentation":"clearRect validates x/y/w/h with isNaN first and throws before checking the ignoreClearRect flag or painting the white fillRect. Since jsPDF cannot erase already-emitted PDF commands, clearRect is implemented as a white fillRect; the only way to make it a no-op is the ignoreClearRect context flag, which is still bypassed by the NaN guard.","triggerScenarios":"ctx.clearRect(x, y, w, h) with any undefined/NaN/missing argument; clearing regions computed from missing layout fields.","commonSituations":"Porting canvas code that clears regions each frame; rendering a multi-pass layout where intermediate clear dimensions are not yet known; treating clearRect as optional-safe when dimensions are absent.","solutions":["Coerce dimensions to finite numbers and guard before calling.","Set ctx.ignoreClearRect = true (and save/restore around it) if you want clearRect to be a global no-op for transparent output.","Skip the call when any dimension is not finite."],"exampleFix":"// before\nctx.clearRect(area.x, area.y, area.width, area.height);\n\n// after\nif ([area.x, area.y, area.width, area.height].every(v => Number.isFinite(+v))) {\n  ctx.clearRect(+area.x, +area.y, +area.width, +area.height);\n}","handlingStrategy":"validation","validationCode":"function safeClearRect(ctx, x, y, w, h) {\n  const a = [x, y, w, h].map(Number);\n  if (!a.every(Number.isFinite)) return;\n  ctx.clearRect(...a);\n}","typeGuard":"const isFinite4 = (x,y,w,h) => [x,y,w,h].every(v => typeof v === 'number' && Number.isFinite(v));","tryCatchPattern":"try { ctx.clearRect(x, y, w, h); }\ncatch (e) { if (!/context2d.clearRect/.test(e.message)) throw e; }","preventionTips":["Remember jsPDF clearRect paints white — it does not erase; set ctx.ignoreClearRect = true for transparent output.","Validate dimensions before calling; the ignoreClearRect flag is checked AFTER the NaN guard."],"tags":["canvas2d","context2d","validation","nan","pdf"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}