Watch Encyclopedia / website · watchencyclopedia.org / exp / tides.js
| 1 | WE.mount('tides', function (S) { |
| 2 | var C = S.C, ctx = S.ctx, days = 3.2, hist = []; |
| 3 | var spd = S.slider('Speed', 0, 4, 1.2, .1, function (v) { return v.toFixed(1) + ' days a second'; }), sun = S.toggle('Include the Sun', true); |
| 4 | function height(th, tm) { return Math.cos(2 * (th - tm)) + (sun.v ? .46 * Math.cos(2 * (th - Math.PI)) : 0); } |
| 5 | S.loop(function (dt) { |
| 6 | days += dt * spd.v; S.clear(); var w = S.W, h = S.H, sm = S.small(), cx = sm ? w / 2 : w * .36, cy = sm ? h * .36 : h * .46, R = Math.min(w, h) * (sm ? .15 : .17), tm = Math.PI + days / 29.53 * S.TAU, te = days * S.TAU, MR = R * 2.5; |
| 7 | for (var i = -2; i <= 2; i++) S.arrow(6, cy + i * R * .6, 6 + Math.min(40, w * .05), cy + i * R * .6, sun.v ? 'rgba(240,210,100,.7)' : 'rgba(240,210,100,.2)', 1.5); |
| 8 | S.txt('Sun', 8, cy - R * 1.7, C.yellow, 12); |
| 9 | ctx.beginPath(); for (var a = 0; a <= 360; a += 3) { var th = a * Math.PI / 180, r = R * (1.16 + .13 * height(th, tm)); ctx.lineTo(cx + r * Math.cos(th), cy - r * Math.sin(th)); } ctx.closePath(); ctx.fillStyle = 'rgba(64,140,210,.55)'; ctx.fill(); |
| 10 | S.dot(cx, cy, R, '#3d6b4f'); S.ring(cx, cy, R, '#27402f', 2); |
| 11 | var px = cx + R * Math.cos(te), py = cy - R * Math.sin(te); S.dot(px, py, 5, C.red); S.ln(px, py, cx + R * 1.5 * Math.cos(te), cy - R * 1.5 * Math.sin(te), C.red, 1); |
| 12 | S.ring(cx, cy, MR, 'rgba(255,255,255,.12)', 1); var mx = cx + MR * Math.cos(tm), my = cy - MR * Math.sin(tm); S.dot(mx, my, R * .27, '#d8d2c2'); S.txt('Moon', mx, my - R * .27 - 12, C.mut, 12, 'center'); |
| 13 | var hv = height(te, tm); hist.push(hv); if (hist.length > 360) hist.shift(); |
| 14 | var gx = sm ? 16 : w * .68, gw = sm ? w - 32 : w * .28, gy = sm ? h * .82 : h * .46, gh = sm ? h * .1 : h * .2; |
| 15 | S.ln(gx, gy, gx + gw, gy, C.grid, 1); ctx.beginPath(); hist.forEach(function (v, i) { ctx.lineTo(gx + gw * i / 360, gy - v / 1.46 * gh); }); ctx.strokeStyle = C.blue; ctx.lineWidth = 2; ctx.stroke(); |
| 16 | S.txt('Water level at the red marker', gx, gy - gh - 16, C.mut, 12.5); S.txt('high', gx + gw + 4, gy - gh, C.mut, 10.5); S.txt('low', gx + gw + 4, gy + gh, C.mut, 10.5); |
| 17 | var el = ((tm - Math.PI) % S.TAU + S.TAU) % S.TAU, q = Math.abs(Math.cos(el)), kind = !sun.v ? 'Moon only' : q > .92 ? 'Spring tides: Sun and Moon pull in line, so highs are highest' : q < .38 ? 'Neap tides: Sun and Moon pull at right angles, so the range is smallest' : 'In between spring and neap tides'; |
| 18 | S.say('<b>' + kind + '.</b> The Moon raises a bulge of water on the near side and another on the far side. As the Earth turns, the marker passes through both, so most coasts get two high tides a day. The bulge is drawn far bigger than life.'); |
| 19 | }); |
| 20 | }); |