{"record":{"id":"81b07e711522b826","repo":"parallax/jsPDF","slug":"invalid-arguments-passed-to-jspdf-context2d-arc","errorCode":null,"errorMessage":"Invalid arguments passed to jsPDF.context2d.arc","messagePattern":"Invalid arguments passed to jsPDF\\.context2d\\.arc","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":989,"sourceCode":"   * @description The arc() method creates an arc/curve (used to create circles, or parts of circles).\n   */\n  Context2D.prototype.arc = function(\n    x,\n    y,\n    radius,\n    startAngle,\n    endAngle,\n    counterclockwise\n  ) {\n    if (\n      isNaN(x) ||\n      isNaN(y) ||\n      isNaN(radius) ||\n      isNaN(startAngle) ||\n      isNaN(endAngle)\n    ) {\n      console.error(\"jsPDF.context2d.arc: Invalid arguments\", arguments);\n      throw new Error(\"Invalid arguments passed to jsPDF.context2d.arc\");\n    }\n    counterclockwise = Boolean(counterclockwise);\n\n    if (!this.ctx.transform.isIdentity) {\n      var xpt = this.ctx.transform.applyToPoint(new Point(x, y));\n      x = xpt.x;\n      y = xpt.y;\n\n      var x_radPt = this.ctx.transform.applyToPoint(new Point(0, radius));\n      var x_radPt0 = this.ctx.transform.applyToPoint(new Point(0, 0));\n      radius = Math.sqrt(\n        Math.pow(x_radPt.x - x_radPt0.x, 2) +\n          Math.pow(x_radPt.y - x_radPt0.y, 2)\n      );\n    }\n    if (Math.abs(endAngle - startAngle) >= 2 * Math.PI) {\n      startAngle = 0;\n      endAngle = 2 * Math.PI;","sourceCodeStart":971,"sourceCodeEnd":1007,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L971-L1007","documentation":"context2d.arc() draws an arc or circle segment specified by center (x, y), radius, start angle, and end angle. Five parameters are validated with isNaN() — the optional sixth parameter (counterclockwise) is not checked because it is coerced via Boolean(). NaN in any of the five checked parameters would corrupt the PDF arc path data. The function logs the invalid arguments to console.error before throwing.","triggerScenarios":"Calling ctx.arc(undefined, y, radius, 0, Math.PI). Passing NaN for radius (e.g., from a negative value passed to Math.sqrt). Computing angles from invalid trigonometric operations. Using variables from destructured objects where the radius or angle key is absent.","commonSituations":"Drawing pie charts where a segment angle computes to NaN (e.g., 0/0 for a segment with zero total). Circle drawing where radius comes from a measurement that fails. Animation timing where angle interpolation produces NaN at boundaries. Coordinate data from getBoundingClientRect on hidden elements.","solutions":["Validate parameters before calling: if ([x,y,radius,startAngle,endAngle].every(isFinite)) ctx.arc(...)","Guard radius against zero or negative values that may produce NaN downstream: radius = Math.max(0, radius)","For pie charts with zero-total segments, guard angle calculations: var angle = total > 0 ? (value / total) * Math.PI * 2 : 0","Check console.error output which logs the invalid arguments before the throw"],"exampleFix":"// before\nvar radius = Math.sqrt(area / Math.PI); // NaN if area is negative\nctx.arc(centerX, centerY, radius, 0, Math.PI * 2); // throws\n\n// after\nvar radius = Math.sqrt(Math.max(0, area) / Math.PI);\nif (isFinite(radius) && radius > 0) {\n  ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);\n}","handlingStrategy":"validation","validationCode":"// Validate arc parameters before calling\nfunction safeArc(ctx, x, y, radius, startAngle, endAngle, counterclockwise) {\n  x = Number(x); y = Number(y);\n  radius = Number(radius);\n  startAngle = Number(startAngle);\n  endAngle = Number(endAngle);\n  if (!isFinite(x) || !isFinite(y) || !isFinite(radius) ||\n      !isFinite(startAngle) || !isFinite(endAngle)) {\n    throw new TypeError('arc requires finite numbers for x, y, radius, startAngle, endAngle');\n  }\n  if (radius < 0) throw new RangeError('radius must be non-negative');\n  ctx.arc(x, y, radius, startAngle, endAngle, counterclockwise);\n}","typeGuard":"/**\n * @param {*} value\n * @returns {boolean}\n */\nfunction isValidArcRadius(value) {\n  return typeof value === 'number' && isFinite(value) && value >= 0;\n}","tryCatchPattern":"try {\n  ctx.arc(x, y, radius, startAngle, endAngle);\n} catch (e) {\n  if (e.message.includes('context2d.arc')) {\n    // Default to unit circle if parameters are invalid\n    ctx.arc(Number(x) || 0, Number(y) || 0, Math.max(0, Number(radius) || 1), 0, Math.PI * 2);\n  } else throw e;\n}","preventionTips":["Guard radius with Math.max(0, radius) to prevent negative/NaN values","Validate all five checked parameters (x, y, radius, startAngle, endAngle) with isFinite","For pie charts, guard against division by zero that produces NaN angles"],"tags":["context2d","path","arc","nan","coordinates","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}