{"record":{"id":"abba7bdc457f9f79","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-lineto","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.lineTo","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.lineTo","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":854,"sourceCode":"    this.path.push({\n      type: \"close\"\n    });\n    this.ctx.lastPoint = new Point(pathBegin.x, pathBegin.y);\n  };\n\n  /**\n   * Adds a new point and creates a line to that point from the last specified point in the canvas\n   *\n   * @name lineTo\n   * @function\n   * @param x The x-coordinate of where to create the line to\n   * @param y The y-coordinate of where to create the line to\n   * @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).\n   */\n  Context2D.prototype.lineTo = function(x, y) {\n    if (isNaN(x) || isNaN(y)) {\n      console.error(\"jsPDF.context2d.lineTo: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.lineTo\");\n    }\n\n    var pt = this.ctx.transform.applyToPoint(new Point(x, y));\n\n    this.path.push({\n      type: \"lt\",\n      x: pt.x,\n      y: pt.y\n    });\n    this.ctx.lastPoint = new Point(pt.x, pt.y);\n  };\n\n  /**\n   * Clips a region of any shape and size from the original canvas\n   *\n   * @name clip\n   * @function\n   * @description The clip() method clips a region of any shape and size from the original canvas.","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L836-L872","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Filter or default NaN values in data before drawing: points.filter(p => isFinite(p.x) && isFinite(p.y))","Use defensive coercion: ctx.lineTo(x || 0, y || 0) if 0 is a safe default","Validate coordinate arrays before path construction","Check console.error output for the actual arguments that triggered the error"],"exampleFix":"// before\nvar points = [{x:10,y:20}, null, {x:30,y:40}];\nctx.moveTo(points[0].x, points[0].y);\nctx.lineTo(points[1].x, points[1].y); // throws - points[1] is null\n\n// after\npoints.forEach(function(p, i) {\n  if (p && isFinite(p.x) && isFinite(p.y)) {\n    if (i === 0) ctx.moveTo(p.x, p.y);\n    else ctx.lineTo(p.x, p.y);\n  }\n});","handlingStrategy":"validation","validationCode":"// Validate coordinates before calling lineTo\nfunction safeLineTo(ctx, x, y) {\n  x = Number(x);\n  y = Number(y);\n  if (!isFinite(x) || !isFinite(y)) {\n    return; // skip invalid points silently\n  }\n  ctx.lineTo(x, y);\n}\n\n// Filter NaN points from arrays before drawing\nvar cleanPoints = points.filter(function(p) {\n  return p && isFinite(p.x) && isFinite(p.y);\n});","typeGuard":"/**\n * @param {*} value\n * @returns {boolean}\n */\nfunction isValidPoint2D(value) {\n  return value != null &&\n    typeof value.x === 'number' && isFinite(value.x) &&\n    typeof value.y === 'number' && isFinite(value.y);\n}","tryCatchPattern":"try {\n  ctx.lineTo(x, y);\n} catch (e) {\n  if (e.message.includes('lineTo')) {\n    // Skip this point - likely NaN in data\n    console.warn('Skipped invalid lineTo point:', x, y);\n  } else throw e;\n}","preventionTips":["Filter data point arrays to remove entries with NaN coordinates before drawing","Use isFinite() checks in drawing loops to skip invalid points","Initialize all coordinate variables to numeric defaults"],"tags":["context2d","path","nan","coordinates","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}