{"record":{"id":"22db4120046dd68e","repo":"TheAlgorithms/JavaScript","slug":"canvaswidth-should-be-greater-than-zero","errorCode":null,"errorMessage":"canvasWidth should be greater than zero","messagePattern":"canvasWidth should be greater than zero","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Recursive/KochSnowflake.manual-test.js","lineNumber":12,"sourceCode":"import { Vector2, iterate } from './KochSnowflake'\n\n/**\n * Method to render the Koch snowflake to a canvas.\n *\n * @param canvasWidth The width of the canvas.\n * @param steps The number of iterations.\n * @returns The canvas of the rendered Koch snowflake.\n */\nfunction getKochSnowflake(canvasWidth = 600, steps = 5) {\n  if (canvasWidth <= 0) {\n    throw new Error('canvasWidth should be greater than zero')\n  }\n\n  const offsetX = canvasWidth / 10.0\n  const offsetY = canvasWidth / 3.7\n  const vector1 = new Vector2(offsetX, offsetY)\n  const vector2 = new Vector2(\n    canvasWidth / 2,\n    Math.sin(Math.PI / 3) * canvasWidth * 0.8 + offsetY\n  )\n  const vector3 = new Vector2(canvasWidth - offsetX, offsetY)\n  const initialVectors = []\n  initialVectors.push(vector1)\n  initialVectors.push(vector2)\n  initialVectors.push(vector3)\n  initialVectors.push(vector1)\n  const vectors = iterate(initialVectors, steps)\n  return drawToCanvas(vectors, canvasWidth, canvasWidth)\n}","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Recursive/KochSnowflake.manual-test.js#L1-L30","documentation":"Thrown by getKochSnowflake() in the recursive Koch snowflake renderer when canvasWidth is zero or negative. The renderer divides and scales geometry by canvasWidth (e.g. offsetX = canvasWidth/10.0, offsetY = canvasWidth/3.7), so a non-positive width would yield degenerate or negative coordinates and an invisible/broken fractal. The guard rejects such input early rather than rendering garbage.","triggerScenarios":"Calling getKochSnowflake(0), getKochSnowflake(-100), or getKochSnowflake(someVariable) where someVariable resolved to 0 or a negative number. Also when a canvas element has CSS display:none giving offsetWidth 0 that gets passed in.","commonSituations":"Reading canvasWidth from a DOM canvas whose clientWidth is 0 before layout; passing a config object's width property that defaulted to 0; unit-testing with a zero width to 'skip' rendering; negative values from a subtractive size calculation.","solutions":["Pass a positive integer/float such as the default 600: getKochSnowflake(600, 5).","If reading from a canvas element, ensure layout is complete and use canvas.width || 600 as a fallback.","Guard the caller: if (canvasWidth > 0) getKochSnowflake(canvasWidth, steps)."],"exampleFix":"// before\ngetKochSnowflake(canvas.clientWidth, 5)\n\n// after\nconst width = canvas.clientWidth > 0 ? canvas.clientWidth : 600\ngetKochSnowflake(width, 5)","handlingStrategy":"validation","validationCode":"function safeKochSnowflake(canvasWidth, steps = 5) {\n  if (typeof canvasWidth !== 'number' || canvasWidth <= 0) {\n    throw new RangeError('canvasWidth must be a positive number')\n  }\n  return getKochSnowflake(canvasWidth, steps)\n}","typeGuard":"function isPositiveNumber(n) {\n  return typeof n === 'number' && Number.isFinite(n) && n > 0\n}","tryCatchPattern":"try {\n  getKochSnowflake(width, steps)\n} catch (e) {\n  if (e.message.includes('canvasWidth should be greater than zero')) {\n    getKochSnowflake(600, steps) // fall back to default\n  } else throw e\n}","preventionTips":["Always supply an explicit positive default for canvas dimensions.","Read canvas dimensions only after layout completes (e.g. in requestAnimationFrame).","Reject or substitute zero/negative widths at the config boundary."],"tags":["input-validation","geometry","canvas","positive-number"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}