Watch Encyclopedia / website / exp / lock.js
| 1 | WE.mount('lock', function (S) { |
| 2 | var C = S.C, ctx = S.ctx, N = 5, req = [], lift = [0, 0, 0, 0, 0], show = [0, 0, 0, 0, 0], key = false, turn = 0, G = {}; |
| 3 | function fresh() { req = []; for (var i = 0; i < N; i++) req.push(1 + Math.floor(Math.random() * 5)); lift = [0, 0, 0, 0, 0]; key = false; } |
| 4 | fresh(); req = [3, 5, 2, 4, 1]; |
| 5 | S.button('Use the right key', function () { lift = req.slice(); key = true; }); |
| 6 | S.button('Try a wrong key', function () { do { lift = req.map(function () { return 1 + Math.floor(Math.random() * 5); }); } while (lift.every(function (v, i) { return v === req[i]; })); key = true; }); |
| 7 | S.button('Take the key out', function () { lift = [0, 0, 0, 0, 0]; key = false; }); |
| 8 | S.button('New lock', fresh); |
| 9 | S.pointer({ scroll: true, down: function (x, y) { for (var i = 0; i < N; i++) if (Math.abs(x - G.px(i)) < G.gap / 2) { if (key) { key = false; } lift[i] = (lift[i] + 1) % 6; } } }); |
| 10 | S.loop(function (dt) { |
| 11 | var w = S.W, h = S.H, sm = S.small(), u = h * .034, ys = h * .46, x0 = w * (sm ? .04 : .08), x1 = w * (sm ? .96 : .66), gap = (x1 - x0) / (N + 1.2), pw = Math.min(gap * .5, u * 2.4); |
| 12 | G.gap = gap; G.px = function (i) { return x0 + gap * (i + 1.1); }; |
| 13 | var open = lift.every(function (v, i) { return v === req[i]; }), ok = 0; |
| 14 | for (var i = 0; i < N; i++) { show[i] += (lift[i] - show[i]) * Math.min(1, dt * 14); } |
| 15 | turn += ((open ? 1 : 0) - turn) * Math.min(1, dt * 5); |
| 16 | S.clear(); |
| 17 | S.rr(x0, ys - 12 * u, x1 - x0, 12 * u, 6, '#1d222b', C.dim); S.rr(x0, ys, x1 - x0, 12.6 * u, 6, '#262c37', C.dim); |
| 18 | ctx.fillStyle = C.bg; ctx.fillRect(x0, ys + 6 * u, x1 - x0, 6 * u); |
| 19 | S.txt('Housing (fixed)', x0 + 8, ys - 12 * u + 12, C.mut, 11.5); S.txt('Plug (turns)', x0 + 8, ys + 13 * u + 8, C.mut, 11.5); |
| 20 | if (key) { ctx.beginPath(); ctx.moveTo(x0 - 4, ys + 12 * u); ctx.lineTo(x0 - 4, ys + 9 * u); for (var j = 0; j < N; j++) { var kx = G.px(j), ky = ys + 11 * u - show[j] * u; ctx.lineTo(kx - gap * .32, ky + (j === 0 ? 0 : 0)); ctx.lineTo(kx - pw * .2, ky); ctx.lineTo(kx + pw * .2, ky); ctx.lineTo(kx + gap * .32, ky); if (j < N - 1) ctx.lineTo(kx + gap / 2, ys + 11.4 * u); } ctx.lineTo(x1 - gap * .25, ys + 12 * u); ctx.closePath(); ctx.fillStyle = '#8f8a7c'; ctx.fill(); ctx.strokeStyle = C.ink; ctx.lineWidth = 1; ctx.stroke(); } |
| 21 | for (i = 0; i < N; i++) { |
| 22 | var px = G.px(i), bot = ys + 11 * u - show[i] * u, kl = (11 - req[i]) * u, kt = bot - kl, dt2 = kt - 6 * u, good = Math.abs(show[i] - req[i]) < .08; if (lift[i] === req[i]) ok++; |
| 23 | ctx.fillStyle = C.bg; ctx.fillRect(px - pw / 2 - 2, ys - 11.4 * u, pw + 4, 22.4 * u); |
| 24 | ctx.strokeStyle = C.mut; ctx.lineWidth = 1.5; ctx.beginPath(); var sy0 = ys - 11.2 * u, n = 9; for (var z = 0; z <= n; z++) ctx.lineTo(px + (z % 2 ? pw * .35 : -pw * .35), sy0 + (dt2 - sy0) * z / n); ctx.stroke(); |
| 25 | S.rr(px - pw / 2, dt2, pw, 6 * u - 1.5, 3, C.blue, good ? C.green : null); S.rr(px - pw / 2, kt + 1.5, pw, kl - 1.5, 3, C.acc, good ? C.green : null); |
| 26 | ctx.beginPath(); ctx.moveTo(px - pw / 2, bot - 4); ctx.lineTo(px, bot + 3); ctx.lineTo(px + pw / 2, bot - 4); ctx.fillStyle = C.acc; ctx.fill(); |
| 27 | S.dot(px, ys, 4, good ? C.green : C.red); |
| 28 | } |
| 29 | ctx.setLineDash([6, 5]); S.ln(x0 - 6, ys, x1 + 6, ys, open ? C.green : C.red, 1.5); ctx.setLineDash([]); if (!sm) S.txt('Shear line', x1 + 10, ys, open ? C.green : C.red, 11.5); |
| 30 | // front view |
| 31 | var fx = sm ? w - 46 : w * .87, fy = sm ? 96 : h * .4, fr = sm ? 26 : Math.min(w * .075, h * .2); |
| 32 | if (!sm) {S.ring(fx, fy, fr * 1.25, C.dim, 6); S.dot(fx, fy, fr, '#262c37'); S.ring(fx, fy, fr, open ? C.green : C.mut, 2); |
| 33 | ctx.save(); ctx.translate(fx, fy); ctx.rotate(turn * Math.PI / 2); S.rr(-fr * .1, -fr * .6, fr * .2, fr * 1.2, 3, C.bg, C.mut); ctx.restore(); } |
| 34 | if (!sm) { S.txt(open ? 'Open' : 'Locked', fx, fy + fr * 1.25 + 34, open ? C.green : C.red, 26, 'center', 700); S.txt(ok + ' of 5 pins lined up', fx, fy + fr * 1.25 + 62, C.mut, 13, 'center'); } |
| 35 | else S.txt(open ? 'Open' : 'Locked: ' + ok + ' of 5 lined up', w - 12, ys + 13 * u + 8, open ? C.green : C.red, 12.5, 'right', 700); |
| 36 | S.say(open ? '<b>Open.</b> Every gap between a gold pin and a blue pin sits exactly on the shear line, so nothing blocks the plug and it can turn.' : key ? 'This key lifts the pins to the wrong heights. Wherever a pin crosses the red line, it <b>blocks the plug</b> like a bolt through two plates.' : 'Springs push the pins down so they cross the line and jam the plug. A key works by lifting each pin just the right amount. <b>Tap a pin</b> to lift it a notch and try to line up all five by hand.'); |
| 37 | }); |
| 38 | }); |