On this page
Explore the Mandelbrot set with mouse, keyboard or fingers. Plain HTML, CSS and JavaScript with Web Workers, and links that share a view.
Launch page and views of the set
Run locally
Serve the Mandelbrot/Fractal-gh folder with any static web server:
cd Mandelbrot/Fractal-gh
python3 -m http.server 8000
# open http://localhost:8000/It also works from a sub-folder or straight from disk (without Web Workers).
Controls
| Key | Action |
|---|---|
| Launch Fractal | Open the fractal fullscreen (448, 896 or 1792 for a higher maximum escape time) |
| Click / 1-finger tap | Zoom in 5x, centred on that point |
| Alt-click / right-click / 2-finger tap | Zoom out 5x (centred on the point, or about the centre for a tap) |
| Shift-click / 0 / Home / 3-finger tap | Show the whole set again |
| Esc / 4-finger tap | Close the help, then leave fullscreen and return to the launch page |
| Drag / 1-finger drag | Move the view (the picture moves at once, the sharp one follows) |
| Wheel / pinch | Zoom about the pointer or the fingers |
| Shift-drag / right-drag | Zoom to the rubber-band box |
| Arrow keys | Move the view by 10% (Shift: 50%) |
| + / - | Zoom in / out 2x about the centre |
| R | Back to the starting view and escape time |
| S | Smooth colours on or off |
| C / E | Next colour scheme / next maximum escape time (Shift: backwards) |
| P | Save the picture as a PNG |
| L | Copy a link to this view |
| F | Fullscreen on or off |
| ? / H | List of keys and gestures |
The maximum escape time is 224 by default.
The address bar carries the view:
#x=-0.74364&y=0.13182&w=0.0001&m=1792&s=1&p=fire
These are the centre , the width , the maximum escape time , smooth colouring s and the scheme p. Only plain decimals are accepted, with the centre within 4 of the origin and the width between and 100.
The mathematics
Each canvas pixel is a point , with the largest imaginary part at the top. Starting from the app iterates
and records the escape time
where is the maximum escape time. Once the orbit escapes. Points with are drawn dark green; escape times of 2 or less are black.
Shortcuts for the inside. The main cardioid and the period-2 disc never escape. With they are
Brent’s cycle test catches other interior points: once the orbit returns to within of its position at the last power-of-two step, it is on an attracting cycle.
Smooth colouring. An escaping point is iterated to and gets
Colour schemes
The default cube follows the edges of the RGB colour cube through all eight corners:
Each of the 7 segments ramps one channel, with escape times per segment and at most 256 levels, so is at most , at least 14, and rounded down to a multiple of 7.
fire, ice, rainbow and grey are cyclic gradients with periods of 64, 64, 48 and 32 escape times.
View and limits
The default view is , , widened along one axis to match the canvas. Zooming in stops when a pixel would be narrower than 16 units in the last place (roughly magnification on a 1000 px canvas); zooming out stops at width 100.
How it draws
- A canvas of 120,000 pixels or more gets two passes: one sample per 8 × 8 block, then every pixel.
- Workers pick up bands of rows as they get free; a new request cancels the queue.
- During a gesture the last picture is moved and scaled at once; the new view is computed when it ends.
- Without workers the same bands run on the page, one per timer tick.
Using the renderer
js/engine.js holds the numbers, js/mandelbrot.js is the renderer, js/progressive.js and js/worker.js draw in the background, js/viewstate.js handles the address.
const fractal = new mandelbrot.Fractal(canvas); // also available as the global Fractal
fractal.update({ pxWidth: 1600, pxHeight: 1000, maxEscapeTime: 448 }); // true if it redrew
// draw in the background instead of on the calling thread
const renderer = new mandelbrot.ProgressiveRenderer();
const background = new mandelbrot.Fractal(canvas, { renderer });update(options) applies the options it is given, in this order, and redraws once.
| Option | Effect |
|---|---|
distortion: bool | true keeps rectangles as given; false (default) widens them to fit |
pxWidth, pxHeight | Resize the canvas (1 to 8192 each, at most 36 million pixels), refitting the last requested rectangle |
defaults: true | Default view and maximum escape time, which also become the starting options |
resetToDefaultCords, resetCords, resetMaxEscapeTime: true | Default view / starting view / starting maximum escape time |
cords: {xCartMin, xCartMax, yCartMin, yCartMax} | Show this rectangle of the plane (non-finite or empty rectangles are ignored) |
maxEscapeTime: number | Set (clamped to 14-1792, rounded down to a multiple of 7) |
palette: name | cube, classic, fire, ice, rainbow or grey |
smooth: bool | Smooth colouring (default true) |
zoomInPxPoint / zoomOutPxPoint: {xPx, yPx} | Zoom in / out 5x, centred on the plane point under that canvas position |
zoomInPxBox: {xPxMin, xPxMax, yPxMin, yPxMax} | Show the plane rectangle under that canvas box |
setAsStartingOptions: true | Remember the current view and as the starting options |
Tests
node --test tests/*.test.jsThe unit tests compare 9,000 points with a plain loop. A Playwright smoke test (skipped without Playwright) drives the app in headless Chromium.





