-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmandelbrot.html
More file actions
294 lines (266 loc) · 9.94 KB
/
mandelbrot.html
File metadata and controls
294 lines (266 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Explore the Mandelbrot Set fractal interactively in your browser." />
<title>Mandelbrot Set – Fun With Math</title>
<link rel="stylesheet" href="../../css/style.css" />
<style>
.canvas-wrapper { cursor: crosshair; }
canvas { display: block; }
#coords {
font-size: .75rem;
color: var(--text-muted);
font-family: 'Fira Code', Consolas, monospace;
min-height: 1.4em;
padding: .3rem 0 0;
text-align: center;
}
#progress {
height: 3px;
background: var(--accent);
width: 0%;
transition: width .1s;
border-radius: 2px;
}
</style>
</head>
<body>
<nav>
<a class="brand" href="../../index.html">∑ Fun With Math</a>
<a href="../../index.html">← All Experiments</a>
</nav>
<div class="page-header">
<h1>Mandelbrot Set</h1>
<p>An interactive fractal explorer. Scroll to zoom, drag to pan.</p>
</div>
<div class="experiment-layout">
<div>
<div class="canvas-wrapper" id="canvasWrap">
<canvas id="canvas"></canvas>
</div>
<div id="progress"></div>
<p id="coords"> </p>
</div>
<aside class="controls">
<h3>Controls</h3>
<label>
Max Iterations: <span id="iterLabel">200</span>
<input type="range" id="iterRange" min="50" max="2000" step="50" value="200" />
</label>
<label>
Color Scheme
<select id="colorScheme">
<option value="classic">Classic Blue</option>
<option value="fire">Fire</option>
<option value="greyscale">Greyscale</option>
<option value="rainbow">Rainbow</option>
</select>
</label>
<div style="display:flex;gap:.5rem;flex-wrap:wrap">
<button class="btn btn-primary" id="renderBtn">Render</button>
<button class="btn btn-secondary" id="resetBtn">Reset View</button>
</div>
<div class="info-box">
<h4>About</h4>
The Mandelbrot set contains every complex number <code>c</code> for which
the sequence <code>z₀=0, zₙ₊₁=zₙ²+c</code> stays bounded.
Points inside are coloured black; outside points are coloured by escape speed.
</div>
<div class="info-box">
<h4>Navigation</h4>
<ul style="padding-left:1rem;line-height:2">
<li><b>Scroll</b> – zoom in / out</li>
<li><b>Drag</b> – pan around</li>
<li><b>Double-click</b> – zoom to point</li>
</ul>
</div>
</aside>
</div>
<footer>
<p><a href="../../index.html">← Back to all experiments</a></p>
</footer>
<script>
(function () {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const wrap = document.getElementById('canvasWrap');
const iterRange = document.getElementById('iterRange');
const iterLabel = document.getElementById('iterLabel');
const colorSelect = document.getElementById('colorScheme');
const renderBtn = document.getElementById('renderBtn');
const resetBtn = document.getElementById('resetBtn');
const coords = document.getElementById('coords');
const progress = document.getElementById('progress');
// Viewport in complex-plane coordinates
let view = { xMin: -2.5, xMax: 1.0, yMin: -1.25, yMax: 1.25 };
function resize() {
const w = wrap.clientWidth;
const h = Math.round(w * 0.65);
canvas.width = w;
canvas.height = h;
render();
}
// ── Colour palettes ──────────────────────────────────────
function palette(scheme, t) {
// t in [0,1]
switch (scheme) {
case 'fire': {
const r = Math.min(255, Math.round(t * 3 * 255));
const g = Math.min(255, Math.max(0, Math.round((t * 3 - 1) * 255)));
const b = Math.min(255, Math.max(0, Math.round((t * 3 - 2) * 255)));
return [r, g, b];
}
case 'greyscale': {
const v = Math.round(t * 255);
return [v, v, v];
}
case 'rainbow': {
const h6 = t * 6;
const s = Math.floor(h6);
const f = h6 - s;
const q = Math.round((1 - f) * 255);
const p2 = Math.round(f * 255);
const lut = [
[255, p2, 0], [q, 255, 0], [0, 255, p2],
[0, q, 255], [p2, 0, 255],[255, 0, q]
];
return lut[s % 6];
}
default: { // classic blue
const r = Math.round(9 * (1 - t) * t * t * t * 255);
const g = Math.round(15 * (1 - t) * (1 - t) * t * t * 255);
const b = Math.round(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255);
return [r, g, b];
}
}
}
// ── Core rendering (chunked to stay responsive) ──────────
let renderToken = 0;
function render() {
const token = ++renderToken;
const W = canvas.width, H = canvas.height;
const maxIter = parseInt(iterRange.value);
const scheme = colorSelect.value;
const { xMin, xMax, yMin, yMax } = view;
const xRange = xMax - xMin, yRange = yMax - yMin;
const imgData = ctx.createImageData(W, H);
const data = imgData.data;
renderBtn.disabled = true;
progress.style.width = '0%';
const CHUNK = 32; // rows per chunk
let row = 0;
function processChunk() {
if (token !== renderToken) return; // superseded
const endRow = Math.min(row + CHUNK, H);
for (let y = row; y < endRow; y++) {
const ci = yMin + (y / H) * yRange;
for (let x = 0; x < W; x++) {
const cr = xMin + (x / W) * xRange;
let zr = 0, zi = 0, iter = 0;
while (iter < maxIter && zr * zr + zi * zi <= 4) {
const tmp = zr * zr - zi * zi + cr;
zi = 2 * zr * zi + ci;
zr = tmp;
iter++;
}
const idx = (y * W + x) * 4;
if (iter === maxIter) {
data[idx] = data[idx+1] = data[idx+2] = 0;
} else {
const t = Math.sqrt(iter / maxIter);
const [r, g, b] = palette(scheme, t);
data[idx] = r; data[idx+1] = g; data[idx+2] = b;
}
data[idx + 3] = 255;
}
}
ctx.putImageData(imgData, 0, 0);
row = endRow;
progress.style.width = (row / H * 100) + '%';
if (row < H) {
requestAnimationFrame(processChunk);
} else {
renderBtn.disabled = false;
progress.style.width = '100%';
setTimeout(() => { progress.style.width = '0%'; }, 400);
}
}
processChunk();
}
// ── Coordinate utilities ─────────────────────────────────
function toComplex(px, py) {
const { xMin, xMax, yMin, yMax } = view;
return {
re: xMin + (px / canvas.width) * (xMax - xMin),
im: yMin + (py / canvas.height) * (yMax - yMin)
};
}
function zoomAt(px, py, factor) {
const c = toComplex(px, py);
const wHalf = (view.xMax - view.xMin) * factor / 2;
const hHalf = (view.yMax - view.yMin) * factor / 2;
view = {
xMin: c.re - wHalf, xMax: c.re + wHalf,
yMin: c.im - hHalf, yMax: c.im + hHalf
};
render();
}
// ── Scroll to zoom ───────────────────────────────────────
canvas.addEventListener('wheel', e => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
const px = (e.clientX - rect.left) * (canvas.width / rect.width);
const py = (e.clientY - rect.top) * (canvas.height / rect.height);
const factor = e.deltaY > 0 ? 1.3 : 0.77;
zoomAt(px, py, factor);
}, { passive: false });
// ── Double-click zoom ────────────────────────────────────
canvas.addEventListener('dblclick', e => {
const rect = canvas.getBoundingClientRect();
const px = (e.clientX - rect.left) * (canvas.width / rect.width);
const py = (e.clientY - rect.top) * (canvas.height / rect.height);
zoomAt(px, py, 0.5);
});
// ── Drag to pan ──────────────────────────────────────────
let drag = null;
canvas.addEventListener('mousedown', e => {
drag = { x: e.clientX, y: e.clientY, view: { ...view } };
});
window.addEventListener('mousemove', e => {
if (!drag) {
const rect = canvas.getBoundingClientRect();
const px = (e.clientX - rect.left) * (canvas.width / rect.width);
const py = (e.clientY - rect.top) * (canvas.height / rect.height);
if (px >= 0 && px < canvas.width && py >= 0 && py < canvas.height) {
const c = toComplex(px, py);
coords.textContent = `Re: ${c.re.toFixed(6)} Im: ${c.im.toFixed(6)}`;
}
return;
}
const dx = (e.clientX - drag.x) / canvas.getBoundingClientRect().width;
const dy = (e.clientY - drag.y) / canvas.getBoundingClientRect().height;
const dRe = dx * (drag.view.xMax - drag.view.xMin);
const dIm = dy * (drag.view.yMax - drag.view.yMin);
view = {
xMin: drag.view.xMin - dRe, xMax: drag.view.xMax - dRe,
yMin: drag.view.yMin - dIm, yMax: drag.view.yMax - dIm
};
render();
});
window.addEventListener('mouseup', () => { drag = null; });
// ── Buttons ──────────────────────────────────────────────
iterRange.addEventListener('input', () => { iterLabel.textContent = iterRange.value; });
renderBtn.addEventListener('click', render);
resetBtn.addEventListener('click', () => {
view = { xMin: -2.5, xMax: 1.0, yMin: -1.25, yMax: 1.25 };
render();
});
// ── Init ─────────────────────────────────────────────────
new ResizeObserver(resize).observe(wrap);
resize();
})();
</script>
</body>
</html>