Here's a complete all-in-one HTML + JavaScript file that renders the Mandelbrot set using the Canvas API. It includes interactive controls to zoom in/out, pan, and reset the view.
✅ Features:
- Zoom in/out using mouse scroll or zoom buttons
- Click and drag to pan the view
- Reset button to return to the original view
- High-performance rendering using
ImageData
- Easily customizable
📄 The Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mandelbrot Set Viewer</title>
<style>
html, body {
margin: 0;
overflow: hidden;
background: #000;
color: #fff;
font-family: sans-serif;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0,0,0,0.6);
padding: 10px;
border-radius: 5px;
z-index: 10;
}
canvas {
display: block;
}
button {
margin: 2px;
}
</style>
</head>
<body>
<div id="controls">
<button id="zoomIn">Zoom In +</button>
<button id="zoomOut">Zoom Out −</button>
<button id="resetView">Reset</button>
</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let width, height;
// View parameters
let minX = -2.5, maxX = 1;
let minY = -1.5, maxY = 1.5;
const maxIterations = 100;
// Resize canvas to fill window
function resizeCanvas() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
drawMandelbrot();
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Convert pixel to complex plane coordinate
function pixelToComplex(x, y) {
return {
re: minX + (x / width) * (maxX - minX),
im: minY + (y / height) * (maxY - minY)
};
}
// Compute color based on iterations
function getColor(iter) {
if (iter === maxIterations) return [0, 0, 0];
const t = iter / maxIterations;
const r = Math.floor(9 * (1 - t) * t * t * t * 255);
const g = Math.floor(15 * (1 - t) * (1 - t) * t * t * 255);
const b = Math.floor(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255);
return [r, g, b];
}
function drawMandelbrot() {
const img = ctx.createImageData(width, height);
for (let py = 0; py < height; py++) {
for (let px = 0; px < width; px++) {
const { re: x0, im: y0 } = pixelToComplex(px, py);
let x = 0, y = 0, iter = 0;
while (x*x + y*y <= 4 && iter < maxIterations) {
const xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
iter++;
}
const [r, g, b] = getColor(iter);
const idx = (py * width + px) * 4;
img.data[idx] = r;
img.data[idx+1] = g;
img.data[idx+2] = b;
img.data[idx+3] = 255;
}
}
ctx.putImageData(img, 0, 0);
}
// Zoom functionality
function zoom(factor) {
const centerRe = (minX + maxX) / 2;
const centerIm = (minY + maxY) / 2;
const rangeRe = (maxX - minX) * factor / 2;
const rangeIm = (maxY - minY) * factor / 2;
minX = centerRe - rangeRe;
maxX = centerRe + rangeRe;
minY = centerIm - rangeIm;
maxY = centerIm + rangeIm;
drawMandelbrot();
}
// Reset view
function resetView() {
minX = -2.5; maxX = 1;
minY = -1.5; maxY = 1.5;
drawMandelbrot();
}
document.getElementById('zoomIn').addEventListener('click', () => zoom(0.5));
document.getElementById('zoomOut').addEventListener('click', () => zoom(2));
document.getElementById('resetView').addEventListener('click', resetView);
// Mouse dragging for pan
let isDragging = false;
let startDrag = { x: 0, y: 0 };
let startView = {};
canvas.addEventListener('mousedown', (e) => {
isDragging = true;
startDrag = { x: e.clientX, y: e.clientY };
startView = { minX, maxX, minY, maxY };
});
canvas.addEventListener('mouseup', () => {
isDragging = false;
});
canvas.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const dx = e.clientX - startDrag.x;
const dy = e.clientY - startDrag.y;
const deltaX = (startView.maxX - startView.minX) * dx / width;
const deltaY = (startView.maxY - startView.minY) * dy / height;
minX = startView.minX - deltaX;
maxX = startView.maxX - deltaX;
minY = startView.minY - deltaY;
maxY = startView.maxY - deltaY;
drawMandelbrot();
});
// Scroll to zoom at pointer
canvas.addEventListener('wheel', (e) => {
e.preventDefault();
const zoomFactor = e.deltaY > 0 ? 1.2 : 0.8;
const mouseX = e.offsetX;
const mouseY = e.offsetY;
const center = pixelToComplex(mouseX, mouseY);
const rangeRe = (maxX - minX) * zoomFactor;
const rangeIm = (maxY - minY) * zoomFactor;
minX = center.re - (mouseX / width) * rangeRe;
maxX = minX + rangeRe;
minY = center.im - (mouseY / height) * rangeIm;
maxY = minY + rangeIm;
drawMandelbrot();
});
</script>
</body>
</html>
🧪 How to Use
- Save the code as an
.html file (e.g., mandelbrot.html)
- Open it in a modern web browser like Chrome or Firefox
- Use:
- Mouse wheel or Zoom In/Out buttons to zoom
- Click and drag with the mouse to pan
- Click Reset to go back to the initial view
💡 Note
- The performance may vary on high resolutions or devices with lower GPU power because Mandelbrot computation is CPU-bound in this version.
- You can increase
maxIterations for more detail at deeper zoom levels, but it will be slower.
Would you like a WebGL version for faster rendering or deeper color gradients?