WE.mount('engine', function (S) { var C = S.C, ctx = S.ctx, th = 0, PI = Math.PI; var NAMES = ['Intake', 'Compression', 'Power', 'Exhaust'], COL = [C.blue, C.violet, C.red, C.mut]; var TXT = ['Intake. The piston slides down and pulls a mix of air and fuel in through the open valve on the left.', 'Compression. Both valves are shut. The piston comes back up and squeezes the mix into a small space, which makes it burn harder.', 'Power. The spark plug fires. The burning gas swells and shoves the piston down. This is the only stroke that makes power.', 'Exhaust. The valve on the right opens and the rising piston pushes the burned gas out. Then it all starts again.']; var sp = S.slider('Speed', 0, 120, 30, 1, function (v) { return v === 0 ? 'paused' : v + ' turns a minute'; }); S.button('Next stroke', function () { th = ((Math.floor(th / PI + 1e-6) + 1) * PI) % (4 * PI) + .02; sp.set(0); }); S.loop(function (dt) { th = (th + dt * sp.v / 60 * S.TAU) % (4 * PI); var w = S.W, h = S.H, sm = S.small(), k = Math.floor(th / PI) % 4, f = th / PI - Math.floor(th / PI); var cx = sm ? w * .5 : w * .34, cy = h * .8, r = h * .1, l = h * .3, ph = h * .1, bw = Math.min(w * .32, h * .36), head = cy - r - l - ph / 2 - h * .035; var pin = cy - (r * Math.cos(th) + Math.sqrt(l * l - r * r * Math.sin(th) * Math.sin(th))), top = pin - ph / 2; S.clear(); // gas var a = k === 0 ? .25 : k === 1 ? .25 + f * .45 : k === 2 ? .85 - f * .5 : .3, gc = k === 0 ? '111,177,230' : k === 1 ? '150,150,240' : k === 2 ? '240,140,70' : '150,146,135'; ctx.fillStyle = 'rgba(' + gc + ',' + a + ')'; ctx.fillRect(cx - bw / 2, head, bw, top - head); // cylinder walls and head S.ln(cx - bw / 2, head, cx - bw / 2, cy - r * .2, C.ink, 3); S.ln(cx + bw / 2, head, cx + bw / 2, cy - r * .2, C.ink, 3); var vw = bw * .28, io = k === 0 ? Math.sin(f * PI) : 0, eo = k === 3 ? Math.sin(f * PI) : 0, pw = h * .1; S.ln(cx - bw / 2, head, cx - bw / 2 + bw * .08, head, C.ink, 3); S.ln(cx - bw / 2 + bw * .08 + vw, head, cx + bw / 2 - bw * .08 - vw, head, C.ink, 3); S.ln(cx + bw / 2 - bw * .08, head, cx + bw / 2, head, C.ink, 3); // ports [[-1, io, C.blue, 'Air and fuel in'], [1, eo, C.mut, 'Burned gas out']].forEach(function (p) { var vx = cx + p[0] * (bw / 2 - bw * .08 - vw / 2), d = p[1] * h * .035; S.ln(vx - vw / 2, head, vx - vw / 2 + p[0] * 0, head - pw, C.dim, 2); S.ln(vx + vw / 2, head, vx + vw / 2, head - pw, C.dim, 2); S.ln(vx, head - pw - h * .03 + d, vx, head + d, p[1] > .02 ? p[2] : C.ink, 3); S.ln(vx - vw / 2 + 1, head + d + 2, vx + vw / 2 - 1, head + d + 2, p[1] > .02 ? p[2] : C.ink, 5); if (p[1] > .05) { if (p[0] < 0) S.arrow(vx - vw * .28, head - pw - 4, vx - vw * .28, head - 6, p[2], 2); else S.arrow(vx + vw * .28, head - 6, vx + vw * .28, head - pw - 4, p[2], 2); } S.txt(p[3], vx + p[0] * (vw / 2 + 8), head - pw * .6, p[1] > .02 ? p[2] : C.dim, sm ? 10.5 : 12, p[0] < 0 ? 'right' : 'left'); }); // spark plug S.ln(cx, head - pw - h * .03, cx, head + 5, C.ink, 4); S.txt('Spark plug', cx, head - pw - h * .03 - 12, k === 2 && f < .25 ? C.yellow : C.dim, sm ? 10.5 : 12, 'center'); if (k === 2 && f < .22) { for (var i = 0; i < 9; i++) { var an = i / 9 * S.TAU + th * 5, rr = (1 - f / .22) * h * .06 + 6; S.ln(cx, head + 8, cx + Math.cos(an) * rr, head + 8 + Math.abs(Math.sin(an)) * rr, C.yellow, 2); } } // piston, rod, crank S.rr(cx - bw / 2 + 3, top, bw - 6, ph, 5, '#5b606b', C.ink); S.ln(cx - bw / 2 + 5, top + ph * .22, cx + bw / 2 - 5, top + ph * .22, C.bg, 2); S.ln(cx - bw / 2 + 5, top + ph * .4, cx + bw / 2 - 5, top + ph * .4, C.bg, 2); var kx = cx + r * Math.sin(th), ky = cy - r * Math.cos(th); S.ring(cx, cy, r, C.dim, 2); S.ln(cx, pin, kx, ky, C.acc, 7); S.ln(cx, cy, kx, ky, C.ink, 6); S.dot(cx, pin, 6, C.ink); S.dot(kx, ky, 7, C.ink); S.dot(cx, cy, 9, C.acc); if (k === 2) S.arrow(cx + bw / 2 + 16, top - 4, cx + bw / 2 + 16, top + ph + 6, C.red, 3); // readout var tx = sm ? 12 : w * .6, ty = sm ? 16 : h * .2; if (sm) { S.txt((k + 1) + '. ' + NAMES[k], tx, ty, COL[k] === C.mut ? C.ink : COL[k], 17, 'left', 700); } else { S.txt('Stroke ' + (k + 1) + ' of 4', tx, ty, C.mut, 13); S.txt(NAMES[k], tx, ty + 34, COL[k] === C.mut ? C.ink : COL[k], 40, 'left', 700); for (var j = 0; j < 4; j++) { var yy = ty + 90 + j * 34; S.rr(tx, yy - 12, w * .3, 26, 13, j === k ? 'rgba(255,255,255,.08)' : null, j === k ? C.dim : null); S.dot(tx + 14, yy + 1, 5, j === k ? (COL[j] === C.mut ? C.ink : COL[j]) : C.dim); S.txt(NAMES[j], tx + 28, yy + 1, j === k ? C.ink : C.mut, 13.5, 'left', j === k ? 700 : 500); if (j === k) S.rr(tx + w * .14, yy - 2, (w * .14) * f, 6, 3, C.acc); } S.txt('The crank turns twice for every one power stroke.', tx, ty + 240, C.mut, 12.5); } S.say(TXT[k]); }); });