Here's an HTML file with JavaScript that creates an interactive Mandelbrot set viewer:
html
<!DOCTYPE html>
<html>
<head>
<title>Mandelbrot Set Explorer</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #333;
margin: 20px;
}
.controls {
margin: 10px;
}
button {
padding: 8px 16px;
margin: 0 5px;
background: #444;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #555;
}
</style>
</head>
<body>
<h1>Mandelbrot Set Explorer</h1>
<canvas id="mandelbrotCanvas" width="800" height="600"></canvas>
<div class="controls">
<button onclick="reset()">Reset</button>
<button onclick="zoomIn()">Zoom In</button>
<button onclick="zoomOut()">Zoom Out</button>
</div>
<script>
const canvas = document.getElementById('mandelbrotCanvas');
const ctx = canvas.getContext('2d');
let width = canvas.width;
let height = canvas.height;
let xMin = -2;
let xMax = 1;
let yMin = -1.5;
let yMax = 1.5;
let maxIterations = 100;
function mandelbrot(x0, y0) {
let x = 0;
let y = 0;
let iteration = 0;
while (x*x + y*y <= 4 && iteration < maxIterations) {
let xTemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xTemp;
iteration++;
}
return iteration;
}
function draw() {
const imageData = ctx.createImageData(width, height);
const data = imageData.data;
for (let px = 0; px < width; px++) {
for (let py = 0; py < height; py++) {
const x = xMin + (xMax - xMin) * px / width;
const y = yMin + (yMax - yMin) * py / height;
const iteration = mandelbrot(x, y);
const index = (py * width + px) * 4;
if (iteration === maxIterations) {
data[index] = 0;
data[index + 1] = 0;
data[index + 2] = 0;
} else {
const hue = (iteration / maxIterations) * 360;
const rgb = hslToRgb(hue/360, 1, 0.5);
data[index] = rgb[0];
data[index + 1] = rgb[1];
data[index + 2] = rgb[2];
}
data[index + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0);
}
function hslToRgb(h, s, l) {
let r, g, b;
if (s === 0) {
r = g = b = l;
} else {
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
function zoom(factor) {
const centerX = (xMin + xMax) / 2;
const centerY = (yMin + yMax) / 2;
const rangeX = (xMax - xMin) * factor;
const rangeY = (yMax - yMin) * factor;
xMin = centerX - rangeX / 2;
xMax = centerX + rangeX / 2;
yMin = centerY - rangeY / 2;
yMax = centerY + rangeY / 2;
draw();
}
function zoomIn() {
zoom(0.5);
}
function zoomOut() {
zoom(2);
}
function reset() {
xMin = -2;
xMax = 1;
yMin = -1.5;
yMax = 1.5;
draw();
}
canvas.addEventListener('click', function(event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const centerX = xMin + (xMax - xMin) * x / width;
const centerY = yMin + (yMax - yMin) * y / height;
const rangeX = (xMax - xMin) * 0.5;
const rangeY = (yMax - yMin) * 0.5;
xMin = centerX - rangeX / 2;
xMax = centerX + rangeX / 2;
yMin = centerY - rangeY / 2;
yMax = centerY + rangeY / 2;
draw();
});
// Initial draw
draw();
</script>
</body>
</html>
This code creates an interactive Mandelbrot set viewer with the following features:
Displays the Mandelbrot set in a colorful visualization
Zoom In/Out buttons
Reset button to return to the initial view
Click-to-zoom functionality (click anywhere on the image to zoom in on that point)
Smooth color transitions using HSL color space
Responsive canvas with a dark theme UI
To use it:
Save the code as an HTML file
Open it in a web browser
Click the buttons to zoom in/out or reset
Click directly on interesting areas of the fractal to zoom in on them
The code uses the HTML5 Canvas API for rendering and basic JavaScript for the Mandelbrot set calculations. The coloring is done using a HSL to RGB conversion to create smooth color transitions based on the number of iterations each point takes to escape the set.
Would you like me to explain any particular part of the code in more detail?