Skip to content

Spirograph

A parametric hypotrochoid — the curve traced by a pen attached to a small circle rolling inside a larger one. The path is split into colour segments that cycle through the full hue wheel.

The hypotrochoid equations place each point at:

x(t) = (R − r)·cos(t) + d·cos((R − r)/r · t) y(t) = (R − r)·sin(t) − d·sin((R − r)/r · t)

where R is the outer (fixed) circle radius, r is the inner (rolling) circle radius, and d is the pen’s offset from the centre of the rolling circle.

The total angle needed to close the curve is 2π × r / gcd(R, r). Each segment is rendered as its own <path> with a hue drawn from the full spectrum, giving the rainbow banding effect.

Parameter Effect
Size Canvas dimensions in pixels
R Outer circle radius (integer ratio units)
r Inner (rolling) circle radius (integer ratio units)
d Pen offset from rolling circle centre (ratio units)
Segments Number of coloured path segments
"use strict";
const { path } = require('d3-path');
function gcd(a, b) { while (b) { var t = b; b = a % b; a = t; } return a; }
const getParameterDefinitions = () => [
{ name: 'size', type: 'number', widget: 'slider', caption: 'Canvas size', default: 600, min: 300, max: 1200 },
{ name: 'R', type: 'number', widget: 'slider', caption: 'Outer radius (R)', default: 7, min: 2, max: 20, step: 1 },
{ name: 'r', type: 'number', widget: 'slider', caption: 'Inner radius (r)', default: 3, min: 1, max: 10, step: 1 },
{ name: 'd', type: 'number', widget: 'slider', caption: 'Pen offset (d)', default: 5, min: 0.5, max: 15, step: 0.5 },
{ name: 'segments', type: 'number', widget: 'slider', caption: 'Colour segments', default: 120, min: 20, max: 360 },
{ name: 'bg', type: 'color', caption: 'Background', default: '#0d0d1a' },
];
function main({ size, R, r, d, segments, bg }) {
var cx = size / 2, cy = size / 2;
var g = gcd(Math.round(R), Math.round(r));
var loops = Math.round(r) / g; // revolutions to close the curve
var total = Math.PI * 2 * loops;
// Scale so the curve fills roughly 90 % of the canvas
var span = (R - r) + d;
var scale = (size / 2 - 4) / span;
var Rs = R * scale, rs = r * scale, ds = d * scale;
var stepsPerSeg = Math.max(8, Math.ceil(total * 60 / segments));
var paths = [];
for (var seg = 0; seg < segments; seg++) {
var t0 = (seg / segments) * total;
var t1 = ((seg + 1) / segments) * total;
var p = path();
var k0x = cx + (Rs - rs) * Math.cos(t0) + ds * Math.cos((Rs - rs) / rs * t0);
var k0y = cy + (Rs - rs) * Math.sin(t0) - ds * Math.sin((Rs - rs) / rs * t0);
p.moveTo(k0x, k0y);
for (var i = 1; i <= stepsPerSeg; i++) {
var t = t0 + (i / stepsPerSeg) * (t1 - t0);
var x = cx + (Rs - rs) * Math.cos(t) + ds * Math.cos((Rs - rs) / rs * t);
var y = cy + (Rs - rs) * Math.sin(t) - ds * Math.sin((Rs - rs) / rs * t);
p.lineTo(x, y);
}
var hue = Math.round((seg / segments) * 360);
var color = 'hsl(' + hue + ',80%,65%)';
paths.push('<path d="' + p.toString() + '" fill="none" stroke="' + color + '" stroke-width="1.2" />');
}
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 + '"/>'
+ paths.join('')
+ '</svg>';
}
module.exports = { main, getParameterDefinitions };