WE.mount('mandelbrot', function (S) {
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;
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] };
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(); });
S.button('Zoom out', function () { span = Math.min(3.2, span * 3); redo(); });
function redo() { row = 0; pass = 0; }
S.onresize = function () { bw = 0; };
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(); } });
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))]; }
S.loop(function () {
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);
if (bw !== tw || bh !== thh) { bw = tw; bh = thh; off.width = bw; off.height = bh; img = octx.createImageData(bw, bh); row = 0; }
if (row < bh) {
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;
while (row < bh && performance.now() - t0 < 14) {
var y0 = cy + (row - bh / 2) * sc;
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; }
row++;
}
octx.putImageData(img, 0, 0, 0, 0, bw, row);
if (row >= bh && pass === 0) { ctx.imageSmoothingEnabled = true; ctx.drawImage(off, 0, 0, w, h); pass = 1; bw = 0; return; }
}
if (pass === 0) { ctx.drawImage(off, 0, 0, bw, row, 0, 0, w, row * res); }
else ctx.drawImage(off, 0, 0, bw, row, 0, 0, w, row * res);
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);
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 forever.' : 'Tap anywhere to zoom in. 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.');
});
});