Mandelbrot set
Mandelbrot Set
Section titled “Mandelbrot Set”The classic fractal rendered using per-pixel iteration counts and a chroma-js colour palette mapped to smooth escape values.
How it works
Section titled “How it works”For each pixel, the complex number c = (px + i·py) is formed from the
mapped canvas coordinate. The iteration z → z² + c is repeated until
|z|² > 4 (escape) or the maximum iteration count is reached (interior).
Smooth colouring avoids harsh banding by computing a fractional escape count using the formula:
t = (iter − log₂(log₂|z|)) / maxIter
which maps to [0, 1] and is looked up in a pre-computed palette array for speed. Interior points (iter === maxIter) are painted black.
ImageData is used for direct pixel writes — calling ctx.fillRect once
per pixel would be far too slow at canvas sizes above ~200 px.
Parameters
Section titled “Parameters”| Parameter | Effect |
|---|---|
| Canvas size | Pixel dimensions (square) |
| Max iterations | Higher = more detail in deep regions |
| Zoom | Magnification level |
| Pan X / Pan Y | Centre of the view in the complex plane |
"use strict";
const chroma = require('chroma-js');
const getParameterDefinitions = () => [ { name: 'size', type: 'number', widget: 'slider', caption: 'Canvas size', default: 700, min: 200, max: 1400 }, { name: 'maxIter', type: 'number', widget: 'slider', caption: 'Max iterations', default: 180, min: 20, max: 800 }, { name: 'zoom', type: 'number', widget: 'slider', caption: 'Zoom', default: 1, min: 0.1, max: 50, step: 0.1 }, { name: 'panX', type: 'number', widget: 'slider', caption: 'Pan X', default: -0.5, min: -2.5, max: 1.5, step: 0.01 }, { name: 'panY', type: 'number', widget: 'slider', caption: 'Pan Y', default: 0, min: -1.5, max: 1.5, step: 0.01 },];
function main({ size, maxIter, zoom, panX, panY }) { var canvas = new OffscreenCanvas(size, size); var ctx = canvas.getContext('2d');
// Pre-compute a 512-entry colour palette for speed var N = 512; var palette = chroma.scale([ '#0d0221', '#240764', '#6b0ac9', '#e94560', '#ff8f00', '#fffde7', ]).mode('lab').colors(N); var palRgb = palette.map(function(h) { return chroma(h).rgb(); });
var imgData = ctx.createImageData(size, size); var data = imgData.data;
// Map pixel → complex plane var scale = 3.5 / zoom / size; // 3.5 spans roughly [-2, 1.5] at zoom=1 var reMin = panX - (size / 2) * scale; var imMin = panY - (size / 2) * scale;
for (var py = 0; py < size; py++) { var im0 = imMin + py * scale; for (var px = 0; px < size; px++) { var re0 = reMin + px * scale;
var zr = 0, zi = 0, iter = 0; while (iter < maxIter) { var zr2 = zr * zr, zi2 = zi * zi; if (zr2 + zi2 > 4) break; zi = 2 * zr * zi + im0; zr = zr2 - zi2 + re0; iter++; }
var i4 = (py * size + px) * 4;
if (iter === maxIter) { // Interior — black data[i4] = 0; data[i4+1] = 0; data[i4+2] = 0; data[i4+3] = 255; } else { // Smooth escape value in [0, 1] var zMod2 = zr * zr + zi * zi; var smooth = iter - Math.log2(Math.log2(zMod2)) + 4; var t = Math.max(0, Math.min(1, smooth / maxIter)); var idx = Math.floor(t * (N - 1)); var rgb = palRgb[idx]; 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 };