Here's an optimized version with dynamic iteration adjustment and smoother rendering:
<!DOCTYPE html>
<html>
<head>
<title>Interactive Mandelbrot Zoom</title>
<style>
body { margin: 0; overflow: hidden; font-family: sans-serif; }
canvas { display: block; cursor: crosshair; }
#ui {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
color: white;
z-index: 1;
}
</style>
</head>
<body>
<div id="ui">
<button id="reset">Reset</button>
<span id="zoomLevel">Zoom: 1x</span>
</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const ui = document.getElementById('ui');
const resetBtn = document.getElementById('reset');
const zoomDisplay = document.getElementById('zoomLevel');
// Set canvas to full screen
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawFractal();
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
let xMin = -2.5, xMax = 1;
let yMin = -1, yMax = 1;
let isDragging = false;
let startX = 0, startY = 0;
let endX = 0, endY = 0;
let originalImageData = null;
// Precompute color palette
const palette = [];
for (let i = 0; i < 360; i++) {
const rgb = hslToRgb(i, 1, 0.5);
palette.push([rgb.r, rgb.g, rgb.b]);
}
function hslToRgb(h, s, l) {
let c = (1 - Math.abs(2*l - 1)) * s;
let x = c * (1 - Math.abs((h/60) % 2 - 1));
let m = l - c/2;
let r = 0, g = 0, b = 0;
if (h < 60) { r = c; g = x; b = 0; }
else if (h < 120) { r = x; g = c; b = 0; }
else if (h < 180) { r = 0; g = c; b = x; }
else if (h < 240) { r = 0; g = x; b = c; }
else if (h < 300) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
return {
r: Math.round((r + m)*255),
g: Math.round((g + m)*255),
b: Math.round((b + m)*255)
};
}
function getZoomFactor() {
const pixelWidth = (xMax - xMin) / canvas.width;
return 1 / pixelWidth;
}
function getIterations() {
const zoom = getZoomFactor();
return Math.min(
10000, // Max iterations
Math.max(
100, // Min iterations
Math.floor(500 * Math.log(zoom) + 100)
)
);
}
function drawFractal() {
const width = canvas.width;
const height = canvas.height;
const imageData = ctx.createImageData(width, height);
const data = imageData.data;
const maxIter = getIterations();
// Calculate zoom level display
zoomDisplay.textContent = `Zoom: ${Math.round(getZoomFactor())}x`;
// Process in chunks for smoother rendering
let row = 0;
function processChunk() {
const endRow = Math.min(row + 10, height);
for (; row < endRow; row++) {
for (let i = 0; i < width; i++) {
const x = xMin + (i / width) * (xMax - xMin);
const y = yMin + (row / height) * (yMax - yMin);
let iteration = 0;
let xTemp, yTemp;
let x2 = 0, y2 = 0;
// Optimized Mandelbrot calculation
while (x2 + y2 <= 4 && iteration < maxIter) {
xTemp = x + x2 - y2;
yTemp = y + 2 * Math.sqrt(x2 + y2); // Optimized using sqrt
x2 = xTemp * xTemp - yTemp * yTemp;
y2 = 2 * xTemp * yTemp;
iteration++;
}
const index = (row * width + i) * 4;
if (iteration === maxIter) {
data[index] = data[index+1] = data[index+2] = 0;
} else {
const color = palette[iteration % 360];
data[index] = color[0];
data[index+1] = color[1];
data[index+2] = color[2];
}
data[index+3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
if (row < height) {
requestAnimationFrame(processChunk);
}
}
requestAnimationFrame(processChunk);
}
// Event handlers
canvas.addEventListener('mousedown', (e) => {
isDragging = true;
startX = e.offsetX;
startY = e.offsetY;
originalImageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
});
canvas.addEventListener('mousemove', (e) => {
if (!isDragging) return;
endX = e.offsetX;
endY = e.offsetY;
ctx.putImageData(originalImageData, 0, 0);
ctx.strokeStyle = 'white';
ctx.lineWidth = 1;
ctx.setLineDash([4, 2]);
ctx.strokeRect(startX, startY, endX - startX, endY - startY);
ctx.setLineDash([]);
});
canvas.addEventListener('mouseup', (e) => {
if (!isDragging) return;
isDragging = false;
endX = e.offsetX;
endY = e.offsetY;
let x0 = Math.min(startX, endX);
let x1 = Math.max(startX, endX);
let y0 = Math.min(startY, endY);
let y1 = Math.max(startY, endY);
if (x0 === x1 || y0 === y1) return;
// Zoom to selected area
xMin = xMin + (x0 / canvas.width) * (xMax - xMin);
xMax = xMin + (x1 / canvas.width) * (xMax - xMin);
yMin = yMin + (y0 / canvas.height) * (yMax - yMin);
yMax = yMin + (y1 / canvas.height) * (yMax - yMin);
drawFractal();
});
resetBtn.addEventListener('click', () => {
xMin = -2.5;
xMax = 1;
yMin = -1;
yMax = 1;
drawFractal();
});
drawFractal();
</script>
</body>
</html>