WE.mount('gears', function (S) { var C = S.C, ctx = S.ctx, th = 0, turns = 0; 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'; }); function gear(x, y, N, m, ang, col) { var rp = N * m / 2, ro = rp + m * .9, rr = rp - m * 1.1, p = S.TAU / N; ctx.beginPath(); 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)); }); } ctx.closePath(); ctx.fillStyle = C.panel; ctx.fill(); ctx.strokeStyle = col; ctx.lineWidth = 2; ctx.stroke(); S.ring(x, y, rp, 'rgba(255,255,255,.07)', 1); 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)); } 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); } S.loop(function (dt) { 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; 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; xa = w / 2 - (ra + rb) + ra; var xb = xa + ra + rb; 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); var ratio = B / A, tb = turns * A / B; 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); 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'); S.say(A === B ? 'Same teeth, same speed. The second gear just turns the other way.' : A < B ? 'The small gear drives the big one. The big gear turns ' + ratio.toFixed(2) + ' times slower, 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 ' + (1 / ratio).toFixed(2) + ' times faster, with less twisting force. This is a high gear: fast and light.'); }); });