parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.context2d.strokeRect

Error message

Invalid arguments passed to jsPDF.context2d.strokeRect

What it means

strokeRect validates x/y/w/h with isNaN up front and throws before the transparency check or beginPath/rect/stroke pipeline. Even when the stroke style is fully transparent, the guard still fires because validation precedes isStrokeTransparent.

Source

Thrown at src/modules/context2d.js:1119

      this.lineJoin = tmp.lineJoin;
    }
  };

  /**
   *     Draws a rectangle (no fill)
   *
   * @name strokeRect
   * @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 strokeRect() method draws a rectangle (no fill). The default color of the stroke is black.
   */
  Context2D.prototype.strokeRect = function strokeRect(x, y, w, h) {
    if (isNaN(x) || isNaN(y) || isNaN(w) || isNaN(h)) {
      console.error("jsPDF.context2d.strokeRect: Invalid arguments", arguments);
      throw new Error("Invalid arguments passed to jsPDF.context2d.strokeRect");
    }
    if (isStrokeTransparent.call(this)) {
      return;
    }
    this.beginPath();
    this.rect(x, y, w, h);
    this.stroke();
  };

  /**
   * 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

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Coerce to finite numbers and guard with Number.isFinite before calling.
  2. Provide explicit numeric defaults (e.g. 0) for missing dimensions.
  3. Move dimension validation into a shared helper used by rect/fillRect/strokeRect/clearRect.

Example fix

// before
ctx.strokeRect(o.x, o.y, o.w, o.h);

// after
function safeStrokeRect(ctx, x, y, w, h) {
  if (![x, y, w, h].every(v => Number.isFinite(+v))) return;
  ctx.strokeRect(+x, +y, +w, +h);
}
Defensive patterns

Strategy: validation

Validate before calling

function safeStrokeRect(ctx, x, y, w, h) {
  const a = [x, y, w, h].map(Number);
  if (!a.every(Number.isFinite)) return;
  ctx.strokeRect(...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.strokeRect(x, y, w, h); }
catch (e) { if (!/context2d.strokeRect/.test(e.message)) throw e; }

Prevention

When it happens

Trigger: ctx.strokeRect(x, y, w, h) where any argument is undefined, omitted, or non-numeric; computing dimensions from null/undefined layout data.

Common situations: Drawing borders around optional panels where dimensions may be absent; passing template-literal numbers; measurement racing producing NaN mid-layout.

Related errors


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