Contour Lines Generator
A pure JavaScript generator of parametric contour lines, exported as beautiful images.
Published ~ Coding
On a topographic map, the shape of the terrain is drawn with contour lines, curves that join the points at the same elevation; the same construction applied to an invented surface turns a few numbers and an interpolation rule into something that looks like a coastline or a mountain ridge. This is something I had wanted to make for a long time: I started sketching it years ago and never finished it, until agentic coding let me close the gap between the idea and a working tool.

The generator draws parametric contour lines. The user places a handful of control points on the canvas; a height function is interpolated from them on a regular grid, optionally roughened with two layers of fBm noise, and for each level the curve
is extracted with marching squares, then stitched, simplified, smoothed and written as an SVG path. The steps are detailed below.
It is pure JavaScript, twelve small modules with no framework and no build step, entirely open source, and it runs in the browser. The whole state of a drawing is serialised in the query string, so the URL is the save format and a shared link restores the exact image; opening the page without parameters gives a fresh random drawing every time. The result can be exported as SVG or PNG.
How it works
Contour lines are level sets of a continuous height function
h(x, y). The user only supplies a handful of scattered samples of that surface, so the pipeline reconstructs it first and slices it second:
- Control points
(x, y, z): the scattered data, placed and dragged on the canvas, in normalised[0,1]²coordinates. Everything downstream is derived from them, so the whole image is one small parameter set, and it fits in a URL. - Scalar field:
his evaluated on a regular grid ofresolution²samples by scattered-data interpolation: either inverse distance weighting or gaussian radial basis functions.- Additive bumps sums the gaussians, so overlapping points build ridges. It’s the most terrain-like of the three;
- inverse distance and gaussian blend average instead, so the field never exceeds the highest control
point. Two optional layers of fBm noise
roughen the surface, and a smoothstep border mask fades it to zero near
the frame so contours close into islands instead of being clipped:
radialgives an oval landmass,framegives rings parallel to the border,nonelets lines run off the edge. The result is rescaled to[0,1]. - The noise layer (amount, scale, octaves) works at the scale of the terrain itself: it moves peaks and basins around and decides where the contours go.
- The detail layer (detail amount, detail scale) is a second fBm, weaker and much finer, with its
own seed offset. Its wavelength is shorter than the spacing between contour levels, so it perturbs the
lines (the eroded, hand-drawn wobble) without displacing the shapes the first layer builds. It is
applied before the border mask, so it fades out at the frame along with everything else. Detail scale is
bounded by the grid: past roughly
resolution / 8it aliases into blocky wobble instead of getting finer.
- Marching squares: the algorithm classifies each cell’s four corners as above or below the level; the 16 possible configurations say which cell edges the contour crosses, and linear interpolation between corner values places the crossing along the edge. Two configurations are ambiguous saddles, the cell-centre average picks a branch.
- Stitching: the loose segments are joined into continuous polylines. Crossings are keyed by the grid edge they lie on rather than by coordinates, so adjacent cells agree exactly and no floating-point tolerance is involved. Chains with a free end run off the frame; the rest close into loops around peaks and basins.
- Simplify & smooth: Ramer–Douglas–Peucker drops vertices that stay within a tolerance of the line they sit on, then Chaikin’s corner cutting rounds the grid-faceted polyline into a curve (it converges to a quadratic B-spline). Simplifying first means smoothing has fewer, longer edges to work with, and the exported file stays small.
- SVG paths: the polylines are mapped to pixels and written as
SVG
M/Lpath data, then coloured from the palette. Topographic and Paper white are fixed lists; Rainbow and Single colour are generated (a hue wheel, and lightness steps of a picked colour), since a palette supplies eithercolorsor abuild(style)function. By default colour is a continuous ramp across the palette, so it encodes elevation; cycle repeats the palette per line and single uses one colour throughout.