Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / mandelbrot.js

Raw 3.1 KB on 2d94b28e8ffdaff52ef49f9af4d2eb2c3a6adb21
1WE.mount('mandelbrot', function (S) {
2 var C = S.C, ctx = S.ctx, cx = -.6, cy = 0, span = 3.2, off = document.createElement('canvas'), octx = off.getContext('2d'), img = null, row = 0, bw = 0, bh = 0, pass = 0, zooms = 0;
3 var HOME = [-.6, 0, 3.2], SPOTS = { home: HOME, valley: [-.7453, .1127, .012], spiral: [-.7436, .1318, .004], mini: [-1.7549, 0, .06], tendril: [.2815, .0085, .03] };
4 S.select('Go to', [['home', 'Whole set'], ['valley', 'Seahorse valley'], ['spiral', 'Spiral'], ['mini', 'A tiny copy'], ['tendril', 'Elephant valley']], 'home', function (v) { var p = SPOTS[v]; cx = p[0]; cy = p[1]; span = p[2]; redo(); });
5 S.button('Zoom out', function () { span = Math.min(3.2, span * 3); redo(); });
6 function redo() { row = 0; pass = 0; }
7 S.onresize = function () { bw = 0; };
8 S.pointer({ scroll: true, cursor: 'zoom-in', down: function (x, y) { var sc = span / S.W; cx += (x - S.W / 2) * sc; cy += (y - S.H / 2) * sc; span = Math.max(2e-13, span / 3); zooms++; redo(); } });
9 function col(n, max, d) { if (n >= max) return [8, 9, 12]; var v = n + 1 - Math.log(Math.log(Math.sqrt(d))) / Math.LN2, t = v * .045; return [Math.round(140 + 115 * Math.cos(t * 2.1 + 3.6)), Math.round(120 + 110 * Math.cos(t * 2.1 + 4.6)), Math.round(110 + 120 * Math.cos(t * 2.1 + 5.9))]; }
10 S.loop(function () {
11 var w = S.W, h = S.H, res = pass === 0 ? 4 : S.dpr > 1 ? 1 : 1, tw = Math.ceil(w / res), thh = Math.ceil(h / res);
12 if (bw !== tw || bh !== thh) { bw = tw; bh = thh; off.width = bw; off.height = bh; img = octx.createImageData(bw, bh); row = 0; }
13 if (row < bh) {
14 var max = Math.min(2000, Math.round(120 + 60 * Math.log(3.2 / span) / Math.LN10 * 2)), sc = span / bw, t0 = performance.now(), d = img.data;
15 while (row < bh && performance.now() - t0 < 14) {
16 var y0 = cy + (row - bh / 2) * sc;
17 for (var i = 0; i < bw; i++) { var x0 = cx + (i - bw / 2) * sc, x = 0, y = 0, n = 0, xx = 0, yy = 0; while (n < max && xx + yy < 64) { y = 2 * x * y + y0; x = xx - yy + x0; xx = x * x; yy = y * y; n++; } var c = col(n, max, xx + yy), o = (row * bw + i) * 4; d[o] = c[0]; d[o + 1] = c[1]; d[o + 2] = c[2]; d[o + 3] = 255; }
18 row++;
19 }
20 octx.putImageData(img, 0, 0, 0, 0, bw, row);
21 if (row >= bh && pass === 0) { ctx.imageSmoothingEnabled = true; ctx.drawImage(off, 0, 0, w, h); pass = 1; bw = 0; return; }
22 }
23 if (pass === 0) { ctx.drawImage(off, 0, 0, bw, row, 0, 0, w, row * res); }
24 else ctx.drawImage(off, 0, 0, bw, row, 0, 0, w, row * res);
25 var mag = 3.2 / span; S.rr(10, 10, 150, 30, 15, 'rgba(13,15,19,.75)'); S.txt('Zoom ' + (mag < 1000 ? mag.toFixed(mag < 10 ? 1 : 0) : mag.toExponential(1).replace('e+', ' × 10^')) + '×', 22, 25, C.ink, 13, 'left', 700);
26 S.say(span < 1e-12 ? 'You have reached the limit of this page’s arithmetic, about a trillion times zoom. The real shape keeps going <b>forever</b>.' : '<b>Tap anywhere to zoom in.</b> One short rule, repeated, draws all of this: take a number, square it, add your starting point, and do it again. Black points stay small forever. Colors show how fast the others fly away. The edge never gets smooth, however deep you go.');
27 });
28});