Interactive Mandelbrot Viewer
Here's a complete, interactive Mandelbrot set viewer in a single HTML file. It includes zooming (click to zoom in, right-click to zoom out), panning (drag with mouse), and color scheme controls.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Mandelbrot Viewer</title>
<style>
body {
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
color: #333;
margin-bottom: 10px;
}
.controls {
margin-bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
align-items: center;
}
.control-group {
display: flex;
flex-direction: column;
margin: 0 10px;
}
label {
margin-bottom: 5px;
font-weight: bold;
}
select, input, button {
padding: 8px;
border-radius: 4px;
border: 1px solid #ccc;
}
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
canvas {
border: 1px solid #333;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
cursor: crosshair;
}
.info {
margin-top: 15px;
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<h1>Interactive Mandelbrot Viewer</h1>
<div class="controls">
<div class="control-group">
<label for="colorScheme">Color Scheme:</label>
<select id="colorScheme">
<option value="rainbow">Rainbow</option>
<option value="grayscale">Grayscale</option>
<option value="fire">Fire</option>
<option value="ocean">Ocean</option>
<option value="forest">Forest</option>
</select>
</div>
<div class="control-group">
<label for="maxIterations">Max Iterations:</label>
<input type="number" id="maxIterations" min="10" max="1000" value="100">
</div>
<div class="control-group">
<label for="renderButton">Render:</label>
<button id="renderButton">Render</button>
</div>
<div class="control-group">
<label for="resetButton">Reset View:</label>
<button id="resetButton">Reset</button>
</div>
</div>
<canvas id="mandelbrotCanvas" width="800" height="600"></canvas>
<div class="info">
<p>Click to zoom in. Right-click to zoom out. Drag to pan.</p>
<p id="coordinates">Coordinates: </p>
<p id="zoomInfo">Zoom: 1x</p>
</div>
<script>
// Canvas setup
const canvas = document.getElementById('mandelbrotCanvas');
const ctx = canvas.getContext('2d');
const width = canvas.width;
const height = canvas.height;
// Mandelbrot parameters
let maxIterations = 100;
let colorScheme = 'rainbow';
// View parameters
let centerX = -0.5;
let centerY = 0;
let scale = 2.5;
let zoomFactor = 1.5;
// Mouse interaction
let isDragging = false;
let lastX, lastY;
// UI elements
const colorSchemeSelect = document.getElementById('colorScheme');
const maxIterationsInput = document.getElementById('maxIterations');
const renderButton = document.getElementById('renderButton');
const resetButton = document.getElementById('resetButton');
const coordinatesDisplay = document.getElementById('coordinates');
const zoomInfoDisplay = document.getElementById('zoomInfo');
// Event listeners
colorSchemeSelect.addEventListener('change', () => {
colorScheme = colorSchemeSelect.value;
renderMandelbrot();
});
maxIterationsInput.addEventListener('change', () => {
maxIterations = parseInt(maxIterationsInput.value);
renderMandelbrot();
});
renderButton.addEventListener('click', renderMandelbrot);
resetButton.addEventListener('click', () => {
centerX = -0.5;
centerY = 0;
scale = 2.5;
renderMandelbrot();
updateInfo();
});
canvas.addEventListener('mousedown', (e) => {
if (e.button === 0) { // Left click
isDragging = true;
lastX = e.clientX;
lastY = e.clientY;
}
});
canvas.addEventListener('mouseup', (e) => {
if (e.button === 0) { // Left click
isDragging = false;
}
});
canvas.addEventListener('mousemove', (e) => {
if (isDragging) {
const dx = e.clientX - lastX;
const dy = e.clientY - lastY;
centerX -= dx * scale / width;
centerY -= dy * scale / height;
lastX = e.clientX;
lastY = e.clientY;
renderMandelbrot();
updateInfo();
}
// Update coordinates display
const x = centerX + (e.offsetX - width/2) * scale / width;
const y = centerY + (e.offsetY - height/2) * scale / height;
coordinatesDisplay.textContent = `Coordinates: ${x.toFixed(10)}, ${y.toFixed(10)}`;
});
canvas.addEventListener('click', (e) => {
if (!isDragging) {
// Zoom in
const x = centerX + (e.offsetX - width/2) * scale / width;
const y = centerY + (e.offsetY - height/2) * scale / height;
centerX = x;
centerY = y;
scale /= zoomFactor;
renderMandelbrot();
updateInfo();
}
});
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
// Zoom out
scale *= zoomFactor;
renderMandelbrot();
updateInfo();
});
// Color schemes
const colorSchemes = {
rainbow: (t) => {
const r = Math.min(255, Math.floor(9 * (1-t) * t * t * t * 255));
const g = Math.min(255, Math.floor(15 * (1-t) * (1-t) * t * t * 255));
const b = Math.min(255, Math.floor(8.5 * (1-t) * (1-t) * (1-t) * t * 255));
return [r, g, b];
},
grayscale: (t) => {
const v = Math.floor(255 * t);
return [v, v, v];
},
fire: (t) => {
const r = Math.min(255, Math.floor(255 * t));
const g = Math.min(255, Math.floor(128 * t * t));
const b = Math.min(255, Math.floor(64 * t * t * t));
return [r, g, b];
},
ocean: (t) => {
const r = Math.min(255, Math.floor(64 * t * t));
const g = Math.min(255, Math.floor(128 * t));
const b = Math.min(255, Math.floor(255 * Math.sqrt(t)));
return [r, g, b];
},
forest: (t) => {
const r = Math.min(255, Math.floor(64 * t * t));
const g = Math.min(255, Math.floor(255 * Math.sqrt(t)));
const b = Math.min(255, Math.floor(64 * t));
return [r, g, b];
}
};
// Mandelbrot calculation
function mandelbrot(cx, cy, maxIter) {
let x = 0;
let y = 0;
let iter = 0;
let x2 = 0;
let y2 = 0;
while (x2 + y2 <= 4 && iter < maxIter) {
y = 2 * x * y + cy;
x = x2 - y2 + cx;
x2 = x * x;
y2 = y * y;
iter++;
}
if (iter