parallax/jsPDF · error · Error
Invalid arguments passed to jsPDF.context2d.fillText
Error message
Invalid arguments passed to jsPDF.context2d.fillText
What it means
fillText requires the text argument to be a string and x/y to be finite numbers; the guard is isNaN(x) || isNaN(y) || typeof text !== 'string'. maxWidth is optional — an NaN maxWidth is silently coerced to undefined, so it never triggers this error. Validation runs before the fill-transparency check.
Source
Thrown at src/modules/context2d.js:1329
var isStrokeTransparent = function() {
return Boolean(this.ctx.isStrokeTransparent || this.globalAlpha == 0);
};
/**
* Draws "filled" text on the canvas
*
* @name fillText
* @function
* @param text {String} Specifies the text that will be written on the canvas
* @param x {Number} The x coordinate where to start painting the text (relative to the canvas)
* @param y {Number} The y coordinate where to start painting the text (relative to the canvas)
* @param maxWidth {Number} Optional. The maximum allowed width of the text, in pixels
* @description The fillText() method draws filled text on the canvas. The default color of the text is black.
*/
Context2D.prototype.fillText = function(text, x, y, maxWidth) {
if (isNaN(x) || isNaN(y) || typeof text !== "string") {
console.error("jsPDF.context2d.fillText: Invalid arguments", arguments);
throw new Error("Invalid arguments passed to jsPDF.context2d.fillText");
}
maxWidth = isNaN(maxWidth) ? undefined : maxWidth;
if (isFillTransparent.call(this)) {
return;
}
var degs = rad2deg(this.ctx.transform.rotation);
// We only use X axis as scale hint
var scale = this.ctx.transform.scaleX;
putText.call(this, {
text: text,
x: x,
y: y,
scale: scale,
angle: degs,
align: this.textAlign,View on GitHub (pinned to a3930ce03a)
Solutions
- Coerce text with String(...) and validate x/y with Number.isFinite before calling.
- Guard the whole call: if (typeof text === 'string' && Number.isFinite(+x) && Number.isFinite(+y)) ctx.fillText(text, +x, +y);
- Check argument order — text comes first, then x, then y, then optional maxWidth.
Example fix
// before ctx.fillText(score, x, y); // score is a number -> throws [106] // after if (Number.isFinite(x) && Number.isFinite(y)) ctx.fillText(String(score), x, y);
Defensive patterns
Strategy: validation
Validate before calling
function safeFillText(ctx, text, x, y, maxWidth) {
if (typeof text !== 'string') text = String(text);
if (!Number.isFinite(+x) || !Number.isFinite(+y)) return;
ctx.fillText(text, +x, +y, Number.isFinite(+maxWidth) ? +maxWidth : undefined);
} Type guard
function isFillTextArgs(text, x, y) {
return typeof text === 'string' && Number.isFinite(+x) && Number.isFinite(+y);
} Try / catch
try { ctx.fillText(text, x, y); }
catch (e) { if (!/context2d.fillText/.test(e.message)) throw e; /* else stringify+retry */ } Prevention
- Always stringify dynamic values before passing as text.
- Keep argument order: text, x, y, [maxWidth].
- Validate coordinates at the layout boundary.
When it happens
Trigger: ctx.fillText(text, x, y) where text is a number, object, undefined, or null; x or y undefined/NaN; passing a numeric value where text was expected (e.g. argument order swapped).
Common situations: Rendering metrics or counters without String() coercion; calling fillText(value, x, y) instead of fillText(String(value), x, y); reading x/y from NaN-producing transforms.
Related errors
- Invalid arguments passed to jsPDF.context2d.strokeText
- Invalid arguments passed to jsPDF.context2d.rect
- Invalid arguments passed to jsPDF.context2d.fillRect
- Invalid arguments passed to jsPDF.context2d.strokeRect
- Invalid arguments passed to jsPDF.context2d.clearRect
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/514108610e4fe246.
Report an issue: GitHub.