parallax/jsPDF · error · Error
Invalid arguments passed to jsPDF.context2d.moveTo
Error message
Invalid arguments passed to jsPDF.context2d.moveTo
What it means
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.
Source
Thrown at src/modules/context2d.js:802
this.path = [
{
type: "begin"
}
];
};
/**
* Moves the path to the specified point in the canvas, without creating a line
*
* @name moveTo
* @function
* @param x {Number} The x-coordinate of where to move the path to
* @param y {Number} The y-coordinate of where to move the path to
*/
Context2D.prototype.moveTo = function(x, y) {
if (isNaN(x) || isNaN(y)) {
console.error("jsPDF.context2d.moveTo: Invalid arguments", arguments);
throw new Error("Invalid arguments passed to jsPDF.context2d.moveTo");
}
var pt = this.ctx.transform.applyToPoint(new Point(x, y));
this.path.push({
type: "mt",
x: pt.x,
y: pt.y
});
this.ctx.lastPoint = new Point(x, y);
};
/**
* Creates a path from the current point back to the starting point
*
* @name closePath
* @function
* @description The closePath() method creates a path from the current point back to the starting point.View on GitHub (pinned to a3930ce03a)
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
Example fix
// before var x = parseFloat(element.style.left); // NaN if not set ctx.moveTo(x, 0); // throws // after ctx.moveTo(parseFloat(element.style.left) || 0, 0); // or var x = Number(element.style.left) || 0; ctx.moveTo(x, 0);
Defensive patterns
Strategy: validation
Validate before calling
// Validate coordinates before calling moveTo
function safeMoveTo(ctx, x, y) {
x = Number(x);
y = Number(y);
if (!isFinite(x) || !isFinite(y)) {
throw new TypeError('moveTo requires finite numbers for x and y');
}
ctx.moveTo(x, y);
} Type guard
/**
* @param {*} value
* @returns {boolean}
*/
function isFiniteNumber(value) {
return typeof value === 'number' && isFinite(value);
} Try / catch
try {
ctx.moveTo(x, y);
} catch (e) {
if (e.message.includes('moveTo')) {
ctx.moveTo(0, 0); // default to origin
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid arguments passed to jsPDF.context2d.lineTo
- Invalid arguments passed to jsPDF.context2d.quadraticCurveTo
- Invalid arguments passed to jsPDF.context2d.bezierCurveTo
- Invalid arguments passed to jsPDF.context2d.arc
- Invalid coordinates passed to jsPDF.addImage
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/732a08faa4b2796e.
Report an issue: GitHub.