WE.mount('gas', function (S) { var C = S.C, ctx = S.ctx, P = [], imp = 0, win = 0, pres = 0; var T = S.slider('Temperature', 50, 900, 300, 10, function (v) { return v + ' K (' + (v - 273) + ' °C)'; }), V = S.slider('Volume', 30, 100, 100, 1, function (v) { return v + '%'; }), N = S.slider('Particles', 10, 300, 120, 10); function mk() { return { x: Math.random(), y: Math.random(), a: Math.random() * 6.283, s: .5 + Math.random() }; } S.loop(function (dt) { while (P.length < N.v) P.push(mk()); P.length = N.v; S.clear(); var w = S.W, h = S.H, sm = S.small(), bx = 20, by = 30, bh = h - 60, full = (sm ? w - 40 : w * .62), bw = full * V.v / 100, sp = Math.sqrt(T.v / 300) * 150; P.forEach(function (p) { var vx = Math.cos(p.a) * sp * p.s, vy = Math.sin(p.a) * sp * p.s; p.x += vx * dt / bw; p.y += vy * dt / bh; if (p.x < 0) { p.x = -p.x; p.a = Math.PI - p.a; imp += 2 * Math.abs(vx); } if (p.x > 1) { p.x = 2 - p.x; if (p.x < 0) p.x = .5; p.a = Math.PI - p.a; imp += 2 * Math.abs(vx); } if (p.y < 0) { p.y = -p.y; p.a = -p.a; imp += 2 * Math.abs(vy); } if (p.y > 1) { p.y = 2 - p.y; if (p.y < 0) p.y = .5; p.a = -p.a; imp += 2 * Math.abs(vy); } var heat = Math.min(1, (T.v - 50) / 850); S.dot(bx + p.x * bw, by + p.y * bh, 3.2, 'rgb(' + Math.round(110 + 140 * heat) + ',' + Math.round(170 - 60 * heat) + ',' + Math.round(230 - 150 * heat) + ')'); }); win += dt; if (win > .5) { pres = (pres ? pres * .6 : 0) + (pres ? .4 : 1) * (imp / win / (2 * (bw + bh))) / 60 * full * bh / 24372; imp = 0; win = 0; } S.rr(bx, by, bw, bh, 4, null, C.mut); S.rr(bx + bw, by - 6, 12, bh + 12, 3, C.dim); S.ln(bx + bw + 12, by + bh / 2, bx + full + 26, by + bh / 2, C.dim, 6); var ideal = N.v * T.v / V.v / 360, tx = sm ? bx + 8 : w * .72, ty = sm ? by + 20 : h * .3; if (sm) S.rr(tx - 6, ty - 16, 190, 74, 8, 'rgba(13,15,19,.75)'); S.txt('Pressure', tx, ty, C.mut, 13); S.txt(pres.toFixed(1), tx, ty + 28, C.ink, sm ? 24 : 38, 'left', 700); S.txt('The gas law predicts ' + ideal.toFixed(1), tx, ty + (sm ? 48 : 62), C.green, 12.5, 'left', 600); S.say('Pressure is nothing but countless tiny hits on the walls. Heat the gas and the particles hit harder. Squeeze it and they hit more often. Halve the volume and the pressure doubles.'); }); });