Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / solar-system.js

Raw 3.5 KB on 2d94b28e8ffdaff52ef49f9af4d2eb2c3a6adb21
1WE.mount('solar-system', function (S) {
2 var C = S.C, P = [
3 ['Mercury', .387, 87.97, '#b5aea3', 4879, 'No moons. A year lasts 88 Earth days.', 3],
4 ['Venus', .723, 224.7, '#e8c98a', 12104, 'No moons. The hottest planet, about 465 °C at the surface.', 5],
5 ['Earth', 1, 365.26, '#6fb1e6', 12742, 'One moon. The only world known to have life.', 5.2],
6 ['Mars', 1.524, 687, '#e8735c', 6779, 'Two small moons. A year lasts 687 Earth days.', 4],
7 ['Jupiter', 5.203, 4332.6, '#e0b98c', 139820, 'About 95 known moons. More massive than all the other planets together.', 11],
8 ['Saturn', 9.537, 10759, '#ecd9a0', 116460, 'More than 270 known moons, and rings of ice and rock.', 9.5],
9 ['Uranus', 19.19, 30687, '#9fe0e0', 50724, 'About 28 known moons. It spins tipped on its side.', 7],
10 ['Neptune', 30.07, 60190, '#6f8df0', 49244, '16 known moons. One trip around the Sun takes 165 Earth years.', 7]];
11 var days = 0, sel = 2, stars = [];
12 for (var i = 0; i < 140; i++) stars.push([Math.random(), Math.random(), Math.random()]);
13 var spd = S.slider('Speed', 0, 100, 38, 1, function (v) { var d = Math.pow(10, v / 25); return d < 30 ? d.toFixed(d < 10 ? 1 : 0) + ' days a second' : d < 700 ? (d / 30.4).toFixed(1) + ' months a second' : (d / 365.26).toFixed(1) + ' years a second'; });
14 var scale = S.select('Distances', [['fit', 'Spread out'], ['true', 'True scale']], 'fit');
15 var trails = S.toggle('Orbits', true);
16 function rad(a) { var R = Math.min(S.W, S.H) / 2 - 16; return scale.v === 'true' ? R * a / 30.07 : R * Math.pow(a, .46) / Math.pow(30.07, .46); }
17 function pos(p) { var ang = -(days / p[2]) * S.TAU - p[1] * 2.3, r = rad(p[1]); return [S.W / 2 + r * Math.cos(ang), S.H / 2 + r * Math.sin(ang)]; }
18 S.pointer({ scroll: true, down: function (x, y) { var best = -1, bd = 30; P.forEach(function (p, i) { var q = pos(p), d = Math.hypot(q[0] - x, q[1] - y); if (d < bd) { bd = d; best = i; } }); if (best >= 0) sel = best; } });
19 S.loop(function (dt) {
20 days += dt * Math.pow(10, spd.v / 25);
21 var ctx = S.ctx; S.clear();
22 stars.forEach(function (s) { S.dot(s[0] * S.W, s[1] * S.H, .5 + s[2] * .8, 'rgba(241,236,226,' + (.15 + s[2] * .35) + ')'); });
23 var g = ctx.createRadialGradient(S.W / 2, S.H / 2, 0, S.W / 2, S.H / 2, 46); g.addColorStop(0, 'rgba(255,214,120,.9)'); g.addColorStop(.25, 'rgba(255,190,90,.35)'); g.addColorStop(1, 'rgba(255,190,90,0)');
24 ctx.fillStyle = g; ctx.fillRect(S.W / 2 - 50, S.H / 2 - 50, 100, 100); S.dot(S.W / 2, S.H / 2, scale.v === 'true' ? 3 : 7, '#ffd678');
25 P.forEach(function (p, i) {
26 if (trails.v) S.ring(S.W / 2, S.H / 2, rad(p[1]), i === sel ? 'rgba(213,163,90,.55)' : 'rgba(255,255,255,.1)', 1);
27 var q = pos(p), r = scale.v === 'true' ? Math.max(2, p[6] * .45) : p[6] * (S.small() ? .7 : 1);
28 if (p[0] === 'Saturn') { ctx.strokeStyle = 'rgba(236,217,160,.7)'; ctx.lineWidth = 2; ctx.beginPath(); ctx.ellipse(q[0], q[1], r * 1.9, r * .6, -.4, 0, S.TAU); ctx.stroke(); }
29 S.dot(q[0], q[1], r, p[3]);
30 if (i === sel) { S.ring(q[0], q[1], r + 5, C.acc, 1.5); S.txt(p[0], q[0], q[1] - r - 14, C.ink, 13, 'center', 650); }
31 });
32 var p = P[sel], yrs = days / 365.26;
33 S.txt(yrs < 2 ? Math.floor(days) + ' days' : yrs.toFixed(1) + ' years', 14, 20, C.mut, 13);
34 S.say('<b>' + p[0] + '</b> &nbsp;' + p[5] + ' ' + p[4].toLocaleString() + ' km wide, ' + (p[1] * 149.6).toFixed(p[1] < 2 ? 1 : 0) + ' million km from the Sun. Sunlight takes ' + (p[1] * 8.317 < 90 ? (p[1] * 8.317).toFixed(1) + ' minutes' : (p[1] * 8.317 / 60).toFixed(1) + ' hours') + ' to arrive. Tap any planet.');
35 });
36});