Skip to content

Noise landscape

A top-down terrain map generated from multi-octave fractal Brownian motion (fBm) noise, coloured with a geographically-inspired chroma-js scale.

createNoise2D from simplex-noise returns smooth pseudo-random values for any 2-D coordinate. Layering several octaves — each at double the previous frequency and half the amplitude — builds up large-scale shapes with fine surface detail on top.

A seeded PRNG (alea-style LCG) is passed to createNoise2D so the same seed always produces the same landscape. The noise value is normalised to [0, 1] and mapped to a terrain colour scale with domain stops that mimic real elevation zones (deep water → shallow water → beach → lowland → mountain → snow).

ImageData writes pixels directly into the canvas buffer — one typed-array write per pixel is far faster than one fillRect call.

Parameter Effect
Width / Height Canvas pixel dimensions
Scale Zoom level — smaller = larger features
Octaves Detail layers; more = finer texture
Seed Random seed for the noise function
"use strict";
const { createNoise2D } = require('simplex-noise');
const chroma = require('chroma-js');
const getParameterDefinitions = () => [
{ name: 'width', type: 'number', widget: 'slider', caption: 'Width', default: 800, min: 200, max: 1600 },
{ name: 'height', type: 'number', widget: 'slider', caption: 'Height', default: 500, min: 200, max: 1000 },
{ name: 'scale', type: 'number', widget: 'slider', caption: 'Scale', default: 3.5, min: 0.5, max: 12, step: 0.1 },
{ name: 'octaves', type: 'number', widget: 'slider', caption: 'Octaves', default: 6, min: 1, max: 10 },
{ name: 'seed', type: 'number', widget: 'slider', caption: 'Seed', default: 42, min: 1, max: 9999 },
];
function seededPrng(seed) {
var s = (seed % 2147483647 + 2147483647) % 2147483647;
return function() { s = s * 16807 % 2147483647; return (s - 1) / 2147483646; };
}
function main({ width, height, scale, octaves, seed }) {
var canvas = new OffscreenCanvas(width, height);
var ctx = canvas.getContext('2d');
var noise2D = createNoise2D(seededPrng(seed));
// Terrain colour scale with elevation-zone domain stops
var terrain = chroma.scale([
'#0a2e52', // deep water
'#1a6091', // shallow water
'#c8b96e', // beach / sand
'#5a9e48', // lowland grass
'#6e5537', // mountain
'#ffffff', // snow
]).domain([0, 0.32, 0.38, 0.52, 0.78, 1.0]).mode('lab');
// Pre-compute palette for speed
var N = 512;
var pal = [];
for (var k = 0; k < N; k++) {
pal.push(terrain(k / (N - 1)).rgb());
}
var imgData = ctx.createImageData(width, height);
var data = imgData.data;
for (var py = 0; py < height; py++) {
for (var px = 0; px < width; px++) {
// fBm — sum octaves
var nx = px * scale / width;
var ny = py * scale / height;
var v = 0, amp = 1, freq = 1, maxAmp = 0;
for (var o = 0; o < octaves; o++) {
v += noise2D(nx * freq, ny * freq) * amp;
maxAmp += amp;
amp *= 0.5;
freq *= 2;
}
var t = (v / maxAmp + 1) / 2; // normalise to [0, 1]
var idx = Math.floor(Math.min(t, 0.9999) * N);
var rgb = pal[idx];
var i4 = (py * width + px) * 4;
data[i4] = rgb[0];
data[i4 + 1] = rgb[1];
data[i4 + 2] = rgb[2];
data[i4 + 3] = 255;
}
}
ctx.putImageData(imgData, 0, 0);
return canvas;
}
module.exports = { main, getParameterDefinitions };