parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.context2d.arc

Error message

Invalid arguments passed to jsPDF.context2d.arc

What it means

context2d.arc() draws an arc or circle segment specified by center (x, y), radius, start angle, and end angle. Five parameters are validated with isNaN() — the optional sixth parameter (counterclockwise) is not checked because it is coerced via Boolean(). NaN in any of the five checked parameters would corrupt the PDF arc path data. The function logs the invalid arguments to console.error before throwing.

Source

Thrown at src/modules/context2d.js:989

   * @description The arc() method creates an arc/curve (used to create circles, or parts of circles).
   */
  Context2D.prototype.arc = function(
    x,
    y,
    radius,
    startAngle,
    endAngle,
    counterclockwise
  ) {
    if (
      isNaN(x) ||
      isNaN(y) ||
      isNaN(radius) ||
      isNaN(startAngle) ||
      isNaN(endAngle)
    ) {
      console.error("jsPDF.context2d.arc: Invalid arguments", arguments);
      throw new Error("Invalid arguments passed to jsPDF.context2d.arc");
    }
    counterclockwise = Boolean(counterclockwise);

    if (!this.ctx.transform.isIdentity) {
      var xpt = this.ctx.transform.applyToPoint(new Point(x, y));
      x = xpt.x;
      y = xpt.y;

      var x_radPt = this.ctx.transform.applyToPoint(new Point(0, radius));
      var x_radPt0 = this.ctx.transform.applyToPoint(new Point(0, 0));
      radius = Math.sqrt(
        Math.pow(x_radPt.x - x_radPt0.x, 2) +
          Math.pow(x_radPt.y - x_radPt0.y, 2)
      );
    }
    if (Math.abs(endAngle - startAngle) >= 2 * Math.PI) {
      startAngle = 0;
      endAngle = 2 * Math.PI;

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Validate parameters before calling: if ([x,y,radius,startAngle,endAngle].every(isFinite)) ctx.arc(...)
  2. Guard radius against zero or negative values that may produce NaN downstream: radius = Math.max(0, radius)
  3. For pie charts with zero-total segments, guard angle calculations: var angle = total > 0 ? (value / total) * Math.PI * 2 : 0
  4. Check console.error output which logs the invalid arguments before the throw

Example fix

// before
var radius = Math.sqrt(area / Math.PI); // NaN if area is negative
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2); // throws

// after
var radius = Math.sqrt(Math.max(0, area) / Math.PI);
if (isFinite(radius) && radius > 0) {
  ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate arc parameters before calling
function safeArc(ctx, x, y, radius, startAngle, endAngle, counterclockwise) {
  x = Number(x); y = Number(y);
  radius = Number(radius);
  startAngle = Number(startAngle);
  endAngle = Number(endAngle);
  if (!isFinite(x) || !isFinite(y) || !isFinite(radius) ||
      !isFinite(startAngle) || !isFinite(endAngle)) {
    throw new TypeError('arc requires finite numbers for x, y, radius, startAngle, endAngle');
  }
  if (radius < 0) throw new RangeError('radius must be non-negative');
  ctx.arc(x, y, radius, startAngle, endAngle, counterclockwise);
}

Type guard

/**
 * @param {*} value
 * @returns {boolean}
 */
function isValidArcRadius(value) {
  return typeof value === 'number' && isFinite(value) && value >= 0;
}

Try / catch

try {
  ctx.arc(x, y, radius, startAngle, endAngle);
} catch (e) {
  if (e.message.includes('context2d.arc')) {
    // Default to unit circle if parameters are invalid
    ctx.arc(Number(x) || 0, Number(y) || 0, Math.max(0, Number(radius) || 1), 0, Math.PI * 2);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling ctx.arc(undefined, y, radius, 0, Math.PI). Passing NaN for radius (e.g., from a negative value passed to Math.sqrt). Computing angles from invalid trigonometric operations. Using variables from destructured objects where the radius or angle key is absent.

Common situations: Drawing pie charts where a segment angle computes to NaN (e.g., 0/0 for a segment with zero total). Circle drawing where radius comes from a measurement that fails. Animation timing where angle interpolation produces NaN at boundaries. Coordinate data from getBoundingClientRect on hidden elements.

Related errors


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