WE.mount('movement', function (S) { var C = S.C, ctx = S.ctx, power = .7, phase = 0, beats = 0, esc = 0, escTarget = 0, lastSign = 1, part = -1; var rate = S.select('Beat rate', [['18000', '18,000'], ['21600', '21,600'], ['28800', '28,800'], ['36000', '36,000']], '28800'); var slow = S.slider('Slow motion', 0, 100, 55, 1, function (v) { var f = Math.pow(10, -v / 50); return f > .95 ? 'Real time' : '1/' + Math.round(1 / f) + ' speed'; }); S.button('Wind the crown', function () { power = Math.min(1, power + .22); }); var snd = S.toggle('Tick sound', false); var NAMES = [['Mainspring barrel', 'A coiled spring stores the energy. As it unwinds it turns the barrel and drives everything else.'], ['Gear train', 'Three wheels step the slow, strong turn of the barrel up into a fast, light one.'], ['Escape wheel', 'It always tries to spin. The fork lets it advance only one tooth at a time. That is the tick.'], ['Pallet fork', 'It rocks side to side, locking the escape wheel and passing a small push to the balance.'], ['Balance wheel', 'It swings back and forth at a steady beat, like a tiny pendulum. Its rhythm sets the time.']]; function gear(x, y, r, n, ang, col, hl) { ctx.beginPath(); for (var i = 0; i < n * 2; i++) { var a = ang + Math.PI * i / n, rr = i % 2 ? r * .9 : r; ctx.lineTo(x + rr * Math.cos(a - .5 * Math.PI / n * .5), y + rr * Math.sin(a - .5 * Math.PI / n * .5)); ctx.lineTo(x + rr * Math.cos(a + .5 * Math.PI / n * .5), y + rr * Math.sin(a + .5 * Math.PI / n * .5)); } ctx.closePath(); ctx.fillStyle = hl ? 'rgba(213,163,90,.28)' : 'rgba(255,255,255,.06)'; ctx.fill(); ctx.strokeStyle = hl ? C.acc : col; ctx.lineWidth = 1.5; ctx.stroke(); for (var k = 0; k < 5; k++) { var b = ang + k * S.TAU / 5; S.ln(x + r * .18 * Math.cos(b), y + r * .18 * Math.sin(b), x + r * .78 * Math.cos(b), y + r * .78 * Math.sin(b), hl ? C.acc : col, 1.5); } S.ring(x, y, r * .78, hl ? C.acc : col, 1.5); S.dot(x, y, r * .1, C.acc); } var L = {}; function layout() { var w = S.W, h = S.H, u = Math.min(w / 8.4, h / 4.4), cy = h * .56, x0 = (w - u * 7.4) / 2; L = { u: u, bar: [x0 + u * 1.1, cy, u * 1.1], g1: [x0 + u * 2.75, cy - u * .25, u * .7], g2: [x0 + u * 3.9, cy + u * .35, u * .58], g3: [x0 + u * 4.9, cy - u * .2, u * .5], esc: [x0 + u * 5.85, cy + u * .35, u * .48], fork: [x0 + u * 6.25, cy - u * .55], bal: [x0 + u * 6.55, cy - u * 1.45, u * .78] }; } S.onresize = layout; layout(); S.pointer({ scroll: true, move: function (x, y) { part = hit(x, y); }, down: function (x, y) { part = hit(x, y); } }); function hit(x, y) { var d = function (p) { return Math.hypot(x - p[0], y - p[1]); }; if (d(L.bal) < L.bal[2]) return 4; if (d(L.fork) < L.u * .35) return 3; if (d(L.esc) < L.esc[2]) return 2; if (d(L.bar) < L.bar[2]) return 0; if (d(L.g1) < L.g1[2] || d(L.g2) < L.g2[2] || d(L.g3) < L.g3[2]) return 1; return -1; } S.loop(function (dt) { var f = +rate.v / 7200, sp = Math.pow(10, -slow.v / 50), run = power > 0; if (run) { phase += dt * sp * f * S.TAU; power = Math.max(0, power - dt * sp * .012 - dt * .0006); } var s = Math.sin(phase), sign = s >= 0 ? 1 : -1; if (sign !== lastSign) { lastSign = sign; beats++; escTarget += S.TAU / 30; if (snd.v && sp > .02) S.tone(sign > 0 ? 2600 : 2100, .03, 'square', .05); } esc += (escTarget - esc) * Math.min(1, dt * sp * f * 40 + .08); S.clear(); var u = L.u, b = L.bar; // barrel + spring S.ring(b[0], b[1], b[2], part === 0 ? C.acc : C.mut, 2); ctx.beginPath(); var turns = 3 + power * 7; for (var i = 0; i <= 220; i++) { var t = i / 220, a = t * turns * S.TAU - esc / 600, r = b[2] * (.12 + (.8 - power * .35) * t); ctx.lineTo(b[0] + r * Math.cos(a), b[1] + r * Math.sin(a)); } ctx.strokeStyle = C.acc; ctx.lineWidth = 1.6; ctx.stroke(); gear(L.g1[0], L.g1[1], L.g1[2], 26, esc / 100, C.mut, part === 1); gear(L.g2[0], L.g2[1], L.g2[2], 22, -esc / 12, C.mut, part === 1); gear(L.g3[0], L.g3[1], L.g3[2], 18, esc / 2.5, C.mut, part === 1); // escape wheel (15 club teeth) var e = L.esc; ctx.beginPath(); for (var k = 0; k < 15; k++) { var a0 = -esc + k * S.TAU / 15; ctx.lineTo(e[0] + e[2] * .62 * Math.cos(a0 - .12), e[1] + e[2] * .62 * Math.sin(a0 - .12)); ctx.lineTo(e[0] + e[2] * Math.cos(a0 + .16), e[1] + e[2] * Math.sin(a0 + .16)); ctx.lineTo(e[0] + e[2] * .62 * Math.cos(a0 + .22), e[1] + e[2] * .62 * Math.sin(a0 + .22)); } ctx.closePath(); ctx.fillStyle = part === 2 ? 'rgba(213,163,90,.28)' : 'rgba(255,255,255,.06)'; ctx.fill(); ctx.strokeStyle = part === 2 ? C.acc : C.blue; ctx.lineWidth = 1.5; ctx.stroke(); S.dot(e[0], e[1], 4, C.acc); // pallet fork var fk = L.fork, tilt = Math.max(-1, Math.min(1, s * 6)) * .16, col = part === 3 ? C.acc : C.red; ctx.save(); ctx.translate(fk[0], fk[1]); ctx.rotate(tilt); ctx.strokeStyle = col; ctx.lineWidth = 3; ctx.lineCap = 'round'; ctx.beginPath(); ctx.moveTo(-u * .42, u * .34); ctx.lineTo(0, 0); ctx.lineTo(u * .42, u * .34); ctx.moveTo(0, 0); ctx.lineTo(0, -u * .5); ctx.stroke(); S.dot(-u * .42, u * .34, 4, C.pink); S.dot(u * .42, u * .34, 4, C.pink); ctx.restore(); S.dot(fk[0], fk[1], 4, col); // balance var bl = L.bal, ang = s * 2.4 * (run ? 1 : 0) + (run ? 0 : 0); ctx.save(); ctx.translate(bl[0], bl[1]); ctx.rotate(ang); ctx.strokeStyle = part === 4 ? C.acc : C.ink; ctx.lineWidth = 4; ctx.beginPath(); ctx.arc(0, 0, bl[2], 0, S.TAU); ctx.stroke(); ctx.lineWidth = 2.5; ctx.beginPath(); ctx.moveTo(-bl[2], 0); ctx.lineTo(bl[2], 0); ctx.moveTo(0, -bl[2]); ctx.lineTo(0, bl[2]); ctx.stroke(); S.dot(0, bl[2] * .32, 3.5, C.pink); ctx.restore(); ctx.beginPath(); for (var j = 0; j <= 160; j++) { var tt = j / 160, aa = tt * 5 * S.TAU + ang * tt, rr = bl[2] * (.08 + .5 * tt * (1 + s * .08)); ctx.lineTo(bl[0] + rr * Math.cos(aa), bl[1] + rr * Math.sin(aa)); } ctx.strokeStyle = C.acc; ctx.lineWidth = 1.1; ctx.stroke(); // labels + meters var fs = S.small() ? 11 : 13; S.txt('Mainspring', b[0], b[1] + b[2] + 16, C.mut, fs, 'center'); S.txt('Gear train', L.g2[0], L.g2[1] + L.g2[2] + 16, C.mut, fs, 'center'); S.txt('Escape wheel', e[0], e[1] + e[2] + 16, C.mut, fs, 'center'); S.txt('Balance', bl[0], bl[1] - bl[2] - 14, C.mut, fs, 'center'); var bw = Math.min(220, S.W * .4); S.txt('Power reserve', 16, 22, C.mut, 12); S.rr(16, 32, bw, 8, 4, C.grid); S.rr(16, 32, bw * power, 8, 4, power > .15 ? C.acc : C.red); S.txt(beats.toLocaleString() + ' beats', S.W - 54, 22, C.mut, 12, 'right'); if (part >= 0) S.say('' + NAMES[part][0] + '  ' + NAMES[part][1]); else S.say(run ? 'Running at ' + (+rate.v).toLocaleString() + ' beats an hour, which is ' + (f * 2) + ' ticks every second. Point at any part to see what it does.' : 'Out of power. The mainspring has run down. Wind the crown to start it again.'); }); });