TheAlgorithms/JavaScript · error · Error
canvasWidth should be greater than zero
Error message
canvasWidth should be greater than zero
What it means
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.
Source
Thrown at Recursive/KochSnowflake.manual-test.js:12
import { Vector2, iterate } from './KochSnowflake'
/**
* Method to render the Koch snowflake to a canvas.
*
* @param canvasWidth The width of the canvas.
* @param steps The number of iterations.
* @returns The canvas of the rendered Koch snowflake.
*/
function getKochSnowflake(canvasWidth = 600, steps = 5) {
if (canvasWidth <= 0) {
throw new Error('canvasWidth should be greater than zero')
}
const offsetX = canvasWidth / 10.0
const offsetY = canvasWidth / 3.7
const vector1 = new Vector2(offsetX, offsetY)
const vector2 = new Vector2(
canvasWidth / 2,
Math.sin(Math.PI / 3) * canvasWidth * 0.8 + offsetY
)
const vector3 = new Vector2(canvasWidth - offsetX, offsetY)
const initialVectors = []
initialVectors.push(vector1)
initialVectors.push(vector2)
initialVectors.push(vector3)
initialVectors.push(vector1)
const vectors = iterate(initialVectors, steps)
return drawToCanvas(vectors, canvasWidth, canvasWidth)
}View on GitHub (pinned to 5c39e87a9a)
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).
Example fix
// before getKochSnowflake(canvas.clientWidth, 5) // after const width = canvas.clientWidth > 0 ? canvas.clientWidth : 600 getKochSnowflake(width, 5)
Defensive patterns
Strategy: validation
Validate before calling
function safeKochSnowflake(canvasWidth, steps = 5) {
if (typeof canvasWidth !== 'number' || canvasWidth <= 0) {
throw new RangeError('canvasWidth must be a positive number')
}
return getKochSnowflake(canvasWidth, steps)
} Type guard
function isPositiveNumber(n) {
return typeof n === 'number' && Number.isFinite(n) && n > 0
} Try / catch
try {
getKochSnowflake(width, steps)
} catch (e) {
if (e.message.includes('canvasWidth should be greater than zero')) {
getKochSnowflake(600, steps) // fall back to default
} else throw e
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid Triangle sides.
- Grid must be a non-empty array
- LFUCache ERROR: The Capacity is 0
- Both keyword and message must be specified
- Invalid keyword!
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/22db4120046dd68e.
Report an issue: GitHub.