parallax/jsPDF · error · Error

Invalid arguments passed to jsPDF.context2d.lineTo

Error message

Invalid arguments passed to jsPDF.context2d.lineTo

What it means

context2d.lineTo() adds a line segment endpoint to the current path, mirroring the HTML Canvas 2D API. jsPDF validates x and y with isNaN() because NaN path coordinates would corrupt the PDF content stream operators (m, l, c). Like moveTo, it logs the invalid arguments to console.error before throwing, giving developers visibility into what was actually passed.

Source

Thrown at src/modules/context2d.js:854

    this.path.push({
      type: "close"
    });
    this.ctx.lastPoint = new Point(pathBegin.x, pathBegin.y);
  };

  /**
   * Adds a new point and creates a line to that point from the last specified point in the canvas
   *
   * @name lineTo
   * @function
   * @param x The x-coordinate of where to create the line to
   * @param y The y-coordinate of where to create the line to
   * @description The lineTo() method adds a new point and creates a line TO that point FROM the last specified point in the canvas (this method does not draw the line).
   */
  Context2D.prototype.lineTo = function(x, y) {
    if (isNaN(x) || isNaN(y)) {
      console.error("jsPDF.context2d.lineTo: Invalid arguments", arguments);
      throw new Error("Invalid arguments passed to jsPDF.context2d.lineTo");
    }

    var pt = this.ctx.transform.applyToPoint(new Point(x, y));

    this.path.push({
      type: "lt",
      x: pt.x,
      y: pt.y
    });
    this.ctx.lastPoint = new Point(pt.x, pt.y);
  };

  /**
   * Clips a region of any shape and size from the original canvas
   *
   * @name clip
   * @function
   * @description The clip() method clips a region of any shape and size from the original canvas.

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Filter or default NaN values in data before drawing: points.filter(p => isFinite(p.x) && isFinite(p.y))
  2. Use defensive coercion: ctx.lineTo(x || 0, y || 0) if 0 is a safe default
  3. Validate coordinate arrays before path construction
  4. Check console.error output for the actual arguments that triggered the error

Example fix

// before
var points = [{x:10,y:20}, null, {x:30,y:40}];
ctx.moveTo(points[0].x, points[0].y);
ctx.lineTo(points[1].x, points[1].y); // throws - points[1] is null

// after
points.forEach(function(p, i) {
  if (p && isFinite(p.x) && isFinite(p.y)) {
    if (i === 0) ctx.moveTo(p.x, p.y);
    else ctx.lineTo(p.x, p.y);
  }
});
Defensive patterns

Strategy: validation

Validate before calling

// Validate coordinates before calling lineTo
function safeLineTo(ctx, x, y) {
  x = Number(x);
  y = Number(y);
  if (!isFinite(x) || !isFinite(y)) {
    return; // skip invalid points silently
  }
  ctx.lineTo(x, y);
}

// Filter NaN points from arrays before drawing
var cleanPoints = points.filter(function(p) {
  return p && isFinite(p.x) && isFinite(p.y);
});

Type guard

/**
 * @param {*} value
 * @returns {boolean}
 */
function isValidPoint2D(value) {
  return value != null &&
    typeof value.x === 'number' && isFinite(value.x) &&
    typeof value.y === 'number' && isFinite(value.y);
}

Try / catch

try {
  ctx.lineTo(x, y);
} catch (e) {
  if (e.message.includes('lineTo')) {
    // Skip this point - likely NaN in data
    console.warn('Skipped invalid lineTo point:', x, y);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling ctx.lineTo(undefined, y). Computing coordinates from arithmetic on undefined values. Passing the result of a function that returns NaN. Forgetting to initialize loop variables that feed into lineTo calls. Using array indexing that returns undefined (out-of-bounds).

Common situations: Drawing charts or graphs where data points may be missing. SVG-to-PDF conversion where path data parsing produces NaN. Polyline/polygon rendering from data arrays with gaps. Coordinate transforms that produce NaN under specific matrix conditions.

Related errors


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