Skip to content

Noise and colour

Two libraries make procedural image generation much more powerful: simplex-noise for smooth pseudo-random values and chroma-js for colour scales. This tutorial shows how to use them together to produce rich, varied textures and visualisations.

Simplex noise returns a smooth, continuous pseudo-random value for any coordinate. Unlike Math.random(), nearby coordinates produce nearby values — which is exactly what you want for terrain, texture, or animation.

const { createNoise2D } = require('simplex-noise')
// createNoise2D() returns a function you call with (x, y)
const noise2D = createNoise2D()
const v = noise2D(0.5, 0.3) // a value in roughly [-1, 1]

Each call to createNoise2D() creates an independent noise function with a different internal permutation table (seeded from Math.random by default). Pass a custom PRNG to get a deterministic result:

function seededPrng(seed) {
let s = (seed % 2147483647 + 2147483647) % 2147483647
return () => { s = s * 16807 % 2147483647; return (s - 1) / 2147483646 }
}
const noise2D = createNoise2D(seededPrng(42))

Scale the input coordinates to control the feature size:

// Large features: divide by canvas size so coordinates are in [0, 1]
const v = noise2D(px / width, py / height)
// Adjust scale to zoom in or out
const scale = 3
const v = noise2D(px * scale / width, py * scale / height)

A scale of 1 gives roughly one feature across the whole image; 4 gives finer detail. The output is always in approximately [-1, 1].

Layering several noise samples at increasing frequencies and decreasing amplitudes produces “fractal” detail — coarse shape with fine texture on top:

function fbm(noise2D, x, y, octaves) {
let value = 0, amplitude = 1, frequency = 1, maxAmp = 0
for (let o = 0; o < octaves; o++) {
value += noise2D(x * frequency, y * frequency) * amplitude
maxAmp += amplitude
amplitude *= 0.5
frequency *= 2
}
return value / maxAmp // normalised to [-1, 1]
}

Calling fbm(noise2D, px/w, py/h, 6) with 6 octaves gives you terrain-like results. More octaves = more detail, but also more computation.

A colour scale maps a [0, 1] value to a colour. Define one with an array of colours and call it like a function:

const chroma = require('chroma-js')
const scale = chroma.scale(['#001b4a', '#0054a0', '#00e8f8', '#ffffff'])
.mode('lab') // perceptually uniform interpolation
scale(0).hex() // '#001b4a'
scale(0.5).hex() // midpoint
scale(1).hex() // '#ffffff'

Match specific colours to specific data values:

const terrain = chroma.scale([
'#14608c', // water
'#d4c88f', // beach
'#62ab50', // lowland
'#6e5537', // mountain
'#ffffff', // snow
]).domain([0, 0.35, 0.50, 0.78, 1.0]).mode('lab')

Pre-computing a palette array is much faster than calling scale() inside a per-pixel loop, since scale() does floating-point arithmetic each call:

const N = 256
const palette = chroma.scale(['#000', '#e94560', '#fff']).colors(N)
// palette is an array of N hex strings
// In the per-pixel loop:
const idx = Math.floor(t * (N - 1))
const [r, g, b] = chroma(palette[idx]).rgb()

Drawing one rectangle per pixel with ctx.fillRect is extremely slow for large canvases. Use ImageData instead — it lets you write directly to a typed pixel buffer:

const imgData = ctx.createImageData(width, height)
const data = imgData.data // Uint8ClampedArray, 4 bytes per pixel (R G B A)
// Set pixel at (px, py) to an opaque colour
const i = (py * width + px) * 4
data[i] = r // 0–255
data[i + 1] = g
data[i + 2] = b
data[i + 3] = 255 // alpha
// After filling all pixels:
ctx.putImageData(imgData, 0, 0)

Here is a minimal noise texture using all three concepts:

const { createNoise2D } = require('simplex-noise')
const chroma = require('chroma-js')
function main({ width, height, scale, octaves }) {
const canvas = new OffscreenCanvas(width, height)
const ctx = canvas.getContext('2d')
const noise2D = createNoise2D()
// Pre-compute colour palette for speed
const palette = chroma.scale(['#0a0a2e', '#533483', '#e94560', '#ffd740'])
.mode('lab').colors(256)
const imgData = ctx.createImageData(width, height)
const px_data = imgData.data
for (let py = 0; py < height; py++) {
for (let px = 0; px < width; px++) {
// Multi-octave noise, normalised to [0, 1]
let v = 0, amp = 1, freq = 1, maxAmp = 0
for (let o = 0; o < octaves; o++) {
v += noise2D(px * scale * freq / width,
py * scale * freq / height) * amp
maxAmp += amp; amp *= 0.5; freq *= 2
}
const t = (v / maxAmp + 1) / 2
const [r, g, b] = chroma(palette[Math.floor(t * 255)]).rgb()
const i = (py * width + px) * 4
px_data[i] = r; px_data[i+1] = g; px_data[i+2] = b; px_data[i+3] = 255
}
}
ctx.putImageData(imgData, 0, 0)
return canvas
}

createNoise3D and createNoise4D work the same way with extra coordinates. Use the extra dimension as a “time” parameter to create an animation frame, or as a second independent axis to avoid tiling artefacts:

const { createNoise3D } = require('simplex-noise')
const noise3D = createNoise3D()
// Tiling-free texture: use z to break any periodic symmetry
const v = noise3D(px / width * scale, py / height * scale, 0.5)
  • Study the Noise landscape and Wave interference examples for complete working scripts.
  • Try varying the colour scale palette — the same noise data looks completely different with different colour mappings.