Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / earth-layers.js

Raw 3.5 KB on 2d94b28e8ffdaff52ef49f9af4d2eb2c3a6adb21
1WE.mount('earth-layers', function (S) {
2 var C = S.C, ctx = S.ctx, RE = 6371;
3 var L = [[0, 35, 'Crust', '#8a6f52', 'Solid rock. The thin skin we live on, about 35 km thick under the continents and only 7 km under the oceans.'], [35, 660, 'Upper mantle', '#c2552f', 'Hot rock that flows very slowly, like thick tar over millions of years. It carries the plates of the crust along.'], [660, 2891, 'Lower mantle', '#d9732e', 'Solid but slowly creeping rock under huge pressure. More than half of the whole planet by volume.'], [2891, 5150, 'Outer core', '#f0a63a', 'Liquid iron and nickel. Its swirling makes the Earth’s magnetic field.'], [5150, 6371, 'Inner core', '#ffe08a', 'A solid ball of iron and nickel, about as hot as the surface of the Sun but squeezed solid by pressure.']];
4 var TP = [[0, 15, 0], [35, 600, 1], [660, 1600, 24], [2891, 3700, 136], [5150, 5000, 330], [6371, 5400, 364]];
5 function at(d, k) { for (var i = 1; i < TP.length; i++) if (d <= TP[i][0]) { var a = TP[i - 1], b = TP[i], f = (d - a[0]) / (b[0] - a[0]); return a[k] + (b[k] - a[k]) * f; } return TP[5][k]; }
6 var dep = S.slider('Depth', 0, 6371, 1200, 1, function (v) { return v.toLocaleString() + ' km down'; }), dive = S.toggle('Sink to the center', false);
7 S.pointer({ scroll: true, down: pick, move: function (x, y, d) { if (d) pick(x, y); } });
8 function geo() { var sm = S.small(), w = S.W, h = S.H; return sm ? { cx: w / 2, cy: h * .98, R: Math.min(w * .9, h * .62) } : { cx: w * .36, cy: h * 1.02, R: Math.min(w * .5, h * .9) }; }
9 function pick(x, y) { var g = geo(), r = Math.hypot(x - g.cx, y - g.cy); if (r <= g.R + 10) dep.set(Math.round(Math.max(0, Math.min(RE, (1 - r / g.R) * RE)))); }
10 S.loop(function (dt, t) {
11 if (dive.v) { var nv = dep.v + dt * 500; if (nv >= RE) { nv = RE; dive.set(false); } dep.set(Math.round(nv)); }
12 S.clear(); var g = geo(), d = dep.v, cur = L[0]; L.forEach(function (l) { if (d >= l[0]) cur = l; });
13 L.forEach(function (l) { var r = g.R * (1 - l[0] / RE); ctx.beginPath(); ctx.moveTo(g.cx, g.cy); ctx.arc(g.cx, g.cy, Math.max(r, l[2] === 'Crust' ? r : 0), Math.PI * 1.12, Math.PI * 1.88); ctx.closePath(); ctx.fillStyle = l[3]; ctx.globalAlpha = l === cur ? 1 : .55; ctx.fill(); ctx.globalAlpha = 1; });
14 S.ln(g.cx - 1, g.cy, g.cx + g.R * Math.cos(Math.PI * 1.12), g.cy + g.R * Math.sin(Math.PI * 1.12), C.bg, 2);
15 var pr = g.R * (1 - d / RE), py = g.cy - pr; ctx.setLineDash([3, 4]); S.ln(g.cx, g.cy - g.R, g.cx, py, C.ink, 1.5); ctx.setLineDash([]); S.dot(g.cx, py, 6 + Math.sin(t * 4), '#fff'); S.ring(g.cx, py, 11, C.ink, 1.5);
16 ctx.beginPath(); ctx.arc(g.cx, g.cy, pr, Math.PI * 1.12, Math.PI * 1.88); ctx.strokeStyle = 'rgba(255,255,255,.6)'; ctx.lineWidth = 1; ctx.stroke();
17 var sm = S.small(), tx = sm ? 14 : S.W * .66, ty = sm ? 24 : S.H * .2;
18 S.txt(cur[2], tx, ty, cur[3], sm ? 22 : 30, 'left', 700); S.txt(d.toLocaleString() + ' km below your feet', tx, ty + (sm ? 26 : 36), C.ink, sm ? 13 : 15);
19 S.txt('About ' + (Math.round(at(d, 1) / 10) * 10).toLocaleString() + ' °C', tx, ty + (sm ? 50 : 72), C.red, sm ? 15 : 19, 'left', 650);
20 var gp = at(d, 2); S.txt(gp < 1 ? 'Pressure: up to ' + Math.max(1, Math.round(gp * 10000)).toLocaleString() + ' times the air' : 'Pressure: about ' + (Math.round(gp * 9869 / 1000) * 1000).toLocaleString() + ' times the air', tx, ty + (sm ? 72 : 102), C.blue, sm ? 12.5 : 15);
21 if (!sm) S.txt('Deepest hole ever drilled: 12 km', tx, ty + 140, C.mut, 13);
22 S.say('<b>' + cur[2] + '</b> &nbsp;' + cur[4]);
23 });
24});