WE.mount('heart', function (S) {
var C = S.C, ctx = S.ctx, ph = 0, beats = 0, ecg = [], pos = 0;
var bpm = S.slider('Heart rate', 40, 190, 70, 1, function (v) { return v + ' beats a minute'; }), snd = S.toggle('Sound', false);
var PTS = [[.5, .88], [.1, .88], [.1, .3], [.37, .3], [.37, .6], [.47, .5], [.47, .1], [.52, .06], [.9, .06], [.9, .3], [.63, .3], [.63, .6], [.63, .88], [.5, .88]], LUNG = 7, len = [0];
for (var i = 1; i < PTS.length; i++) len.push(len[i - 1] + Math.hypot(PTS[i][0] - PTS[i - 1][0], PTS[i][1] - PTS[i - 1][1]));
var TOT = len[len.length - 1], LF = len[LUNG] / TOT;
function at(s) { var d = (s % 1) * TOT; for (var i = 1; i < len.length; i++) if (d <= len[i]) { var f = (d - len[i - 1]) / (len[i] - len[i - 1]); return [PTS[i - 1][0] + (PTS[i][0] - PTS[i - 1][0]) * f, PTS[i - 1][1] + (PTS[i][1] - PTS[i - 1][1]) * f]; } return PTS[0]; }
function g(x, m, s) { return Math.exp(-(x - m) * (x - m) / (2 * s * s)); }
function wave(p) { return .12 * g(p, .08, .025) - .12 * g(p, .185, .008) + 1 * g(p, .2, .01) - .22 * g(p, .218, .009) + .28 * g(p, .42, .045); }
S.loop(function (dt) {
var prev = ph, rate = bpm.v / 60; ph += dt * rate; if (Math.floor(ph) > Math.floor(prev)) beats++;
var p = ph % 1, pp = prev % 1; if (snd.v) { if (pp < .2 && p >= .2) S.tone(55, .14, 'sine', .5); if (pp < .5 && p >= .5) S.tone(75, .1, 'sine', .35); }
var atr = Math.sin(Math.PI * Math.min(1, Math.max(0, p / .16))), ven = Math.sin(Math.PI * Math.min(1, Math.max(0, (p - .2) / .3)));
pos += dt * rate * (.012 + .09 * ven); ecg.push(wave(p)); if (ecg.length > 420) ecg.shift();
var w = S.W, h = S.H, sm = S.small(), bs = sm ? Math.min(w * .96, h * .66) : Math.min(w * .5, h * .96), bx = sm ? (w - bs) / 2 : w * .04, by = sm ? 4 : (h - bs) / 2;
function X(v) { return bx + v * bs; } function Y(v) { return by + v * bs; }
S.clear();
for (var i = 1; i < PTS.length; i++) S.ln(X(PTS[i - 1][0]), Y(PTS[i - 1][1]), X(PTS[i][0]), Y(PTS[i][1]), i <= LUNG ? 'rgba(111,177,230,.22)' : 'rgba(232,115,92,.22)', bs * .035);
function box(x, y, ww, hh, name, sq, col) { var k = 1 - sq * .16; S.rr(X(x + ww * (1 - k) / 2), Y(y + hh * (1 - k) / 2), ww * k * bs, hh * k * bs, bs * .03, col, C.ink); var L = x < .5, lx = L ? X(x + .018) : X(x + ww - .018), fs = sm ? 9.5 : 12.5; name.forEach(function (nm, j) { S.txt(nm, lx, Y(y + hh / 2) + (j - .5) * (fs + 3), C.ink, fs, L ? 'left' : 'right', 600); }); }
box(.22, .2, .2, .2, ['Right', 'atrium'], atr, '#2b4763'); box(.22, .46, .2, .28, ['Right', 'ventricle'], ven, '#2b4763'); box(.58, .2, .2, .2, ['Left', 'atrium'], atr, '#6b3329'); box(.58, .46, .2, .28, ['Left', 'ventricle'], ven, '#6b3329');
S.rr(X(.35), Y(.015), .3 * bs, .09 * bs, bs * .03, '#1b2028', C.dim); S.txt('Lungs', X(.6), Y(.035), C.ink, sm ? 11 : 13, 'center', 600); S.rr(X(.3), Y(.85), .4 * bs, .1 * bs, bs * .03, '#1b2028', C.dim); S.txt('Rest of the body', X(.5), Y(.925), C.ink, sm ? 11 : 13, 'center', 600);
for (var k = 0; k < 70; k++) { var s = (k / 70 + pos) % 1, q = at(s); S.dot(X(q[0]), Y(q[1]), Math.max(2, bs * .009), s < LF ? C.blue : C.red); }
// readout
var tx = sm ? 12 : w * .6, ty = sm ? by + bs + 14 : h * .16, gw = sm ? w - 24 : w * .36, gy = sm ? ty + 54 : h * .62, gh = sm ? Math.min(40, (h - gy) * .7) : h * .16;
S.txt(bpm.v + '', tx, ty + (sm ? 0 : 10), C.ink, sm ? 22 : 44, 'left', 700); S.txt('beats a minute', tx + (sm ? 44 : 84), ty + (sm ? 2 : 16), C.mut, 13);
S.txt((bpm.v * 1440).toLocaleString('en-US') + ' beats a day at this pace', sm ? w - 12 : tx, sm ? ty + 2 : ty + 56, C.mut, sm ? 11 : 13.5, sm ? 'right' : 'left');
if (!sm) { S.dot(tx + 6, ty + 96, 5, C.blue); S.txt('Used blood, low in oxygen', tx + 20, ty + 96, C.mut, 12.5); S.dot(tx + 6, ty + 118, 5, C.red); S.txt('Fresh blood, rich in oxygen', tx + 20, ty + 118, C.mut, 12.5); }
ctx.beginPath(); ecg.forEach(function (v, i) { ctx.lineTo(tx + gw * i / 420, gy - v * gh); }); ctx.strokeStyle = C.green; ctx.lineWidth = 2; ctx.stroke(); S.txt('Electrical signal', tx, gy + gh * .5 + 6, C.dim, 11.5);
S.say('The heart is two pumps side by side. The right side sends used blood to the lungs to pick up oxygen. The left side sends that fresh blood to the whole body. The small top rooms fill the big bottom rooms, then the bottom rooms squeeze. This is a diagram, not a drawing of a real heart.');
});
});