Watch Encyclopedia / website · watchencyclopedia.org / exp / gears.js
| 1 | WE.mount('gears', function (S) { |
| 2 | var C = S.C, ctx = S.ctx, th = 0, turns = 0; |
| 3 | var na = S.slider('Gold gear teeth', 8, 40, 12, 1), nb = S.slider('Blue gear teeth', 8, 40, 30, 1), sp = S.slider('Gold gear speed', 0, 60, 20, 1, function (v) { return v + ' turns a minute'; }); |
| 4 | function gear(x, y, N, m, ang, col) { |
| 5 | var rp = N * m / 2, ro = rp + m * .9, rr = rp - m * 1.1, p = S.TAU / N; |
| 6 | ctx.beginPath(); |
| 7 | for (var i = 0; i < N; i++) { var a = ang + i * p; [[rr, -.27], [ro, -.14], [ro, .14], [rr, .27]].forEach(function (q) { ctx.lineTo(x + q[0] * Math.cos(a + q[1] * p), y + q[0] * Math.sin(a + q[1] * p)); }); } |
| 8 | ctx.closePath(); ctx.fillStyle = C.panel; ctx.fill(); ctx.strokeStyle = col; ctx.lineWidth = 2; ctx.stroke(); |
| 9 | S.ring(x, y, rp, 'rgba(255,255,255,.07)', 1); |
| 10 | for (var s = 0; s < 3; s++) { var b = ang + s * S.TAU / 3; S.ln(x + Math.cos(b) * m * 1.2, y + Math.sin(b) * m * 1.2, x + Math.cos(b) * (rr - m * .5), y + Math.sin(b) * (rr - m * .5), 'rgba(255,255,255,.1)', Math.max(2, m * .5)); } |
| 11 | S.dot(x + Math.cos(ang) * (rr - m * .2), y + Math.sin(ang) * (rr - m * .2), Math.max(3, m * .35), col); S.dot(x, y, Math.max(4, m * .7), col); |
| 12 | } |
| 13 | S.loop(function (dt) { |
| 14 | var w = S.W, h = S.H, sm = S.small(), A = na.v, B = nb.v, d = dt * sp.v / 60 * S.TAU; th += d; turns += d / S.TAU; |
| 15 | var top = sm ? 50 : 20, m = Math.min(w * .9 / (A + B + 4), (h - top - 20) / (Math.max(A, B) + 3)), ra = A * m / 2, rb = B * m / 2, xa = w / 2 - (ra + rb) / 2 - (rb - ra) / 2 + 0, y = top + (h - top) / 2; |
| 16 | xa = w / 2 - (ra + rb) + ra; var xb = xa + ra + rb; |
| 17 | S.clear(); gear(xa, y, A, m, th, C.acc); gear(xb, y, B, m, Math.PI + Math.PI / B - th * A / B, C.blue); |
| 18 | var ratio = B / A, tb = turns * A / B; |
| 19 | S.txt('Gold: ' + turns.toFixed(1) + ' turns', 14, 18, C.acc, sm ? 13 : 15, 'left', 700); S.txt('Blue: ' + tb.toFixed(1) + ' turns', 14, sm ? 38 : 42, C.blue, sm ? 13 : 15, 'left', 700); |
| 20 | S.txt(A + ' : ' + B, w - 54, 18, C.ink, sm ? 15 : 20, 'right', 700); S.txt('Blue turns at ' + (sp.v / ratio).toFixed(1) + ' a minute', w - 54, sm ? 38 : 44, C.mut, 12.5, 'right'); |
| 21 | S.say(A === B ? 'Same teeth, same speed. The second gear just turns the <b>other way</b>.' : A < B ? 'The small gear drives the big one. The big gear turns <b>' + ratio.toFixed(2) + ' times slower</b>, but with ' + ratio.toFixed(2) + ' times the twisting force. This is a low gear: slow and strong.' : 'The big gear drives the small one. The small gear spins <b>' + (1 / ratio).toFixed(2) + ' times faster</b>, with less twisting force. This is a high gear: fast and light.'); |
| 22 | }); |
| 23 | }); |