Search the documentation

Type to search. Use ↑ and ↓ to move, Enter to open.

Mandelbrot Fractal (web)

A dependency-free browser app that draws the Mandelbrot set in Web Workers, with touch, mouse and keyboard zoom and shareable links.

Screenshot of Mandelbrot Fractal Generator (web)
Status
Builds and runs
Languages
JavaScript, HTML, CSS
Build system
None (opens in a browser)
License
MIT
Upstream
Original work in this repository
Show in graph

Explore

Live demo

The project’s own web app, running in a sandboxed frame.

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:

Bash
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

KeyAction
Launch FractalOpen the fractal fullscreen (448, 896 or 1792 for a higher maximum escape time)
Click / 1-finger tapZoom in 5x, centred on that point
Alt-click / right-click / 2-finger tapZoom out 5x (centred on the point, or about the centre for a tap)
Shift-click / 0 / Home / 3-finger tapShow the whole set again
Esc / 4-finger tapClose the help, then leave fullscreen and return to the launch page
Drag / 1-finger dragMove the view (the picture moves at once, the sharp one follows)
Wheel / pinchZoom about the pointer or the fingers
Shift-drag / right-dragZoom to the rubber-band box
Arrow keysMove the view by 10% (Shift: 50%)
+ / -Zoom in / out 2x about the centre
RBack to the starting view and escape time
SSmooth colours on or off
C / ENext colour scheme / next maximum escape time (Shift: backwards)
PSave the picture as a PNG
LCopy a link to this view
FFullscreen on or off
? / HList 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 x+iyx + iy, the width ww, the maximum escape time mm, smooth colouring s and the scheme p. Only plain decimals are accepted, with the centre within 4 of the origin and the width between 10−1410^{-14} and 100.

The mathematics

Each canvas pixel is a point c=x+iyc = x + iy, with the largest imaginary part at the top. Starting from z0=0z_0 = 0 the app iterates

zn+1=zn2+cz_{n+1} = z_n^2 + c

and records the escape time

n(c)=min⁡(M, min⁡{ n≥1:∣zn∣>2 })n(c) = \min\bigl(M,\ \min\{\, n \ge 1 : |z_n| > 2 \,\}\bigr)

where MM is the maximum escape time. Once ∣zn∣>2|z_n| > 2 the orbit escapes. Points with n(c)=Mn(c) = M 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 q=(x−14)2+y2q = (x - \tfrac14)^2 + y^2 they are

q (q+x−14)≤14y2or(x+1)2+y2≤116,q\,\bigl(q + x - \tfrac14\bigr) \le \tfrac14 y^2 \qquad\text{or}\qquad (x + 1)^2 + y^2 \le \tfrac{1}{16},

Brent’s cycle test catches other interior points: once the orbit returns to within 10−1510^{-15} of its position at the last power-of-two step, it is on an attracting cycle.

Smooth colouring. An escaping point is iterated to ∣zn∣>256|z_n| > 256 and gets

ν=n−log⁡2ln⁡∣zn∣.\nu = n - \log_2 \ln |z_n| .

Colour schemes

The default cube follows the edges of the RGB colour cube through all eight corners:

black→green→cyan→blue→magenta→red→yellow→white\text{black} \to \text{green} \to \text{cyan} \to \text{blue} \to \text{magenta} \to \text{red} \to \text{yellow} \to \text{white}

Each of the 7 segments ramps one channel, with s=M/7s = M/7 escape times per segment and at most 256 levels, so MM is at most 7⋅256=17927 \cdot 256 = 1792, 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 −2.1≤x≤0.8-2.1 \le x \le 0.8, −1.2≤y≤1.2-1.2 \le y \le 1.2, 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 101210^{12} magnification on a 1000 px canvas); zooming out stops at width 100.

How it draws

  1. A canvas of 120,000 pixels or more gets two passes: one sample per 8 × 8 block, then every pixel.
  2. Workers pick up bands of rows as they get free; a new request cancels the queue.
  3. During a gesture the last picture is moved and scaled at once; the new view is computed when it ends.
  4. 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.

JavaScript
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.

OptionEffect
distortion: booltrue keeps rectangles as given; false (default) widens them to fit
pxWidth, pxHeightResize the canvas (1 to 8192 each, at most 36 million pixels), refitting the last requested rectangle
defaults: trueDefault view and maximum escape time, which also become the starting options
resetToDefaultCords, resetCords, resetMaxEscapeTime: trueDefault 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: numberSet MM (clamped to 14-1792, rounded down to a multiple of 7)
palette: namecube, classic, fire, ice, rainbow or grey
smooth: boolSmooth 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: trueRemember the current view and MM as the starting options

Tests

Bash
node --test tests/*.test.js

The unit tests compare 9,000 points with a plain loop. A Playwright smoke test (skipped without Playwright) drives the app in headless Chromium.

Run it yourself

Build
cd Mandelbrot/Fractal-gh
Run
python3 -m http.server 8000
Test
node --test tests/*.test.js

From the repository root, the task runner wraps these commands:

mp.py
python3 scripts/mp.py deps fractal-gh
python3 scripts/mp.py ci fractal-gh
python3 scripts/mp.py run fractal-gh

Connections

Neighbourhood in the knowledge graph

Projects and concepts within two links of this page. Select a node to open it.

Mandelbrot Fractal Generator (web)Mandelbrot setEscape-time algorithmComplex numbersColour mappingMandelbrot ColoringMandelbrot Set ExplorerBuddhabrotChaos PatternsPySpaceFractal geometryBuddhabrotBurning Ship fractalNumber theory and algebraGeometry and computer graphicsColour spacesMonte Carlo methodsChaos EquationsChaos DotterIterated function systemsBarnsley fernChaos gameSierpinski triangleTinkerbell mapStrange attractorsChaos Theory VisualsRay marchingDistance-estimated 3D fractals
ProjectsConceptsDomainsShow in graph →