Below is a self-contained, single-file Mandelbrot explorer.
• Click-drag to pan
• Scroll (track-pad / mouse-wheel) to zoom at the cursor
• Double-click to zoom-in, Shift + double-click to zoom-out
• Optional controls (iterations & coloring) sit in the top-left corner
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Mandelbrot Explorer — single-file demo</title>
<style>
html,body {height:100%;margin:0;background:#000;font-family:sans-serif;color:#fff;overflow:hidden;}
#toolbar{
position:fixed;top:8px;left:8px;padding:6px 10px;
background:rgba(0,0,0,.6);border-radius:6px;font-size:14px;
backdrop-filter: blur(4px);
user-select:none
}
#toolbar input{width:90px}
a{color:#8cf}
canvas{display:block;width:100%;height:100%}
</style>
</head>
<body>
<div id="toolbar">
Iterations: <input type="range" id="iter" min="50" max="2000" step="50" value="500">
<span id="iterVal">500</span><br>
<label><input type="checkbox" id="smooth" checked> Smooth coloring</label><br>
<small>Scroll = zoom • Drag = pan • Double-click = zoom</small>
</div>
<canvas id="fractal"></canvas>
<script>
(() => {
/* =================== Canvas & resize =================== */
const canvas = document.getElementById('fractal');
const ctx = canvas.getContext('2d');
const dpr = window.devicePixelRatio || 1;
function sizeCanvas(){
canvas.width = innerWidth * dpr;
canvas.height = innerHeight * dpr;
draw();
}
window.addEventListener('resize', sizeCanvas);
sizeCanvas();
/* =================== Viewport State =================== */
let view = { // complex plane rectangle
cx : -0.75, // center x
cy : 0.0, // center y
scale : 3.5 // width of view (height derives from aspect)
};
/* =================== UI elements =================== */
const iterSlider = document.getElementById('iter');
const iterValLbl = document.getElementById('iterVal');
const smoothChk = document.getElementById('smooth');
let maxIter = +iterSlider.value;
iterSlider.oninput = () => { maxIter = +iterSlider.value; iterValLbl.textContent = iterSlider.value; draw();}
smoothChk.onchange = draw;
/* =================== Mouse interaction =================== */
let isDragging = false;
let dragStart = null; // {x,y} in px
let viewStart = null; // copy of view when drag starts
canvas.addEventListener('mousedown', e => {
isDragging = true;
dragStart = {x:e.clientX, y:e.clientY};
viewStart = {...view};
});
window.addEventListener('mousemove', e=>{
if(!isDragging) return;
const dx = (e.clientX - dragStart.x)/canvas.width * viewStart.scale;
const dy = (e.clientY - dragStart.y)/canvas.height * viewStart.scale * canvas.height/canvas.width;
view.cx = viewStart.cx - dx;
view.cy = viewStart.cy + dy;
draw();
});
window.addEventListener('mouseup', ()=> isDragging=false);
/* Zoom via wheel */
canvas.addEventListener('wheel', e=>{
e.preventDefault();
// mouse position -> complex coords
const rect = canvas.getBoundingClientRect();
const px = (e.clientX - rect.left) * dpr;
const py = (e.clientY - rect.top ) * dpr;
const {x: zx, y: zy} = pixelToComplex(px,py);
const zoom = e.deltaY < 0 ? 0.8 : 1.25; // in/out
view.scale *= zoom;
// keep cursor point fixed
view.cx = zx + (view.cx - zx)*zoom;
view.cy = zy + (view.cy - zy)*zoom;
draw();
}, {passive:false});
/* Double-click zoom */
canvas.addEventListener('dblclick', e=>{
const rect = canvas.getBoundingClientRect();
const px = (e.clientX - rect.left) * dpr;
const py = (e.clientY - rect.top ) * dpr;
const {x: zx, y: zy} = pixelToComplex(px,py);
const zoom = e.shiftKey ? 2.0 : 0.5; // shift = out
view.scale *= zoom;
view.cx = zx + (view.cx - zx)*zoom;
view.cy = zy + (view.cy - zy)*zoom;
draw();
});
/* =================== Core math =================== */
function pixelToComplex(px,py){
const aspect = canvas.height/canvas.width;
const x = view.cx + (px/canvas.width - .5)*view.scale;
const y = view.cy + (py/canvas.height-.5)*view.scale*aspect;
return {x,y};
}
function hsvToRgb(h,s,v){
let r,g,b,i=Math.floor(h*6);
let f=h*6 - i;
let p=v*(1-s), q=v*(1-f*s), t=v*(1-(1-f)*s);
switch(i%6){
case 0: r=v,g=t,b=p; break;
case 1: r=q,g=v,b=p; break;
case 2: r=p,g=v,b=t; break;
case 3: r=p,g=q,b=v; break;
case 4: r=t,g=p,b=v; break;
case 5: r=v,g=p,b=q; break;
}
return [r*255,g*255,b*255];
}
/* =================== Render =================== */
let animReq = null;
function draw(){
if(animReq) cancelAnimationFrame(animReq);
animReq = requestAnimationFrame(render);
}
function render(){
const w = canvas.width, h=canvas.height;
const image = ctx.createImageData(w,h);
const data = image.data;
const aspect = h/w;
const scaleX = view.scale;
const scaleY = view.scale*aspect;
for(let py=0; py<h; py++){
const y0 = view.cy + (py/h - .5)*scaleY;
let idx = py*w*4;
for(let px=0; px<w; px++, idx+=4){
const x0 = view.cx + (px/w - .5)*scaleX;
let x=0, y=0, iter=0;
while(x*x + y*y <= 4 && iter < maxIter){
const xt = x*x - y*y + x0;
y = 2*x*y + y0;
x = xt;
iter++;
}
let r,g,b;
if(iter===maxIter){
r=g=b=0;
}else{
if(smoothChk.checked){
// smooth iteration count
const logzn = Math.log(x*x + y*y)/2;
const nu = Math.log(logzn/Math.log(2))/Math.log(2);
iter = iter + 1 - nu;
}
const hue = (0.02*iter) % 1;
[r,g,b] = hsvToRgb(hue, 0.8, 1);
}
data[idx]=r; data[idx+1]=g; data[idx+2]=b; data[idx+3]=255;
}
}
ctx.putImageData(image,0,0);
}
draw();
})();
</script>
</body>
</html>