{"record":{"id":"732a08faa4b2796e","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-moveto","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.moveTo","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.moveTo","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":802,"sourceCode":"    this.path = [\n      {\n        type: \"begin\"\n      }\n    ];\n  };\n\n  /**\n   * Moves the path to the specified point in the canvas, without creating a line\n   *\n   * @name moveTo\n   * @function\n   * @param x {Number} The x-coordinate of where to move the path to\n   * @param y {Number} The y-coordinate of where to move the path to\n   */\n  Context2D.prototype.moveTo = function(x, y) {\n    if (isNaN(x) || isNaN(y)) {\n      console.error(\"jsPDF.context2d.moveTo: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.moveTo\");\n    }\n\n    var pt = this.ctx.transform.applyToPoint(new Point(x, y));\n\n    this.path.push({\n      type: \"mt\",\n      x: pt.x,\n      y: pt.y\n    });\n    this.ctx.lastPoint = new Point(x, y);\n  };\n\n  /**\n   * Creates a path from the current point back to the starting point\n   *\n   * @name closePath\n   * @function\n   * @description The closePath() method creates a path from the current point back to the starting point.","sourceCodeStart":784,"sourceCodeEnd":820,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L784-L820","documentation":"context2d.moveTo() is the Canvas 2D API equivalent for beginning a new sub-path at the given coordinates. jsPDF validates both x and y with isNaN() to prevent NaN values from propagating into the PDF path data, which would produce corrupt output. NaN typically arises from undefined variables, failed type coercion, or math operations on non-numeric values. The function also logs the invalid arguments to console.error before throwing.","triggerScenarios":"Calling ctx.moveTo(undefined, 10) or ctx.moveTo(NaN, 10). Passing a variable computed from division by zero. Using parseFloat('abc') which returns NaN. Passing null for coordinates. Destructuring from an object where the coordinate keys don't exist.","commonSituations":"Rendering SVG paths where coordinate parsing fails on malformed data. Drawing functions that receive undefined when an element is not found in the DOM. Animation loops where a coordinate becomes NaN at boundary conditions. Porting Canvas code where some values are lazily initialized.","solutions":["Ensure coordinates are finite numbers: ctx.moveTo(Number(x) || 0, Number(y) || 0)","Validate before calling: if (isFinite(x) && isFinite(y)) ctx.moveTo(x, y)","Check for undefined: ctx.moveTo(x ?? 0, y ?? 0)","Debug by checking console.error output which prints the actual arguments passed"],"exampleFix":"// before\nvar x = parseFloat(element.style.left); // NaN if not set\nctx.moveTo(x, 0); // throws\n\n// after\nctx.moveTo(parseFloat(element.style.left) || 0, 0);\n// or\nvar x = Number(element.style.left) || 0;\nctx.moveTo(x, 0);","handlingStrategy":"validation","validationCode":"// Validate coordinates before calling moveTo\nfunction safeMoveTo(ctx, x, y) {\n  x = Number(x);\n  y = Number(y);\n  if (!isFinite(x) || !isFinite(y)) {\n    throw new TypeError('moveTo requires finite numbers for x and y');\n  }\n  ctx.moveTo(x, y);\n}","typeGuard":"/**\n * @param {*} value\n * @returns {boolean}\n */\nfunction isFiniteNumber(value) {\n  return typeof value === 'number' && isFinite(value);\n}","tryCatchPattern":"try {\n  ctx.moveTo(x, y);\n} catch (e) {\n  if (e.message.includes('moveTo')) {\n    ctx.moveTo(0, 0); // default to origin\n  } else throw e;\n}","preventionTips":["Use isFinite() to check coordinates before path operations","Initialize coordinate variables to 0 rather than leaving undefined","Check console.error output which logs the actual arguments before the throw"],"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"}