Branchwell

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

Raw 4.1 KB on e076cd18a5e49bf3de0e63d360b533c7e924e76a
1WE.mount('spectrum', function (S) {
2 var C = S.C, ctx = S.ctx, B = [[-99, -11, 'Gamma rays', '#b79cf0', 'Made by exploding stars and radioactive atoms. Doctors aim them at tumors.', 'smaller than an atom'], [-11, -8, 'X-rays', '#8fa5f5', 'They pass through skin and muscle but not bone, which is how an X-ray picture works.', 'about the size of an atom'], [-8, -6.42, 'Ultraviolet', '#a47be8', 'Invisible light from the Sun that tans and burns skin. Bees can see it.', 'about the size of a virus'], [-6.42, -6.125, 'Visible light', '#ffffff', 'The narrow band our eyes can see, from violet through green to red.', 'about the size of a bacterium'], [-6.125, -3, 'Infrared', '#e8735c', 'Heat you can feel. Remote controls and night vision cameras use it.', 'from a cell to a pinhead'], [-3, 0, 'Microwaves', '#f0a63a', 'Ovens, radar, Wi-Fi and phone signals all use this band.', 'from a grain of rice to a person'], [0, 99, 'Radio waves', '#7ecb93', 'The longest waves. They carry radio and TV and can bend around hills.', 'from a person to a mountain']];
3 var lg = S.slider('Wavelength', -12, 3, -6.27, .01, function (v) { return fmt(Math.pow(10, v)); });
4 function fmt(m) { var u = [[1e-12, 'pm'], [1e-9, 'nm'], [1e-6, 'µm'], [1e-3, 'mm'], [1, 'm'], [1e3, 'km']], q = u[0]; u.forEach(function (x) { if (m >= x[0]) q = x; }); var n = m / q[0]; return (n >= 100 ? n.toFixed(0) : n >= 10 ? n.toFixed(1) : n.toFixed(2)) + ' ' + q[1]; }
5 function rgb(nm) { var r = 0, g = 0, b = 0; if (nm < 380 || nm > 750) return null; if (nm < 440) { r = (440 - nm) / 60; b = 1; } else if (nm < 490) { g = (nm - 440) / 50; b = 1; } else if (nm < 510) { g = 1; b = (510 - nm) / 20; } else if (nm < 580) { r = (nm - 510) / 70; g = 1; } else if (nm < 645) { r = 1; g = (645 - nm) / 65; } else r = 1; var f = nm < 420 ? .3 + .7 * (nm - 380) / 40 : nm > 700 ? .3 + .7 * (750 - nm) / 50 : 1; return 'rgb(' + Math.round(255 * r * f) + ',' + Math.round(255 * g * f) + ',' + Math.round(255 * b * f) + ')'; }
6 S.pointer({ scroll: true, down: pick, move: function (x, y, d) { if (d) pick(x); } }); function pick(x) { lg.set(Math.max(-12, Math.min(3, -12 + (x - 20) / (S.W - 40) * 15))); }
7 S.loop(function (dt, t) {
8 S.clear(); var w = S.W, h = S.H, v = lg.v, lam = Math.pow(10, v), band = B.filter(function (b) { return v >= b[0] && v < b[1]; })[0] || B[6], nm = lam * 1e9, col = rgb(nm) || band[3], by = h * .78, X = function (e) { return 20 + (e + 12) / 15 * (w - 40); };
9 B.forEach(function (b) { var a = X(Math.max(-12, b[0])), z = X(Math.min(3, b[1])); if (b[2] === 'Visible light') { for (var px = a; px < z; px++) { ctx.fillStyle = rgb(380 + (px - a) / (z - a) * 370); ctx.fillRect(px, by, 1.5, 30); } } else S.rr(a + 1, by, z - a - 2, 30, 3, b === band ? b[3] : 'rgba(255,255,255,.1)'); if (z - a > 62 && !S.small()) S.txt(b[2], (a + z) / 2, by + 46, b === band ? C.ink : C.mut, 11.5, 'center'); });
10 var mx = X(v); S.ln(mx, by - 10, mx, by + 40, C.ink, 2); ctx.fillStyle = C.ink; ctx.beginPath(); ctx.moveTo(mx, by - 4); ctx.lineTo(mx - 6, by - 14); ctx.lineTo(mx + 6, by - 14); ctx.fill();
11 var per = Math.max(10, 16 + (v + 12) / 15 * (w * .55)), wy = h * .38, amp = h * .13; ctx.beginPath(); for (var x = 0; x <= w; x += 2) ctx.lineTo(x, wy + amp * Math.sin((x / per + t * 1.2) * S.TAU)); ctx.strokeStyle = col; ctx.lineWidth = 2.5; ctx.stroke();
12 var f = 299792458 / lam, ev = 1.2398e-6 / lam, fs = f >= 1e18 ? (f / 1e18).toFixed(1) + ' EHz' : f >= 1e15 ? (f / 1e15).toFixed(1) + ' PHz' : f >= 1e12 ? (f / 1e12).toFixed(1) + ' THz' : f >= 1e9 ? (f / 1e9).toFixed(1) + ' GHz' : f >= 1e6 ? (f / 1e6).toFixed(1) + ' MHz' : (f / 1e3).toFixed(0) + ' kHz';
13 S.txt(band[2], 20, 26, col, S.small() ? 22 : 30, 'left', 700); S.txt('Each wave is ' + fmt(lam) + ' long, ' + band[5], 20, 26 + (S.small() ? 26 : 34), C.ink, S.small() ? 12.5 : 15);
14 S.txt(fs + ' · ' + (ev >= 1e6 ? (ev / 1e6).toFixed(1) + ' MeV' : ev >= 1e3 ? (ev / 1e3).toFixed(1) + ' keV' : ev >= .01 ? ev.toFixed(2) + ' eV' : ev.toExponential(1) + ' eV') + ' per photon', 20, h * .62, C.mut, 12.5);
15 S.say('<b>' + band[2] + '</b> &nbsp;' + band[4] + ' It is all the same thing, light, at different wavelengths.');
16 });
17});