Mandelbrot Browser Demo: Zoom, Pan, and Black Pixels
Open the Mandelbrot browser demo, click to zoom, and drag to move around. Start at an edge between black and colored regions, zoom in, then use Reset to return to the overview.
The demo assigns a complex number to each pixel and uses iteration to choose its color. The loop has a limit of 100 iterations. Black means that the loop did not detect an escape; it does not prove that the point is in the interior of the set.
Zoom, pan, and Reset
- On a desktop, click a location to make it the new center and zoom in by a factor of two.
- Use the mouse wheel to zoom in or out around the pointer position.
- Drag the canvas to pan. A movement check suppresses click-to-zoom after a drag.
- Press
Resetto restore the center to−0.5 + 0i, the initial range, and the1.0xzoom display.
On a touch screen, drag with one finger to pan and pinch with two fingers to zoom. Pinching changes the range while keeping the current view center. It uses a separate input handler from desktop click-to-zoom.
If you have zoomed into a black region, use Reset and choose an edge between black and color again. The zoom label reports magnification relative to the initial range; it does not certify that the detail is accurate.
What belongs to the Mandelbrot set?
Choose a complex number c and start with z₀ = 0. Keep c fixed as you repeatedly square the previous z and add c.
z₀ = 0
zₙ₊₁ = zₙ² + cThe Mandelbrot set consists of the values of c for which this sequence does not diverge to infinity. The renderer computes c from a screen position. Long Nguyen's explanation gives this definition and the escape criterion: an orbit that leaves the circle of radius two around the origin will diverge.
Why black does not prove membership
The demo stores z = zx + zy·i as separate real and imaginary parts. It tests zx² + zy² > 4 to detect escape beyond radius two. The iteration portion of the current code is:
const MAX_ITER = 100
let zx = 0
let zy = 0
let i = 0
for (; i < MAX_ITER; i++) {
const zx2 = zx * zx
const zy2 = zy * zy
if (zx2 + zy2 > 4) break
zy = 2 * zx * zy + cy
zx = zx2 - zy2 + cx
}Detecting escape in exact arithmetic establishes that c is outside the set. Failing to detect it in a finite loop does not establish that escape will never occur. The current demo returns 100 when the loop reaches its limit and maps that value to black. This finite approximation must be distinguished from the mathematical definition, which concerns an infinite sequence.
The UI has no control for changing the iteration limit. Zooming leaves it at 100, so black pixels near the boundary should not be treated as a complete classification of those points.
Mapping pixels to the complex plane
The renderer uses the image data width pw and height ph, with the shorter dimension as the coordinate scale. centerX and centerY specify the view center; range is the span along the shorter dimension. The implementation uses:
const size = Math.min(pw, ph)
const cx = centerX + ((px - pw / 2) / size) * range
const cy = centerY + ((py - ph / 2) / size) * rangeThe initial center is (-0.5, 0) and the initial range is 3. Zooming reduces range; panning changes the center. In this mapping, cy increases down the screen, so its vertical orientation differs from a complex-plane diagram with an upward imaginary axis.
Colors, progressive rendering, and limits
For escaped points, the renderer uses the iteration count and the magnitude at escape to calculate a fractional value, then maps it to HSL colors. These colors belong to the demo's palette, not to the mathematical set. It then converts HSL to RGB and writes the result to image data.
After an interaction, the renderer works through sampling steps of four pixels, two pixels, and one pixel. It draws coarse blocks before filling in finer detail. This refinement does not increase the iteration limit or the precision of the numbers.
At deep zoom levels, both the fixed iteration limit and coordinate calculations using JavaScript number have limits. The implementation does not use arbitrary-precision arithmetic. It provides no guarantee of accurate detail at a particular magnification or a particular rendering speed or FPS on your device.
How the fern and tree demos differ
The Barnsley Fern demo chooses among four affine transformations according to their probabilities, then moves the previous point to its next position and draws it. Use Preset and the coefficient sliders to change the shape. It does not test each pixel for escape as the Mandelbrot renderer does.
Fractal Tree recursively draws two shorter branches from each branch tip. Moving the pointer horizontally changes the branching angle; moving it vertically changes the branch-length ratio. The current code sets a maximum depth of 11. That is an implementation setting, not a performance limit shared by other devices or renderers.
The controls change different things: view position and range in Mandelbrot, transformation coefficients in the fern, and branching angle and length ratio in the tree. Try each demo to compare these ways of drawing fractal structures.
Further reading
The definition and escape criterion come from Long Nguyen's “The Mandelbrot Set” at UMass Boston. Its 40-iteration demo is separate from this site's 100-iteration implementation. The controls and palette described here follow this site's current code.
Go deeper on this topic
Creative Coding
Mathematical simulations, nature visualization, and generative art in the browser
Article 3 of 13 in this series.
Next reads
Generating Natural Terrain with Perlin Noise — Why Randomness Can Be Beautiful
Step-by-step implementation of natural terrain generation with Perlin noise, from the mathematics of gradient noise to octave composition and 3D mesh rendering on Canvas.
Wave Visualization: 2D Waves, Interference and Chladni Patterns
Try browser demos for 2D waves, interference and Chladni patterns. Learn the source, phase and boundary controls, the calculations behind them, and their limits.
The N-Body Problem in the Browser — Implementing Gravitational Simulation
Implementation details of gravitational calculations, vector operations, integration methods, and orbit rendering used in the NBody, SolarSystem, and GravityWell components.
Use the topic hub as a map, then continue to the next article in the series. New here? Start at the home hub →