Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / watch-history.js

Raw 4.3 KB on 2d94b28e8ffdaff52ef49f9af4d2eb2c3a6adb21
1WE.mount('watch-history', function (S) {
2 var C = S.C, ctx = S.ctx, ev = [].map.call(document.querySelectorAll('.tl li'), function (li) { var y = li.querySelector('.yr').textContent, n = parseInt((y.match(/\d{3,4}/) || [0])[0], 10); return { label: y, year: n, text: li.textContent.slice(y.length) }; }).filter(function (e) { return e.year; });
3 if (!ev.length) return; var idx = 0, y0 = ev[0].year, y1 = ev[ev.length - 1].year;
4 var sl = S.slider('Travel through time', 0, ev.length - 1, 0, 1, function (v) { return ev[v].label; }, function (v) { idx = v; });
5 S.button('Earlier', function () { idx = Math.max(0, idx - 1); sl.set(idx); }); S.button('Later', function () { idx = Math.min(ev.length - 1, idx + 1); sl.set(idx); });
6 var X = function (yr) { return 30 + (yr - y0) / (y1 - y0) * (S.W - 60); };
7 S.pointer({ scroll: true, down: function (x) { var b = 0, bd = 1e9; ev.forEach(function (e, i) { var d = Math.abs(X(e.year) - x); if (d < bd) { bd = d; b = i; } }); idx = b; sl.set(b); } });
8 function device(x, y, r, yr, t) {
9 ctx.lineWidth = 3; ctx.strokeStyle = C.ink; var a = t * .8;
10 if (yr < 1500) { S.rr(x - r * .55, y - r, r * 1.1, r * 2, 4, '#1b2029', C.mut); S.ring(x, y - r * .45, r * .4, C.ink, 3); S.ln(x, y - r * .45, x + r * .3 * Math.sin(a * .1), y - r * .45 - r * .3 * Math.cos(a * .1), C.acc, 3); S.ln(x, y, x + Math.sin(t * 2) * r * .35, y + r * .8, C.mut, 2); S.dot(x + Math.sin(t * 2) * r * .35, y + r * .8, 7, C.acc); return 'Tower and chamber clocks'; }
11 if (yr < 1900) { S.ring(x, y - r * 1.05, r * .16, C.acc, 3); S.dot(x, y, r * .85, '#1b2029'); S.ring(x, y, r * .85, C.acc, 4); }
12 else if (yr < 1969 || (yr >= 1969 && yr < 1972)) { S.rr(x - r * .4, y - r * 1.5, r * .8, r * 3, 8, '#2a2f38'); S.dot(x, y, r * .8, '#1b2029'); S.ring(x, y, r * .8, '#b9ac95', 4); }
13 else if (yr < 2010) { S.rr(x - r * .4, y - r * 1.5, r * .8, r * 3, 8, '#2a2f38'); S.rr(x - r * .8, y - r * .65, r * 1.6, r * 1.3, 10, '#22252b', C.mut); S.rr(x - r * .6, y - r * .32, r * 1.2, r * .64, 4, '#b9c2a8'); var d = new Date(); ctx.fillStyle = '#14161a'; ctx.font = '700 ' + r * .36 + 'px ui-monospace,monospace'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(('0' + d.getHours()).slice(-2) + (d.getSeconds() % 2 ? ':' : ' ') + ('0' + d.getMinutes()).slice(-2), x, y); return 'Quartz and digital'; }
14 else { S.rr(x - r * .4, y - r * 1.5, r * .8, r * 3, 8, '#2a2f38'); S.rr(x - r * .7, y - r * .85, r * 1.4, r * 1.7, r * .3, '#0b0c0f', C.mut); S.ring(x, y, r * .4, C.green, 5); ctx.strokeStyle = C.red; ctx.lineWidth = 5; ctx.beginPath(); ctx.arc(x, y, r * .55, -1.57, -1.57 + (t % 6) / 6 * 6.28); ctx.stroke(); return 'Smartwatches'; }
15 for (var i = 0; i < 12; i++) { var b = i * S.TAU / 12; S.ln(x + r * .62 * Math.sin(b), y - r * .62 * Math.cos(b), x + r * .72 * Math.sin(b), y - r * .72 * Math.cos(b), C.ink, 2); }
16 S.ln(x, y, x + r * .4 * Math.sin(a * .08), y - r * .4 * Math.cos(a * .08), C.ink, 4); S.ln(x, y, x + r * .62 * Math.sin(a), y - r * .62 * Math.cos(a), C.ink, 2.5); S.dot(x, y, 4, C.acc);
17 return yr < 1900 ? 'Pocket watches' : 'Mechanical wristwatches';
18 }
19 function wrap(s, x, y, mw, lh, px) { ctx.font = '500 ' + px + 'px system-ui,sans-serif'; var words = s.trim().split(' '), line = ''; words.forEach(function (wd) { if (ctx.measureText(line + wd).width > mw && line) { S.txt(line, x, y, C.ink, px); y += lh; line = ''; } line += wd + ' '; }); S.txt(line, x, y, C.ink, px); }
20 S.loop(function (dt, t) {
21 S.clear(); var w = S.W, h = S.H, e = ev[idx], sm = S.small(), ty = h * .84;
22 var era = device(sm ? w / 2 : w * .2, sm ? h * .2 : h * .4, Math.min(w, h) * (sm ? .11 : .15), e.year, t);
23 var tx = sm ? 20 : w * .4, yy = sm ? h * .42 : h * .2; S.txt(e.label, tx, yy, C.acc, sm ? 30 : 46, 'left', 700); S.txt(era, tx, yy + (sm ? 30 : 42), C.mut, 13, 'left', 600); wrap(e.text, tx, yy + (sm ? 58 : 78), w - tx - 20, sm ? 21 : 26, sm ? 14.5 : 18);
24 S.ln(30, ty, w - 30, ty, C.dim, 2); ev.forEach(function (q, i) { S.dot(X(q.year), ty, i === idx ? 8 : 4, i === idx ? C.acc : i < idx ? '#8a7650' : C.dim); });
25 for (var c = Math.ceil(y0 / 100) * 100; c <= y1; c += 100) { S.ln(X(c), ty + 10, X(c), ty + 16, C.dim, 1); if (!sm || c % 200 === 0) S.txt(String(c), X(c), ty + 28, C.mut, 11, 'center'); }
26 S.say('Step ' + (idx + 1) + ' of ' + ev.length + '. Tap any dot on the line, or use Earlier and Later.');
27 });
28});