Skip to content

Wave interference

Circular waves emanating from N point sources arranged in a ring. Where waves meet, their amplitudes add — producing the classic constructive and destructive interference pattern.

For each pixel, the combined wave amplitude is the sum of cosine waves from every source:

A(px, py) = Σ cos(2π · dist(px,py, sx,sy) / λ − φ)

where dist is the Euclidean distance from the pixel to source (sx, sy), λ is the wavelength, and φ is an optional phase offset.

The result is normalised from [−N, N] to [0, 1] and looked up in a pre-computed chroma-js palette. ImageData writes the colour directly into the pixel buffer for performance.

Parameter Effect
Canvas size Pixel dimensions (square)
Sources Number of point wave sources
Wavelength Distance (px) between successive crests
Phase Phase offset (×π) — animates the pattern
Source radius Distance of sources from canvas centre
"use strict";
const chroma = require('chroma-js');
const getParameterDefinitions = () => [
{ name: 'size', type: 'number', widget: 'slider', caption: 'Canvas size', default: 700, min: 200, max: 1200 },
{ name: 'numSrc', type: 'number', widget: 'slider', caption: 'Sources', default: 5, min: 2, max: 12 },
{ name: 'lambda', type: 'number', widget: 'slider', caption: 'Wavelength (px)', default: 60, min: 10, max: 200, step: 1 },
{ name: 'phase', type: 'number', widget: 'slider', caption: 'Phase (×π)', default: 0, min: 0, max: 2, step: 0.05 },
{ name: 'srcR', type: 'number', widget: 'slider', caption: 'Source radius %', default: 30, min: 5, max: 48, step: 1 },
];
function main({ size, numSrc, lambda, phase, srcR }) {
var canvas = new OffscreenCanvas(size, size);
var ctx = canvas.getContext('2d');
var cx = size / 2, cy = size / 2;
var r = size * srcR / 100;
// Source positions — evenly spaced around a circle
var sources = [];
for (var k = 0; k < numSrc; k++) {
var a = (k / numSrc) * Math.PI * 2;
sources.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
}
// Pre-compute palette
var N = 512;
var palette = chroma.scale([
'#0f3460', '#533483', '#e94560', '#ff8f00', '#fffde7',
]).mode('lab').colors(N);
var palRgb = palette.map(function(h) { return chroma(h).rgb(); });
var phaseRad = phase * Math.PI;
var k2pi = 2 * Math.PI / lambda;
var imgData = ctx.createImageData(size, size);
var data = imgData.data;
for (var py = 0; py < size; py++) {
for (var px = 0; px < size; px++) {
var amp = 0;
for (var s = 0; s < sources.length; s++) {
var dx = px - sources[s][0];
var dy = py - sources[s][1];
var dist = Math.sqrt(dx * dx + dy * dy);
amp += Math.cos(dist * k2pi - phaseRad);
}
// Normalise [-N, N] → [0, 1]
var t = (amp / numSrc + 1) / 2;
var idx = Math.floor(Math.min(t, 0.9999) * N);
var rgb = palRgb[idx];
var i4 = (py * size + 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 };