simplex-noise
Simplex noise — a smooth, gradient-based coherent noise algorithm. Faster than classic Perlin noise and with fewer directional artefacts. Accepts an optional seeded random function so the same input always produces the same noise field.
Returned noise values are in the range [-1, 1]. Normalise to [0, 1]
with (value + 1) / 2 before mapping to colours or heights.
See also:
createNoise2D
Section titled “createNoise2D”function createNoise2D(random?: RandomFn): (x: number, y: number) => numberCreate a 2D simplex noise function seeded from random.
The returned function maps any (x, y) coordinate to a smooth noise value in [-1, 1]. Identical inputs always produce identical outputs for a given seed. Scale the inputs to control feature size — smaller values produce broader, smoother features; larger values produce finer detail.
Stack multiple calls at increasing frequencies and decreasing amplitudes (fractal Brownian motion / fBm) to add realistic surface detail:
var v = 0, amp = 1, freq = 1, maxAmp = 0;for (var o = 0; o < octaves; o++) { v += noise2D(x * freq, y * freq) * amp; maxAmp += amp; amp *= 0.5; freq *= 2;}var t = (v / maxAmp + 1) / 2; // normalise to [0, 1]Parameters:
| Name | Type | Description |
|---|---|---|
random |
— | Optional seeded PRNG. Defaults to Math.random (non-reproducible). |
Returns: A function (x, y) => number returning noise values in [-1, 1].
const { createNoise2D } = require('simplex-noise');async function main({ width, height }) { var canvas = new OffscreenCanvas(width, height); var ctx = canvas.getContext('2d'); var noise2D = createNoise2D(); var imgData = ctx.createImageData(width, height); var data = imgData.data; for (var py = 0; py < height; py++) { for (var px = 0; px < width; px++) { var v = (noise2D(px / 80, py / 80) + 1) / 2; // normalise to [0, 1] var c = Math.round(v * 255); var i4 = (py * width + px) * 4; data[i4] = data[i4+1] = data[i4+2] = c; data[i4+3] = 255; } } ctx.putImageData(imgData, 0, 0); return canvas;}See also:
createNoise3D
Section titled “createNoise3D”function createNoise3D(random?: RandomFn): (x: number, y: number, z: number) => numberCreate a 3D simplex noise function seeded from random.
The returned function maps any (x, y, z) coordinate to a smooth noise value in [-1, 1]. Using the third dimension as a time coordinate produces a smoothly animated 2D noise field without seams:
var noise3D = createNoise3D();// inside an animation loop, advance z each frame:var v = (noise3D(px / 80, py / 80, frameIndex * 0.01) + 1) / 2;Parameters:
| Name | Type | Description |
|---|---|---|
random |
— | Optional seeded PRNG. Defaults to Math.random (non-reproducible). |
Returns: A function (x, y, z) => number returning noise values in [-1, 1].
const { createNoise3D } = require('simplex-noise');async function main({ width, height, t }) { var canvas = new OffscreenCanvas(width, height); var ctx = canvas.getContext('2d'); var noise3D = createNoise3D(); var imgData = ctx.createImageData(width, height); var data = imgData.data; for (var py = 0; py < height; py++) { for (var px = 0; px < width; px++) { var v = (noise3D(px / 80, py / 80, t * 0.01) + 1) / 2; var c = Math.round(v * 255); var i4 = (py * width + px) * 4; data[i4] = data[i4+1] = data[i4+2] = c; data[i4+3] = 255; } } ctx.putImageData(imgData, 0, 0); return canvas;}See also:
createNoise4D
Section titled “createNoise4D”function createNoise4D(random?: RandomFn): (x: number, y: number, z: number, w: number) => numberCreate a 4D simplex noise function seeded from random.
The returned function maps any (x, y, z, w) coordinate to a smooth noise value in [-1, 1]. The fourth dimension can be used as a second time axis, or to create seamlessly tileable noise by mapping (z, w) to a circle:
var noise4D = createNoise4D();// Tileable 2D noise — wrap the x axis onto a circle in (z, w):var angle = (px / width) * Math.PI * 2;var v = noise4D(px / 80, py / 80, Math.cos(angle), Math.sin(angle));Parameters:
| Name | Type | Description |
|---|---|---|
random |
— | Optional seeded PRNG. Defaults to Math.random (non-reproducible). |
Returns: A function (x, y, z, w) => number returning noise values in [-1, 1].
const { createNoise4D } = require('simplex-noise');async function main({ width, height }) { var canvas = new OffscreenCanvas(width, height); var ctx = canvas.getContext('2d'); var noise4D = createNoise4D(); var imgData = ctx.createImageData(width, height); var data = imgData.data; for (var py = 0; py < height; py++) { for (var px = 0; px < width; px++) { // Tile horizontally by mapping x onto a circle in (z, w) var angle = (px / width) * Math.PI * 2; var v = (noise4D(px / 80, py / 80, Math.cos(angle), Math.sin(angle)) + 1) / 2; var c = Math.round(v * 255); var i4 = (py * width + px) * 4; data[i4] = data[i4+1] = data[i4+2] = c; data[i4+3] = 255; } } ctx.putImageData(imgData, 0, 0); return canvas;}See also: