Here's an interactive Mandelbrot zoom website in a single HTML file with pan/zoom controls and real-time rendering:
<!DOCTYPE html>
<html>
<head>
<title>Mandelbrot Explorer</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0,0,0,0.7);
color: white;
padding: 10px;
border-radius: 5px;
}
input {
width: 100px;
margin: 5px;
background: #333;
color: white;
border: 1px solid #555;
padding: 5px;
}
button {
background: #444;
color: white;
border: none;
padding: 5px 10px;
margin: 5px;
cursor: pointer;
border-radius: 3px;
}
button:hover {
background: #666;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="controls">
<div>
<label>Center X: <input type="text" id="centerX" value="-0.5"></label>
<label>Center Y: <input type="text" id="centerY" value="0"></label>
</div>
<div>
<label>Zoom: <input type="text" id="zoom" value="1">x</label>
<button id="resetBtn">Reset View</button>
</div>
<div>
<label>Max Iterations: <input type="range" id="maxIter" min="10" max="1000" value="100"></label>
<span id="iterValue">100</span>
</div>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let width, height;
let centerX = -0.5, centerY = 0;
let zoom = 1;
let maxIter = 100;
let isDragging = false;
let lastX, lastY;
// Initialize canvas size
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
draw();
}
// Convert pixel coordinates to complex plane
function pixelToComplex(x, y) {
const aspectRatio = width / height;
const range = 4 / zoom;
const minRe = centerX - range/2;
const maxRe = centerX + range/2;
const minIm = centerY - (range / aspectRatio)/2;
const maxIm = centerY + (range / aspectRatio)/2;
const re = minRe + (x / width) * (maxRe - minRe);
const im = minIm + (y / height) * (maxIm - minIm);
return { re, im };
}
// Mandelbrot iteration calculation
function mandelbrot(re, im) {
let zr = 0, zi = 0;
let iter = 0;
while (zr*zr + zi*zi < 4 && iter < maxIter) {
const zr2 = zr*zr - zi*zi + re;
zi = 2*zr*zi + im;
zr = zr2;
iter++;
}
return iter;
}
// Color mapping
function getColor(iter) {
if (iter === maxIter) return [0, 0, 0];
const t = iter / maxIter;
const hue = 260 * t;
const sat = 100;
const light = 50 + 50 * t;
// HSL to RGB conversion
const h = hue / 60;
const s = sat / 100;
const l = light / 100;
const c = (1 - Math.abs(2*l - 1)) * s;
const x = c * (1 - Math.abs((h % 2) - 1));
const m = l - c/2;
let r, g, b;
if (h < 1) { r = c; g = x; b = 0; }
else if (h < 2) { r = x; g = c; b = 0; }
else if (h < 3) { r = 0; g = c; b = x; }
else if (h < 4) { r = 0; g = x; b = c; }
else if (h < 5) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
return [
Math.round((r + m) * 255),
Math.round((g + m) * 255),
Math.round((b + m) * 255)
];
}
// Main drawing function
function draw() {
const imageData = ctx.createImageData(width, height);
const data = imageData.data;
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
const { re, im } = pixelToComplex(x, y);
const iter = mandelbrot(re, im);
const [r, g, b] = getColor(iter);
const i = (y * width + x) * 4;
data[i] = r;
data[i+1] = g;
data[i+2] = b;
data[i+3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
}
// Event Handlers
canvas.addEventListener('mousedown', (e) => {
isDragging = true;
lastX = e.clientX;
lastY = e.clientY;
});
canvas.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const dx = e.clientX - lastX;
const dy = e.clientY - lastY;
lastX = e.clientX;
lastY = e.clientY;
// Convert pixel movement to complex plane
const aspectRatio = width / height;
const range = 4 / zoom;
const deltaX = (dx / width) * range;
const deltaY = (dy / height) * (range / aspectRatio);
centerX -= deltaX;
centerY -= deltaY;
draw();
});
window.addEventListener('mouseup', () => isDragging = false);
canvas.addEventListener('wheel', (e) => {
e.preventDefault();
const zoomFactor = e.deltaY > 0 ? 0.9 : 1.1;
// Get mouse position in complex plane
const rect = canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
const oldPos = pixelToComplex(mouseX, mouseY);
// Update zoom
zoom *= zoomFactor;
// Adjust center to keep mouse position fixed
const newPos = pixelToComplex(mouseX, mouseY);
centerX += oldPos.re - newPos.re;
centerY += oldPos.im - newPos.im;
draw();
});
// Control inputs
document.getElementById('resetBtn').addEventListener('click', () => {
centerX = -0.5;
centerY = 0;
zoom = 1;
maxIter = 100;
document.getElementById('maxIter').value = 100;
document.getElementById('iterValue').textContent = 100;
updateInputs();
draw();
});
document.getElementById('updateBtn').addEventListener('click', updateInputs);
document.getElementById('maxIter').addEventListener('input', (e) => {
maxIter = parseInt(e.target.value);
document.getElementById('iterValue').textContent = maxIter;
draw();
});
function updateInputs() {
document.getElementById('centerX').value = centerX.toFixed(6);
document.getElementById('centerY').value = centerY.toFixed(6);
document.getElementById('zoom').value = zoom.toFixed(2);
}
// Start
window.addEventListener('resize', resize);
resize();
updateInputs();
</script>
</body>
</html>
This implementation includes:
- Real-time interactive zooming with mouse wheel
- Panning by clicking and dragging
- Adjustable maximum iterations for deeper zooms
- Color mapping based on escape time
- Responsive canvas that fills the window
- Input fields to manually set coordinates and zoom level
- Reset button to return to the initial view
Features:
- Smooth color transitions using HSL color space
- Maintains aspect ratio when resizing
- Progressive rendering as you zoom/pan
- High-precision calculations
- Modern dark UI theme
To use:
- Mouse wheel to zoom in/out
- Click and drag to pan
- Use the input fields to manually set coordinates and zoom level
- Adjust the max iterations slider for more detail at higher zoom levels
The initial view shows the full Mandelbrot set. As you zoom deeper, increase the max iterations slider to see more detail. The color scheme cycles through a blue-purple-yellow gradient based on escape velocity.