<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive Mandelbrot Viewer</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
}
canvas {
display: block;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let width = window.innerWidth;
let height = window.innerHeight;
canvas.width = width;
canvas.height = height;
// Initial view parameters
let centerX = -0.5;
let centerY = 0;
let zoom = 0.3; // Smaller zoom means wider view; adjust for initial fit
let maxIter = 256;
// Dragging state
let isDragging = false;
let startMouseX = 0;
let startMouseY = 0;
let startCenterX = 0;
let startCenterY = 0;
// Compute view bounds
function getBounds() {
const aspect = width / height;
const realWidth = 4 / zoom;
const imagHeight = realWidth / aspect;
const minReal = centerX - realWidth / 2;
const maxReal = centerX + realWidth / 2;
const minImag = centerY - imagHeight / 2;
const maxImag = centerY + imagHeight / 2;
return { minReal, maxReal, minImag, maxImag };
}
// Color function (smooth coloring)
function getColor(iter) {
if (iter === maxIter) return [0, 0, 0];
const t = iter / maxIter;
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];
}
// Render the Mandelbrot set
function render() {
const { minReal, maxReal, minImag, maxImag } = getBounds();
const imgData = ctx.getImageData(0, 0, width, height);
const data = imgData.data;
for (let px = 0; px < width; px++) {
for (let py = 0; py < height; py++) {
const real = minReal + (px / width) * (maxReal - minReal);
const imag = maxImag - (py / height) * (maxImag - minImag); // Top is maxImag, bottom minImag
let x = 0;
let y = 0;
let iter = 0;
while (x * x + y * y <= 4 && iter < maxIter) {
const xt = x * x - y * y + real;
y = 2 * x * y + imag;
x = xt;
iter++;
}
const [r, g, b] = getColor(iter);
const idx = (py * width + px) * 4;
data[idx] = r;
data[idx + 1] = g;
data[idx + 2] = b;
data[idx + 3] = 255;
}
}
ctx.putImageData(imgData, 0, 0);
}
// Handle resize
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
render();
}
// Mouse wheel for zoom
function onWheel(event) {
event.preventDefault();
const factor = event.deltaY < 0 ? 1.2 : 1 / 1.2; // Zoom in/out
const { minReal, maxReal, minImag, maxImag } = getBounds();
// Mouse position in complex plane
const mouseReal = minReal + (event.offsetX / width) * (maxReal - minReal);
const mouseImag = maxImag - (event.offsetY / height) * (maxImag - minImag);
// Adjust center to zoom towards mouse
centerX = mouseReal + (centerX - mouseReal) / factor;
centerY = mouseImag + (centerY - mouseImag) / factor;
// Update zoom
zoom *= factor;
render();
}
// Mouse down
function onMouseDown(event) {
isDragging = true;
startMouseX = event.clientX;
startMouseY = event.clientY;
startCenterX = centerX;
startCenterY = centerY;
canvas.style.cursor = 'grabbing';
}
// Mouse move
function onMouseMove(event) {
if (!isDragging) return;
const deltaX = event.clientX - startMouseX;
const deltaY = event.clientY - startMouseY;
const { minReal, maxReal, minImag, maxImag } = getBounds();
const realDelta = -deltaX * (maxReal - minReal) / width;
const imagDelta = deltaY * (maxImag - minImag) / height; // Sign flip for direction
centerX = startCenterX + realDelta;
centerY = startCenterY + imagDelta;
render();
}
// Mouse up
function onMouseUp() {
isDragging = false;
canvas.style.cursor = 'grab';
}
// Event listeners
window.addEventListener('resize', resize);
canvas.addEventListener('wheel', onWheel);
canvas.addEventListener('mousedown', onMouseDown);
canvas.addEventListener('mousemove', onMouseMove);
canvas.addEventListener('mouseup', onMouseUp);
canvas.addEventListener('mouseleave', onMouseUp); // Stop dragging if mouse leaves
// Initial render
render();
</script>
</body>
</html>