Branchwell

Watch Encyclopedia / website / exp / scale.js

1WE.mount('scale', function (S) {
2 var C = S.C, ctx = S.ctx, O = [
3 [1.7e-15, 'Proton', C.red], [1.4e-14, 'Gold nucleus', C.yellow], [1.06e-10, 'Hydrogen atom', C.blue], [2.8e-10, 'Water molecule', C.teal], [2e-9, 'DNA strand, across', C.violet], [1e-7, 'Flu virus', C.green], [7.5e-6, 'Red blood cell', C.red], [7.5e-5, 'Human hair, across', C.acc], [5e-4, 'Grain of sand', C.yellow], [2e-6, 'Bacterium', C.teal], [5e-3, 'Ant', C.mut], [.22, 'Soccer ball', C.ink], [1.7, 'Person', C.pink], [12, 'Bus', C.yellow], [30, 'Blue whale', C.blue], [105, 'Soccer field', C.green], [828, 'Tallest building', C.mut], [8849, 'Mount Everest', C.ink], [2.16e4, 'Manhattan island', C.acc], [4e5, 'Earth to the space station', C.teal], [3.475e6, 'The Moon', '#d8d2c2'], [1.2742e7, 'Earth', C.blue], [1.3982e8, 'Jupiter', '#e0b98c'], [1.3927e9, 'The Sun', '#ffd678'], [3.844e8, 'Earth to the Moon', C.mut], [1.496e11, 'Earth to the Sun', C.acc], [9e12, 'Orbit of Neptune', '#6f8df0'], [9.461e15, 'One light year', C.teal], [4.0e16, 'Distance to the nearest star', C.pink], [9.5e20, 'The Milky Way', C.violet], [2.4e22, 'Distance to the Andromeda galaxy', C.green], [8.8e26, 'The observable universe', C.ink]];
4 var UN = [[1e-15, 'femtometers'], [1e-12, 'picometers'], [1e-9, 'nanometers'], [1e-6, 'micrometers'], [1e-3, 'millimeters'], [1, 'meters'], [1e3, 'kilometers'], [9.461e15, 'light years']];
5 function fmt(m) { var u = UN[0]; UN.forEach(function (q) { if (m >= q[0]) u = q; }); var n = m / u[0]; return (n >= 1e9 ? (n / 1e9).toFixed(1) + ' billion' : n >= 1e6 ? (n / 1e6).toFixed(1) + ' million' : n >= 100 ? Math.round(n).toLocaleString() : n.toPrecision(2)) + ' ' + u[1]; }
6 var ex = S.slider('Zoom', -15.5, 27.5, 0.6, .05, function (v) { return 'screen is ' + fmt(Math.pow(10, v)) + ' wide'; }), auto = S.toggle('Zoom out by itself', false), cur = .6;
7 S.loop(function (dt) {
8 if (auto.v) { var nv = ex.v + dt * .9; if (nv > 27.5) { nv = 27.5; auto.set(false); } ex.set(nv); }
9 cur += (ex.v - cur) * Math.min(1, dt * 8);
10 S.clear(); var w = S.W, h = S.H, view = Math.pow(10, cur), near = null, nd = 99;
11 O.forEach(function (o) {
12 var px = o[0] / view * w, l = Math.log10(o[0]) - cur; if (px < 1.5 || px > w * 40) return;
13 var al = Math.max(0, Math.min(1, px < 30 ? px / 30 : px > w * 1.5 ? 1 - (px - w * 1.5) / (w * 10) : 1)); ctx.globalAlpha = al;
14 S.ring(w / 2, h / 2, px / 2, o[2], px > 60 ? 2 : 1.5); if (px < 14) S.dot(w / 2, h / 2, px / 2, o[2]);
15 if (px > 26 && px < w * 3) { var yy = h / 2 - Math.min(px / 2, h * .46) - 12; S.txt(o[1], w / 2, Math.max(14, yy), o[2], 13.5, 'center', 650); }
16 ctx.globalAlpha = 1; if (Math.abs(l + .45) < nd) { nd = Math.abs(l + .45); near = o; }
17 });
18 var bar = Math.pow(10, Math.floor(cur - .5)), bp = bar / view * w; S.ln(16, h - 20, 16 + bp, h - 20, C.ink, 2); S.ln(16, h - 25, 16, h - 15, C.ink, 2); S.ln(16 + bp, h - 25, 16 + bp, h - 15, C.ink, 2); S.txt(fmt(bar), 22 + bp, h - 20, C.mut, 12);
19 S.txt('10', 16, 28, C.mut, 22, 'left', 600); S.txt(String(Math.round(cur)), 42, 18, C.acc, 14, 'left', 700); S.txt('meters wide', 66, 28, C.mut, 13);
20 S.say(near ? '<b>' + near[1] + '</b> is about ' + fmt(near[0]) + ' across. Each step of the slider makes the view ten times wider.' : 'Slide to zoom from the inside of an atom to the edge of the universe we can see.');
21 });
22});