parallax/jsPDF · error · Error
Invalid arguments passed to jsPDF.context2d.clearRect
Error message
Invalid arguments passed to jsPDF.context2d.clearRect
What it means
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.
Source
Thrown at src/modules/context2d.js:1147
/**
* Clears the specified pixels within a given rectangle
*
* @name clearRect
* @function
* @param x {Number} The x-coordinate of the upper-left corner of the rectangle
* @param y {Number} The y-coordinate of the upper-left corner of the rectangle
* @param w {Number} The width of the rectangle to clear, in pixels
* @param h {Number} The height of the rectangle to clear, in pixels
* @description We cannot clear PDF commands that were already written to PDF, so we use white instead. <br />
* As a special case, read a special flag (ignoreClearRect) and do nothing if it is set.
* This results in all calls to clearRect() to do nothing, and keep the canvas transparent.
* This flag is stored in the save/restore context and is managed the same way as other drawing states.
*
*/
Context2D.prototype.clearRect = function(x, y, w, h) {
if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {
console.error("jsPDF.context2d.clearRect: Invalid arguments", arguments);
throw new Error("Invalid arguments passed to jsPDF.context2d.clearRect");
}
if (this.ignoreClearRect) {
return;
}
this.fillStyle = "#ffffff";
this.fillRect(x, y, w, h);
};
/**
* Saves the state of the current context
*
* @name save
* @function
*/
Context2D.prototype.save = function(doStackPush) {
doStackPush = typeof doStackPush === "boolean" ? doStackPush : true;
var tmpPageNumber = this.pdf.internal.getCurrentPageInfo().pageNumber;View on GitHub (pinned to a3930ce03a)
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.
Example fix
// before
ctx.clearRect(area.x, area.y, area.width, area.height);
// after
if ([area.x, area.y, area.width, area.height].every(v => Number.isFinite(+v))) {
ctx.clearRect(+area.x, +area.y, +area.width, +area.height);
} Defensive patterns
Strategy: validation
Validate before calling
function safeClearRect(ctx, x, y, w, h) {
const a = [x, y, w, h].map(Number);
if (!a.every(Number.isFinite)) return;
ctx.clearRect(...a);
} Type guard
const isFinite4 = (x,y,w,h) => [x,y,w,h].every(v => typeof v === 'number' && Number.isFinite(v));
Try / catch
try { ctx.clearRect(x, y, w, h); }
catch (e) { if (!/context2d.clearRect/.test(e.message)) throw e; } Prevention
- 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.
When it happens
Trigger: ctx.clearRect(x, y, w, h) with any undefined/NaN/missing argument; clearing regions computed from missing layout fields.
Common situations: 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.
Related errors
- Invalid arguments passed to jsPDF.context2d.rect
- Invalid arguments passed to jsPDF.context2d.fillRect
- Invalid arguments passed to jsPDF.context2d.strokeRect
- Invalid arguments passed to jsPDF.context2d.fillText
- Invalid arguments passed to jsPDF.context2d.strokeText
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/a00c818accee5eef.
Report an issue: GitHub.