parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.context2d.rect

Error message

Invalid arguments passed to jsPDF.context2d.rect

What it means

jsPDF.context2d.rect validates its four numeric arguments with isNaN() before building the rectangle path. Because isNaN(undefined) is true, omitting any argument (or passing a non-numeric value) trips the guard. The console.error logs the raw arguments object, then a descriptive Error is thrown, halting path construction.

Source

Thrown at src/modules/context2d.js:1053

  Context2D.prototype.arcTo = function(x1, y1, x2, y2, radius) {
    throw new Error("arcTo not implemented.");
  };

  /**
   * Creates a rectangle
   *
   * @name rect
   * @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, in pixels
   * @param h {Number} The height of the rectangle, in pixels
   * @description The rect() method creates a rectangle.
   */
  Context2D.prototype.rect = function(x, y, w, h) {
    if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {
      console.error("jsPDF.context2d.rect: Invalid arguments", arguments);
      throw new Error("Invalid arguments passed to jsPDF.context2d.rect");
    }
    this.moveTo(x, y);
    this.lineTo(x + w, y);
    this.lineTo(x + w, y + h);
    this.lineTo(x, y + h);
    this.lineTo(x, y);
    this.lineTo(x + w, y);
    this.lineTo(x, y);
  };

  /**
   * Draws a "filled" rectangle
   *
   * @name fillRect
   * @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, in pixels

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Pass four finite numbers — coerce with Number() and apply defaults: ctx.rect(x|0, y|0, w||0, h||0).
  2. Validate inputs upstream and skip the rect call when any dimension is missing or non-numeric.
  3. Strip units/strings before calling (parseFloat('100px') => 100).

Example fix

// before
ctx.rect(box.x, box.y, box.w, box.h); // throws if any field is undefined

// after
const r = ['x','y','w','h'].map(k => Number(box[k]));
if (r.every(v => Number.isFinite(v))) ctx.rect(...r);
Defensive patterns

Strategy: validation

Validate before calling

function safeRect(ctx, x, y, w, h) {
  const [nx, ny, nw, nh] = [x, y, w, h].map(Number);
  if (![nx, ny, nw, nh].every(Number.isFinite)) {
    console.warn('rect skipped: non-finite args', { x, y, w, h });
    return;
  }
  ctx.rect(nx, ny, nw, nh);
}

Type guard

function isRectArgs(x, y, w, h) {
  return [x, y, w, h].every(v => typeof v === 'number' && Number.isFinite(v));
}

Try / catch

try { ctx.rect(x, y, w, h); }
catch (e) {
  if (/Invalid arguments passed to jsPDF.context2d.rect/.test(e.message)) {
    // coerce/fix and retry with finite numbers, or skip
  } else throw e;
}

Prevention

When it happens

Trigger: Calling ctx.rect() with fewer than four arguments; passing a string, null-undefined variable, or NaN for x/y/w/h; reading dimensions from a malformed config object whose fields are undefined.

Common situations: Dynamic layouts where a layout field is missing and defaults are not applied; passing CSS-pixel strings ('10px') instead of numbers; math that divides by zero producing NaN dimensions.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/f3d54fb5452e54d1. Report an issue: GitHub.