Below is a complete, self-contained HTML file.
Save it as, for example, mandelbrot.html, open it in any modern browser, and start zooming with your mouse wheel (or track-pad) and panning by click-dragging. Everything (HTML + CSS + JS) lives in this single file.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Interactive Mandelbrot Zoom – Single File Demo</title>
<style>
html,body{
margin:0;
height:100%;
overflow:hidden;
background:#000;
font-family:system-ui,Arial,Helvetica,sans-serif;
color:#fff;
}
#canvas{
display:block; /* remove scrollbars */
cursor:grab;
}
#canvas:active{
cursor:grabbing;
}
#hud{
position:fixed;
top:8px;
left:10px;
padding:6px 10px;
background:rgba(0,0,0,.55);
border-radius:4px;
font-size:14px;
pointer-events:none; /* let clicks pass through */
white-space:nowrap;
}
a{color:#7ae;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="hud">
Scroll to zoom, drag to pan 🖱️
<br><span id="stats"></span>
</div>
<script>
(() => {
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d",{ willReadFrequently:false });
const stats = document.getElementById("stats");
let W, H, pixelRatio;
// Current view state
let centerX = -0.75,
centerY = 0.0,
scale = 4 / window.innerHeight; // complex-plane units per CSS pixel
const BASE_ITER = 200; // iterations at initial zoom
const ITER_FACTOR = 20; // extra iterations every 2× zoom-in
/* ---------- resize handling ----------- */
function resize(){
pixelRatio = window.devicePixelRatio || 1;
W = canvas.width = Math.round(window.innerWidth * pixelRatio);
H = canvas.height = Math.round(window.innerHeight * pixelRatio);
canvas.style.width = window.innerWidth + "px";
canvas.style.height = window.innerHeight + "px";
render();
}
window.addEventListener("resize", resize);
resize();
/* ---------- rendering core ----------- */
function render(){
const img = ctx.createImageData(W, H);
const data = img.data;
const maxIter = Math.round(BASE_ITER + Math.log2(4/scale) * ITER_FACTOR);
for (let py = 0, idx = 0; py < H; py++){
const y0 = centerY + (py - H/2) * scale / pixelRatio;
for (let px = 0; px < W; px++, idx += 4){
const x0 = centerX + (px - W/2) * scale / pixelRatio;
let x = 0, y = 0, iter = 0;
while (x*x + y*y <= 4 && iter < maxIter){
const xTemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xTemp;
++iter;
}
if (iter === maxIter){
data[idx] = data[idx+1] = data[idx+2] = 0; // inside set = black
}else{
const t = iter / maxIter; // smooth coloring
data[idx] = 9 * (1-t)*t*t*t * 255;
data[idx+1] = 15 * (1-t)*(1-t)*t*t * 255;
data[idx+2] = 8.5 * (1-t)*(1-t)*(1-t)*t * 255;
}
data[idx+3] = 255; // opaque
}
}
ctx.putImageData(img,0,0);
updateHUD(maxIter);
}
function updateHUD(maxIter){
stats.textContent =
`center: (${centerX.toFixed(5)}, ${centerY.toFixed(5)}) `
+ `scale: ${scale.toExponential(2)} `
+ `iter: ${maxIter}`;
}
/* ---------- zoom (scroll) ----------- */
canvas.addEventListener("wheel", e => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
const cx = (e.clientX - rect.left) * pixelRatio;
const cy = (e.clientY - rect.top ) * pixelRatio;
const zoomIn = e.deltaY < 0;
const factor = zoomIn ? 0.8 : 1.25; // < 1 => zoom in
const newScale = scale * factor;
// keep the point under the cursor fixed
centerX += (cx - W/2) * (scale - newScale) / pixelRatio;
centerY += (cy - H/2) * (scale - newScale) / pixelRatio;
scale = newScale;
render();
}, { passive:false });
/* ---------- panning (mouse drag) ----------- */
let drag = false, startX, startY;
canvas.addEventListener("mousedown", e=>{
if (e.button !== 0) return;
drag = true;
startX = e.clientX;
startY = e.clientY;
});
window.addEventListener("mousemove", e=>{
if (!drag) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
centerX -= dx * scale;
centerY -= dy * scale;
startX = e.clientX;
startY = e.clientY;
render();
});
window.addEventListener("mouseup", ()=> drag = false);
canvas.addEventListener("contextmenu", e => e.preventDefault()); // disable right-click menu
})();
</script>
</body>
</html>