Branchwell

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

Raw 2.6 KB on 8b38e375b3e16f98454b93adca8e1fbd4fc534c5
1WE.mount('quartz', function (S) {
2 var C = S.C, ctx = S.ctx;
3 var stage = S.slider('Divide by two, this many times', 0, 15, 0, 1, function (v) { return v + (v === 1 ? ' time' : ' times'); });
4 S.button('Hear this stage', function () { var f = 32768 / Math.pow(2, stage.v); if (f <= 4096) S.tone(f, .9, 'square', .04); });
5 function dial(x, y, r, ang, title, sub) {
6 S.dot(x, y, r, '#151920'); S.ring(x, y, r, C.dim, 2);
7 for (var i = 0; i < 60; i++) { var a = i * S.TAU / 60, b = i % 5 === 0; S.ln(x + r * (b ? .82 : .9) * Math.sin(a), y - r * (b ? .82 : .9) * Math.cos(a), x + r * .95 * Math.sin(a), y - r * .95 * Math.cos(a), b ? C.ink : C.dim, b ? 2 : 1); }
8 S.ln(x - r * .15 * Math.sin(ang), y + r * .15 * Math.cos(ang), x + r * .86 * Math.sin(ang), y - r * .86 * Math.cos(ang), C.acc, 2.5); S.dot(x, y, 4, C.acc);
9 S.txt(title, x, y + r + 18, C.ink, 14, 'center', 650); S.txt(sub, x, y + r + 36, C.mut, 12.5, 'center');
10 }
11 S.loop(function (dt, t) {
12 S.clear(); var w = S.W, h = S.H, r = Math.min(w * .14, h * .15), y = h * .2;
13 dial(w * .27, y, r, Math.floor(t * 8) / 8 * S.TAU / 60, 'Mechanical', '8 small steps a second');
14 dial(w * .73, y, r, Math.floor(t) * S.TAU / 60, 'Quartz', '1 step a second');
15 // divider chain
16 var n = stage.v, f = 32768 / Math.pow(2, n), x0 = w * .06, x1 = w * .94, by = h * .72, bw = (x1 - x0) / 16;
17 for (var i = 0; i <= 15; i++) { var on = i <= n, hh = h * .13 * (1 - i / 22); S.rr(x0 + i * bw + 2, by - hh, bw - 4, hh, 4, i === n ? C.acc : on ? 'rgba(213,163,90,.35)' : C.grid); if (!S.small() || i % 3 === 0 || i === 15) S.txt(i === 0 ? '32,768' : i === 15 ? '1' : String(32768 / Math.pow(2, i)), x0 + i * bw + bw / 2, by + 13, i === n ? C.ink : C.mut, S.small() ? 9 : 10.5, 'center'); }
18 S.txt('Each box is a tiny circuit that halves the beat', x0, by - h * .13 - 12, C.mut, 12.5);
19 // waveform for this stage (drawn slowed so you can see it)
20 var wy = h * .9, amp = h * .045, vis = Math.max(.6, 12 / Math.pow(1.45, n)); ctx.beginPath();
21 for (var x = x0; x <= x1; x += 2) { var ph = ((x - x0) / (x1 - x0) * vis * 6 - t * vis * 1.2) % 1; if (ph < 0) ph += 1; ctx.lineTo(x, wy + (ph < .5 ? -amp : amp)); }
22 ctx.strokeStyle = C.blue; ctx.lineWidth = 2; ctx.stroke();
23 S.say('After <b>' + n + '</b> ' + (n === 1 ? 'halving' : 'halvings') + ' the signal beats <b>' + f.toLocaleString() + ' times a second</b>.' + (n === 15 ? ' Exactly one pulse a second: that pulse moves the hand.' : n === 0 ? ' This is the raw crystal, far too fast to see or hear. Slide to divide it down.' : f > 4096 ? ' Still too high to hear comfortably.' : ' Press Hear this stage.'));
24 });
25});