Skip to content

Lissajous grid

A grid of Lissajous figures — the curves traced when x and y oscillate at different frequencies. Each cell (row i, column j) uses frequency ratio i : j, so the grid shows all integer ratios up to N × N at a glance.

A Lissajous curve is parameterised by:

x(t) = A · sin(a · t + δ) y(t) = B · sin(b · t)

where a and b are the x and y frequencies and δ is a phase offset. When a / b is a simple integer ratio the curve is a closed loop; irrational ratios produce never-closing knots.

d3-path builds each curve by sampling t in [0, 2π] and calling lineTo for each point. Each curve is stroked with a colour drawn from a chroma scale keyed to the diagonal index.

Parameter Effect
Grid size Number of rows / columns (N × N)
Phase δ Phase offset applied to the x oscillator
Steps Number of line segments per curve
Stroke width Width of each curve stroke
Background Canvas fill colour
"use strict";
const { path } = require('d3-path');
const chroma = require('chroma-js');
const getParameterDefinitions = () => [
{ name: 'gridN', type: 'number', widget: 'slider', caption: 'Grid size', default: 5, min: 2, max: 8 },
{ name: 'delta', type: 'number', widget: 'slider', caption: 'Phase δ (π×)', default: 0.5, min: 0, max: 2, step: 0.05 },
{ name: 'steps', type: 'number', widget: 'slider', caption: 'Steps', default: 300, min: 60, max: 800 },
{ name: 'sw', type: 'number', widget: 'slider', caption: 'Stroke width', default: 1.4, min: 0.5, max: 4, step: 0.1 },
{ name: 'bg', type: 'color', caption: 'Background', default: '#0d0d1a' },
];
function main({ gridN, delta, steps, sw, bg }) {
var size = 600;
var margin = 32;
var cell = (size - margin * 2) / gridN;
var r = cell * 0.42;
var phase = delta * Math.PI;
var colors = chroma.scale(['#e94560', '#533483', '#0a9396', '#94d2bd'])
.mode('lab').colors(gridN * 2 - 1);
var pathElems = [];
for (var row = 0; row < gridN; row++) {
var freqY = row + 1;
var cx0 = margin + cell * 0.5;
var cy = margin + cell * (row + 0.5);
for (var col = 0; col < gridN; col++) {
var freqX = col + 1;
var cx = cx0 + cell * col;
var color = colors[row + col]; // diagonal index
var p = path();
var t0 = 0;
var x0 = cx + r * Math.sin(freqX * t0 + phase);
var y0 = cy + r * Math.sin(freqY * t0);
p.moveTo(x0, y0);
for (var i = 1; i <= steps; i++) {
var t = (i / steps) * Math.PI * 2;
var x = cx + r * Math.sin(freqX * t + phase);
var y = cy + r * Math.sin(freqY * t);
p.lineTo(x, y);
}
p.closePath();
pathElems.push(
'<path d="' + p.toString() + '"'
+ ' fill="none" stroke="' + color + '"'
+ ' stroke-width="' + sw + '" stroke-linecap="round"/>'
);
}
}
// Axis frequency labels
var labels = [];
for (var k = 0; k < gridN; k++) {
var freq = k + 1;
var lx = margin + cell * (k + 0.5);
var ly = margin - 10;
labels.push(
'<text x="' + lx.toFixed(1) + '" y="' + ly.toFixed(1) + '"'
+ ' text-anchor="middle" font-family="monospace" font-size="11" fill="#888">'
+ freq + '</text>'
);
var ry = margin + cell * (k + 0.5) + 4;
labels.push(
'<text x="' + (margin - 10).toFixed(1) + '" y="' + ry.toFixed(1) + '"'
+ ' text-anchor="middle" font-family="monospace" font-size="11" fill="#888">'
+ freq + '</text>'
);
}
return '<svg xmlns="http://www.w3.org/2000/svg"'
+ ' width="' + size + '" height="' + size + '"'
+ ' viewBox="0 0 ' + size + ' ' + size + '">'
+ '<rect width="' + size + '" height="' + size + '" fill="' + bg + '"/>'
+ pathElems.join('')
+ labels.join('')
+ '</svg>';
}
module.exports = { main, getParameterDefinitions };