Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
306 lines (267 loc) · 10.6 KB
/
Copy pathapp.js
File metadata and controls
306 lines (267 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const screens = document.querySelectorAll('.screen');
function showScreen(id) {
screens.forEach(s => s.classList.remove('active'));
document.getElementById(id).classList.add('active');
window.scrollTo(0, 0);
}
document.querySelectorAll('[data-back]').forEach(btn => {
btn.addEventListener('click', () => showScreen(btn.dataset.back));
});
document.querySelectorAll('[data-open]').forEach(btn => {
btn.addEventListener('click', () => {
const target = btn.dataset.open;
showScreen(target + '-landing');
});
});
const PROGRESS_KEY = 'cvs-flow-progress';
function getProgress() {
try {
return JSON.parse(localStorage.getItem(PROGRESS_KEY)) || {};
} catch (e) {
return {};
}
}
function setDone(moduleId) {
const p = getProgress();
p[moduleId] = true;
try { localStorage.setItem(PROGRESS_KEY, JSON.stringify(p)); } catch (e) {}
renderBadges();
}
function renderBadges() {
const p = getProgress();
const flowBadge = document.getElementById('badge-flow');
const loopBadge = document.getElementById('badge-loop');
if (p.flow) { flowBadge.textContent = 'Completed'; flowBadge.classList.add('done'); }
if (p.loop) { loopBadge.textContent = 'Completed'; loopBadge.classList.add('done'); }
}
renderBadges();
document.getElementById('flow-start-btn').addEventListener('click', () => showScreen('flow-module'));
document.getElementById('to-trap-btn').addEventListener('click', () => showScreen('flow-trap'));
const VESSEL_LEFT = 70;
const VESSEL_RIGHT = 350;
const VESSEL_CENTER = (VESSEL_LEFT + VESSEL_RIGHT) / 2;
const NARROW_SPAN = 45;
const BASE_HALF = 28;
function halfGapAt(x, gap) {
const dist = Math.abs(x - VESSEL_CENTER);
if (dist > NARROW_SPAN) return BASE_HALF;
const t = dist / NARROW_SPAN;
const eased = t * t * (3 - 2 * t);
return (gap / 2) + (BASE_HALF - gap / 2) * eased;
}
function buildVesselPaths(gap) {
let top = 'M ' + VESSEL_LEFT + ' ' + (110 - BASE_HALF);
let bottom = 'M ' + VESSEL_LEFT + ' ' + (110 + BASE_HALF);
let fill = 'M ' + VESSEL_LEFT + ' ' + (110 - BASE_HALF);
for (let x = VESSEL_LEFT; x <= VESSEL_RIGHT; x += 4) {
const h = halfGapAt(x, gap);
top += ' L ' + x + ' ' + (110 - h);
fill += ' L ' + x + ' ' + (110 - h);
}
for (let x = VESSEL_RIGHT; x >= VESSEL_LEFT; x -= 4) {
const h = halfGapAt(x, gap);
bottom += ' L ' + x + ' ' + (110 + h);
fill += ' L ' + x + ' ' + (110 + h);
}
fill += ' Z';
return { top, bottom, fill };
}
function flowFor(push, gap) {
const radius = gap / 2;
const resistance = 1 / Math.pow(radius, 4);
const K = 1200;
return push / (resistance * K);
}
function wordFor(v, lowMax, highMin, low, mid, high) {
if (v <= lowMax) return low;
if (v >= highMin) return high;
return mid;
}
function makeParticles(group, n, color) {
let arr = [];
for (let i = 0; i < n; i++) {
const c = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
c.setAttribute('r', 5);
c.setAttribute('fill', color);
group.appendChild(c);
arr.push({ el: c, t: i / n });
}
return arr;
}
(function flowModule() {
const pushSlider = document.getElementById('push-slider');
const squeezeSlider = document.getElementById('squeeze-slider');
const pushOut = document.getElementById('push-out');
const squeezeOut = document.getElementById('squeeze-out');
const flowWord = document.getElementById('flow-word');
const vesselTop = document.getElementById('vessel-top');
const vesselBottom = document.getElementById('vessel-bottom');
const vesselFill = document.getElementById('vessel-fill');
const particleGroup = document.getElementById('particles');
const particles = makeParticles(particleGroup, 10, '#E24B4A');
function updateVessel() {
const gap = parseFloat(squeezeSlider.value);
const paths = buildVesselPaths(gap);
vesselTop.setAttribute('d', paths.top);
vesselBottom.setAttribute('d', paths.bottom);
vesselFill.setAttribute('d', paths.fill);
}
function updateReadout() {
const push = parseFloat(pushSlider.value);
const gap = parseFloat(squeezeSlider.value);
pushOut.textContent = wordFor(push, 30, 70, 'light', 'medium', 'strong');
squeezeOut.textContent = wordFor(gap, 10, 22, 'tight', 'medium', 'loose');
const Q = flowFor(push, gap);
flowWord.textContent = wordFor(Q, 0.6, 4, 'barely any flow', 'medium flow', 'fast flow');
updateVessel();
}
pushSlider.addEventListener('input', updateReadout);
squeezeSlider.addEventListener('input', updateReadout);
document.getElementById('tighten-btn').addEventListener('click', () => {
squeezeSlider.value = 6;
updateReadout();
});
document.getElementById('loosen-btn').addEventListener('click', () => {
squeezeSlider.value = 30;
updateReadout();
});
function animate() {
const push = parseFloat(pushSlider.value);
const gap = parseFloat(squeezeSlider.value);
const Q = flowFor(push, gap);
const speed = Math.max(0.02, Math.min(1.4, Q / 3));
particles.forEach(p => {
p.t += speed * 0.006;
if (p.t > 1) p.t -= 1;
const x = VESSEL_LEFT + p.t * (VESSEL_RIGHT - VESSEL_LEFT);
const h = halfGapAt(x, gap);
p.el.setAttribute('cx', x);
p.el.setAttribute('cy', 110);
p.el.setAttribute('r', Math.max(2.5, Math.min(5, h / 6)));
});
requestAnimationFrame(animate);
}
updateReadout();
animate();
})();
let flowTrapAnswered = false;
document.querySelectorAll('#trap-options .opt-btn').forEach(btn => {
btn.addEventListener('click', () => {
if (flowTrapAnswered) return;
flowTrapAnswered = true;
document.querySelectorAll('#trap-options .opt-btn').forEach(b => b.disabled = true);
const correct = btn.dataset.correct === 'true';
btn.classList.add(correct ? 'correct' : 'wrong');
if (!correct) {
document.querySelector('#trap-options .opt-btn[data-correct="true"]').classList.add('correct');
}
playFlowReplay();
setDone('flow');
document.getElementById('flow-done-btn').classList.remove('hidden');
});
});
document.getElementById('flow-done-btn').addEventListener('click', () => showScreen('home'));
function playFlowReplay() {
const replaySvg = document.getElementById('replay-svg');
const replayTop = document.getElementById('replay-top');
const replayBottom = document.getElementById('replay-bottom');
const replayFill = document.getElementById('replay-fill');
const replayGroup = document.getElementById('replay-particles');
replaySvg.classList.remove('hidden');
replayGroup.innerHTML = '';
const particles = makeParticles(replayGroup, 10, '#E24B4A');
let frame = 0;
function step() {
frame++;
const t = Math.min(1, frame / 100);
const gap = 30 - (30 - 6) * t;
const paths = buildVesselPaths(gap);
replayTop.setAttribute('d', paths.top);
replayBottom.setAttribute('d', paths.bottom);
replayFill.setAttribute('d', paths.fill);
const Q = flowFor(50, gap);
const speed = Math.max(0.02, Math.min(1.4, Q / 3));
particles.forEach(p => {
p.t += speed * 0.006;
if (p.t > 1) p.t -= 1;
const x = VESSEL_LEFT + p.t * (VESSEL_RIGHT - VESSEL_LEFT);
const h = halfGapAt(x, gap);
p.el.setAttribute('cx', x);
p.el.setAttribute('cy', 110);
p.el.setAttribute('r', Math.max(2.5, Math.min(5, h / 6)));
});
if (frame < 260) requestAnimationFrame(step);
}
step();
document.getElementById('trap-result').classList.remove('hidden');
document.getElementById('trap-result').textContent = 'Watch how fast the flow drops as the vessel narrows, even though the push from the heart never changed. Blood flow depends on the radius raised to the fourth power, so even a small squeeze causes a huge drop, not a small one.';
}
document.getElementById('loop-start-btn').addEventListener('click', () => showScreen('loop-module'));
document.getElementById('to-loop-trap-btn').addEventListener('click', () => showScreen('loop-trap'));
(function loopModule() {
const group = document.getElementById('loop-particles');
const playBtn = document.getElementById('loop-play-btn');
let playing = false;
let particles = [];
function makeLoopParticle(color) {
const c = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
c.setAttribute('r', 5);
c.setAttribute('fill', color);
group.appendChild(c);
return c;
}
const heartCenter = { x: 190, y: 125 };
const lungCenter = { x: 190, y: 42 };
const bodyCenter = { x: 190, y: 217 };
function lerp(a, b, t) { return a + (b - a) * t; }
playBtn.addEventListener('click', () => {
if (playing) return;
playing = true;
group.innerHTML = '';
const blue = makeLoopParticle('#378ADD');
const red = makeLoopParticle('#E24B4A');
let t = 0;
function step() {
t += 0.012;
const cycle = t % 2;
if (cycle < 1) {
const localT = cycle;
blue.setAttribute('cx', lerp(heartCenter.x, lungCenter.x, localT));
blue.setAttribute('cy', lerp(heartCenter.y - 20, lungCenter.y + 20, localT));
red.setAttribute('cx', lerp(heartCenter.x, bodyCenter.x, 0));
red.setAttribute('cy', lerp(heartCenter.y + 20, bodyCenter.y - 20, 0));
} else {
const localT = cycle - 1;
blue.setAttribute('cx', lerp(lungCenter.x, heartCenter.x, localT));
blue.setAttribute('cy', lerp(lungCenter.y + 20, heartCenter.y - 20, localT));
red.setAttribute('cx', lerp(heartCenter.x, bodyCenter.x, localT));
red.setAttribute('cy', lerp(heartCenter.y + 20, bodyCenter.y - 20, localT));
}
if (t < 6) {
requestAnimationFrame(step);
} else {
playing = false;
}
}
step();
});
})();
let loopTrapAnswered = false;
document.querySelectorAll('#loop-trap-options .opt-btn').forEach(btn => {
btn.addEventListener('click', () => {
if (loopTrapAnswered) return;
loopTrapAnswered = true;
document.querySelectorAll('#loop-trap-options .opt-btn').forEach(b => b.disabled = true);
const correct = btn.dataset.correct === 'true';
btn.classList.add(correct ? 'correct' : 'wrong');
if (!correct) {
document.querySelector('#loop-trap-options .opt-btn[data-correct="true"]').classList.add('correct');
}
const result = document.getElementById('loop-trap-result');
result.classList.remove('hidden');
result.textContent = 'Blood always returns to the heart between circuits. It never travels lungs to body directly. The heart is the hub for both loops, the pulmonary circuit (heart to lungs to heart) and the systemic circuit (heart to body to heart), not a single continuous path.';
setDone('loop');
document.getElementById('loop-done-btn').classList.remove('hidden');
});
});
document.getElementById('loop-done-btn').addEventListener('click', () => showScreen('home'));