Branchwell

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

Raw 3.2 KB on e076cd18a5e49bf3de0e63d360b533c7e924e76a
1WE.mount('seasons', function (S) {
2 var C = S.C, ctx = S.ctx, MON = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], ML = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
3 function dstr(n) { var m = 0; n = Math.floor(n); while (n >= ML[m]) { n -= ML[m]; m++; } return MON[m] + ' ' + (n + 1); }
4 var day = S.slider('Day of the year', 0, 364, 171, 1, dstr), lat = S.slider('Latitude', -90, 90, 47, 1, function (v) { return Math.abs(v) + '° ' + (v >= 0 ? 'north' : 'south'); }), play = S.toggle('Play the year', false);
5 S.loop(function (dt) {
6 if (play.v) day.set((day.v + dt * 30) % 365);
7 S.clear(); var w = S.W, h = S.H, sm = S.small(), N = day.v, dec = -23.44 * Math.cos(S.TAU * (N + 10) / 365), dr = dec * Math.PI / 180, ph = lat.v * Math.PI / 180;
8 var c = -Math.tan(ph) * Math.tan(dr), hours = c >= 1 ? 0 : c <= -1 ? 24 : 24 / Math.PI * Math.acos(c), noon = 90 - Math.abs(lat.v - dec);
9 // orbit view
10 var ox = sm ? w / 2 : w * .24, oy = sm ? h * .22 : h * .5, a = sm ? w * .36 : w * .16, b = sm ? h * .13 : h * .3, th = S.TAU * (N - 171) / 365;
11 ctx.strokeStyle = 'rgba(255,255,255,.15)'; ctx.lineWidth = 1; ctx.beginPath(); ctx.ellipse(ox, oy, a, b, 0, 0, S.TAU); ctx.stroke(); S.dot(ox, oy, 12, '#ffd678');
12 [['Jun', 0], ['Sep', .25], ['Dec', .5], ['Mar', .75]].forEach(function (q) { var t = q[1] * S.TAU; S.txt(q[0], ox - (a + 18) * Math.cos(t), oy + (b + 14) * Math.sin(t), C.mut, 11.5, 'center'); });
13 var ex = ox - a * Math.cos(th), ey = oy + b * Math.sin(th); S.dot(ex, ey, 9, C.blue); S.ln(ex - 7, ey + 16, ex + 7, ey - 16, C.ink, 1.5); S.txt('N', ex + 12, ey - 20, C.mut, 10);
14 // close view: sunlight from the left, axis leans toward the Sun by the declination
15 var gx = sm ? w / 2 : w * .76, gy = sm ? h * .66 : h * .5, R = sm ? Math.min(w * .26, h * .2) : Math.min(w * .17, h * .34);
16 for (var i = -2; i <= 2; i++) S.arrow(gx - R * 1.75, gy + i * R * .45, gx - R * 1.2, gy + i * R * .45, 'rgba(240,210,100,.7)', 1.5);
17 S.dot(gx, gy, R, C.blue); ctx.beginPath(); ctx.arc(gx, gy, R, -Math.PI / 2, Math.PI / 2); ctx.fillStyle = '#0f1a2c'; ctx.fill();
18 ctx.save(); ctx.translate(gx, gy); ctx.rotate(-dr); S.ln(0, -R * 1.25, 0, R * 1.25, C.ink, 1.5); S.ln(-R, 0, R, 0, 'rgba(255,255,255,.35)', 1);
19 var ly = -R * Math.sin(ph), lx = R * Math.cos(ph); S.ln(-lx, ly, lx, ly, C.acc, 3); S.dot(-lx, ly, 4, C.acc); ctx.restore();
20 S.txt('Day', gx - R * .5, gy + R + 16, C.mut, 12, 'center'); S.txt('Night', gx + R * .5, gy + R + 16, C.mut, 12, 'center');
21 var tx = sm ? 14 : gx - R, ty = sm ? h * .93 : gy - R - 44; S.txt(hours.toFixed(1) + ' hours of daylight', tx, ty, C.ink, sm ? 16 : 20, 'left', 700);
22 var season = Math.abs(dec) < 8 ? (N < 180 ? ['spring', 'fall'] : ['fall', 'spring']) : dec > 0 ? ['summer', 'winter'] : ['winter', 'summer'];
23 S.say(dstr(N) + ' at ' + Math.abs(lat.v) + '° ' + (lat.v >= 0 ? 'north' : 'south') + ': <b>' + (lat.v >= 0 ? season[0] : season[1]) + '</b>. ' + (hours >= 24 ? 'The Sun never sets.' : hours <= 0 ? 'The Sun never rises.' : 'At noon the Sun stands ' + Math.max(0, noon).toFixed(0) + '° above the horizon.') + ' The gold line is your latitude: the more of it in daylight, the longer your day.');
24 });
25});