const ECG_WINDOW_S=3,ECG_MORPH={normal:{pAmp:.15,pDur:.09,notch:!1,q:-.1,r:1,s:-.25,qrsDur:.08,st:0,stSlope:0,tAmp:.3,label:["\u7AA6\u6027\u5FC3\u5F8B \xB7 \u6B63\u5E38","Sinus rhythm \xB7 normal"]},hf:{pAmp:.15,pDur:.1,notch:!1,q:-.05,r:.8,s:-.2,qrsDur:.12,st:-.05,stSlope:0,tAmp:.12,label:["\u7AA6\u6027\u5FC3\u52A8\u8FC7\u901F \xB7 QRS \u589E\u5BBD\u3001T \u4F4E\u5E73\uFF08\u6269\u5F20\u7684\u5FC3\u5BA4\u4F20\u5BFC\u53D8\u6162\uFF09","Sinus tachycardia \xB7 wide QRS, flat T (dilated ventricle conducts slowly)"]},ms:{pAmp:.18,pDur:.12,notch:!0,q:-.08,r:.9,s:-.3,qrsDur:.09,st:0,stSlope:0,tAmp:.25,label:["P \u6CE2\u589E\u5BBD\u3001\u5207\u8FF9\uFF08P mitrale\uFF0C\u5DE6\u623F\u6269\u5927\uFF09\xB7 \u7AA6\u5F8B\uFF08MS \u5E38\u5408\u5E76\u623F\u98A4\uFF0C\u8FD9\u91CC\u4FDD\u6301\u7AA6\u5F8B\u4E0E\u52A8\u753B\u540C\u6B65\uFF09","Broad notched P (P mitrale, LA enlargement) \xB7 sinus (AF is common in MS; kept in sinus to stay in step with the animation)"]},mr:{pAmp:.18,pDur:.12,notch:!0,q:-.15,r:1.3,s:-.25,qrsDur:.09,st:0,stSlope:0,tAmp:.4,label:["P mitrale + \u5BB9\u91CF\u8D1F\u8377\u6027\u5DE6\u5BA4\u80A5\u539A\uFF08\u9AD8 R\u3001\u9AD8 T\uFF09","P mitrale + volume-overload LVH (tall R, tall T)"]},as:{pAmp:.15,pDur:.1,notch:!1,q:-.08,r:1.6,s:-.3,qrsDur:.1,st:-.12,stSlope:-.08,tAmp:-.25,label:["\u5DE6\u5BA4\u80A5\u539A + \u52B3\u635F\uFF08\u9AD8 R\uFF0CST \u538B\u4F4E\u3001T \u5012\u7F6E\uFF09","LVH with strain (tall R, ST depression, inverted T)"]},ar:{pAmp:.15,pDur:.1,notch:!1,q:-.25,r:1.5,s:-.2,qrsDur:.1,st:0,stSlope:0,tAmp:.45,label:["\u5BB9\u91CF\u8D1F\u8377\u6027\u5DE6\u5BA4\u80A5\u539A\uFF08\u6DF1 Q\u3001\u9AD8 R\u3001\u9AD8\u5C16 T\uFF09","Volume-overload LVH (deep Q, tall R, tall peaked T)"]},hfpef:{pAmp:.18,pDur:.11,notch:!0,q:-.08,r:1.55,s:-.3,qrsDur:.1,st:-.08,stSlope:-.05,tAmp:-.15,label:["\u5DE6\u5BA4\u80A5\u539A + \u5DE6\u623F\u8D1F\u8377\uFF08P \u6CE2\u589E\u5BBD\u3001\u5207\u8FF9\uFF09\u3001\u8F7B\u5EA6\u52B3\u635F\uFF08\u9AD8\u8840\u538B\u5FC3\u810F\u75C5\uFF09","LVH + left atrial abnormality (broad notched P), mild strain (hypertensive heart)"]},mi:{pAmp:.15,pDur:.09,notch:!1,q:-.45,r:.7,s:-.1,qrsDur:.09,st:.35,stSlope:-.05,tAmp:.45,label:["\u6025\u6027\u4E0B\u58C1\u5FC3\u6897\uFF1A\u5BFC\u8054 II ST \u6BB5\u5F13\u80CC\u62AC\u9AD8\u3001\u75C5\u7406\u6027 Q \u6CE2\u3001\u9AD8\u8038 T","Acute inferior STEMI: convex ST elevation, pathological Q, tall T in lead II"]},exercise:{pAmp:.18,pDur:.08,notch:!1,q:-.1,r:1,s:-.25,qrsDur:.08,st:.02,stSlope:.03,tAmp:.35,label:["\u7AA6\u6027\u5FC3\u52A8\u8FC7\u901F\uFF08\u8FD0\u52A8\uFF09\xB7 \u5F62\u6001\u6B63\u5E38\uFF0CP \u6CE2\u7565\u9AD8\uFF0CQT \u968F\u5FC3\u7387\u7F29\u77ED","Sinus tachycardia (exercise) \xB7 normal morphology, slightly taller P, QT shortens with rate"]},shock:{pAmp:.15,pDur:.09,notch:!1,q:-.1,r:.9,s:-.25,qrsDur:.08,st:0,stSlope:0,tAmp:.22,label:["\u7AA6\u6027\u5FC3\u52A8\u8FC7\u901F\uFF08\u5931\u8840\u4EE3\u507F\uFF09\xB7 \u5F62\u6001\u6B63\u5E38\uFF1B\u4F4E\u704C\u6CE8\u52A0\u91CD\u65F6\u51FA\u73B0 ST \u6539\u53D8","Sinus tachycardia (compensating for blood loss) \xB7 normal morphology; ST changes appear as hypoperfusion worsens"]},athlete:{pAmp:.15,pDur:.1,notch:!1,q:-.1,r:1.45,s:-.3,qrsDur:.09,st:.08,stSlope:0,tAmp:.5,label:["\u7AA6\u6027\u5FC3\u52A8\u8FC7\u7F13 + \u9AD8\u7535\u538B\u3001\u65E9\u671F\u590D\u6781\uFF08ST \u8F7B\u5EA6\u62AC\u9AD8\u3001\u9AD8\u5C16 T\uFF09\uFF1A\u8FD0\u52A8\u5458\u5FC3\u810F\u7684\u6B63\u5E38\u53D8\u5F02","Sinus bradycardia + tall voltages, early repolarisation (slight ST elevation, tall peaked T): a normal variant in athletes"]}},ECG_INFO={P:["P \u6CE2 = \u5FC3\u623F\u53BB\u6781\u5316 \u2192 \u89E6\u53D1\u5FC3\u623F\u6536\u7F29\uFF08\u7B2C 1 \u65F6\u76F8\uFF09\uFF0C\u6324\u5165\u6700\u540E\u7EA6\u56DB\u5206\u4E4B\u4E00\u7684\u5145\u76C8\u91CF\u3002\u4E8C\u5C16\u74E3\u72ED\u7A84 / \u5173\u95ED\u4E0D\u5168\u5DE6\u623F\u589E\u5927\u65F6\uFF0CP \u6CE2\u589E\u5BBD\u5E76\u51FA\u73B0\u5207\u8FF9\uFF08P mitrale\uFF09\u3002","P wave = atrial depolarisation \u2192 triggers atrial systole (phase 1), the last quarter of filling. With LA enlargement (MS / MR) the P wave widens and notches (P mitrale)."],QRS:["QRS \u6CE2 = \u5FC3\u5BA4\u53BB\u6781\u5316 \u2192 \u7D27\u63A5\u7B49\u5BB9\u6536\u7F29\u671F\uFF08\u7B2C 2 \u65F6\u76F8\uFF09\uFF1BR \u6CE2\u9876\u9644\u8FD1\u623F\u5BA4\u74E3\u5173\u95ED \u2192 \u7B2C\u4E00\u5FC3\u97F3 S1\u3002\u4E3B\u52A8\u8109\u74E3\u72ED\u7A84\u7B49\u5DE6\u5BA4\u80A5\u539A\u65F6 R \u6CE2\u9AD8\u5927\uFF1B\u6269\u5F20\u7684\u5FC3\u8870\u5FC3\u5BA4 QRS \u589E\u5BBD\u3002","QRS = ventricular depolarisation \u2192 isovolumic contraction follows (phase 2); the AV valves close near the R peak \u2192 S1. LVH (e.g. aortic stenosis) gives tall R waves; a dilated failing ventricle widens the QRS."],T:["T \u6CE2 = \u5FC3\u5BA4\u590D\u6781 \u2192 \u5BF9\u5E94\u51CF\u6162\u5C04\u8840\u672B\u5230\u7B49\u5BB9\u8212\u5F20\u671F\uFF08\u7B2C 4\u20135 \u65F6\u76F8\uFF09\uFF1BT \u6CE2\u7EC8\u70B9\u5904\u534A\u6708\u74E3\u5173\u95ED \u2192 \u7B2C\u4E8C\u5FC3\u97F3 S2\u3002\u5DE6\u5BA4\u52B3\u635F\u65F6 ST \u538B\u4F4E\u3001T \u6CE2\u5012\u7F6E\u3002","T wave = ventricular repolarisation \u2192 late ejection into isovolumic relaxation (phases 4\u20135); the semilunar valves close at its end \u2192 S2. LV strain depresses the ST segment and inverts T."],PR:["PR \u95F4\u671F\uFF08\u7EA6 0.12\u20130.20 s\uFF09= \u5FC3\u623F\u53BB\u6781\u5316\u5230\u5FC3\u5BA4\u53BB\u6781\u5316\uFF0C\u4E3B\u8981\u662F\u623F\u5BA4\u7ED3\u7684\u4F20\u5BFC\u5EF6\u6401\uFF0C\u4FDD\u8BC1\u5FC3\u623F\u5148\u6536\u7F29\u3001\u5FC3\u5BA4\u5145\u76C8\u5B8C\u518D\u6536\u7F29\u3002","PR interval (\u2248 0.12\u20130.20 s) = atrial to ventricular depolarisation, mostly AV-nodal delay, so the atria contract and finish filling the ventricles before they contract."],QT:["QT \u95F4\u671F = \u5FC3\u5BA4\u53BB\u6781\u5316\u5230\u590D\u6781\u7ED3\u675F\uFF0C\u7EA6\u7B49\u4E8E\u6574\u4E2A\u5FC3\u5BA4\u6536\u7F29\u671F\u3002\u5FC3\u7387\u8D8A\u5FEB QT \u8D8A\u77ED\uFF08\u62D6\u5FC3\u7387\u6ED1\u5757\u770B\u8BFB\u6570\u53D8\u5316\uFF09\uFF1B\u4E34\u5E8A\u7528\u7387\u6821\u6B63\u7684 QTc\uFF08Bazett\uFF09\u3002","QT interval = ventricular depolarisation to the end of repolarisation \u2248 the whole of ventricular systole. Faster rate \u2192 shorter QT (drag the heart-rate slider); clinically rate-corrected as QTc (Bazett)."]};function ecgGauss(e,t,o){const n=(e-t)/o;return Math.exp(-.5*n*n)}function ecgTiming(e,t){const o=ECG_MORPH[e.id]||ECG_MORPH.normal,n=60/t,s=O=>O/n,l=PHASE_BOUNDS,a=l[1]-s(.01),p=a-s(.45*o.qrsDur),u=a+s(.55*o.qrsDur),c=1-s(.01),h=c-s(o.pDur),f=l[4]+s(.04+.1*Math.max(0,n-.8)),d=.16*Math.sqrt(n/.8),S=f-s(d),g=(p-h+1)%1*n,P=(f-p)*n;return{m:o,rr:n,rPk:a,qOn:p,sEnd:u,pOn:h,pOff:c,tOn:S,tOff:f,pr:g,qrs:o.qrsDur,qt:P,qtc:P/Math.sqrt(n)}}function ecgValue(e,t){const o=t.m;let n=0;const s=e<.5?e+1:e,l=(t.pOn+t.pOff)/2,a=t.pOff-t.pOn;o.notch?n+=o.pAmp*(.85*ecgGauss(s,l-.22*a,a*.2)+ecgGauss(s,l+.2*a,a*.2)):n+=o.pAmp*ecgGauss(s,l,a*.28);const p=t.rPk-t.qOn,u=t.sEnd-t.rPk;if(n+=o.q*ecgGauss(e,t.rPk-.6*p,p*.26),n+=o.r*ecgGauss(e,t.rPk,p*.34),n+=o.s*ecgGauss(e,t.rPk+.55*u,u*.28),e>t.sEnd&&e<t.tOff){const f=Math.min(1,(e-t.sEnd)/Math.max(1e-6,t.tOn-t.sEnd)),d=o.st+o.stSlope*f,S=e<=t.tOn?1:Math.max(0,1-(e-t.tOn)/((t.tOff-t.tOn)*.6));n+=d*S}const c=(t.tOn+t.tOff)/2,h=t.tOff-t.tOn;return n+=o.tAmp*ecgGauss(e,c,h*.27),n}function buildEcgCurve(e,t,o){const n=ecgTiming(e,t),s=new Array(o);for(let l=0;l<o;l++)s[l]=ecgValue(l/o,n);return s}function ecgSpan(e,t){switch(e){case"P":return[t.pOn,t.pOff];case"QRS":return[t.qOn,t.sEnd];case"T":return[t.tOn,t.tOff];case"PR":return[t.pOn,t.qOn+1];case"QT":return[t.qOn,t.tOff];default:return null}}function drawEcgMonitor(e,t,o,n,{T:s,tAbs:l,lang:a,activeWave:p,flash:u}){const c=s.m,h=s.rr,f=3/h,d=o/f,S=d/h,g=n*.58,P=n*.26;e.setTransform(t,0,0,t,0,0),e.clearRect(0,0,o,n);const O=.04*S,D=.2*S;e.lineWidth=1,e.strokeStyle="rgba(34,49,63,0.10)",e.beginPath();for(let r=o;r>=0;r-=O)e.moveTo(r,0),e.lineTo(r,n);for(let r=0;r<=n;r+=O)e.moveTo(0,r),e.lineTo(o,r);e.stroke(),e.strokeStyle="rgba(34,49,63,0.20)",e.beginPath();for(let r=o;r>=0;r-=D)e.moveTo(r,0),e.lineTo(r,n);for(let r=0;r<=n;r+=D)e.moveTo(0,r),e.lineTo(o,r);e.stroke();const q=r=>o-(l-r)*d,b=r=>g-ecgValue((r%1+1)%1,s)*P,T=1/400;e.strokeStyle="#4A7C59",e.lineWidth=1.8,e.lineJoin="round",e.shadowColor="rgba(74,124,89,0.4)",e.shadowBlur=3,e.beginPath();let M=!0,k=g;for(let r=l-f;r<=l+1e-9;r+=T){const m=q(r),i=b(r);k=i,M?(e.moveTo(m,i),M=!1):e.lineTo(m,i)}e.stroke(),e.shadowBlur=0;const R=Math.floor(l-f)-1,w=Math.floor(l)+1,v=p?ecgSpan(p,s):null;if(v){e.strokeStyle="#B8860B",e.lineWidth=3,e.shadowColor="rgba(184,134,11,0.55)",e.shadowBlur=6;for(let r=R;r<=w;r++){e.beginPath();let m=!0;for(let i=v[0];i<=v[1];i+=.0025){const y=r+i;if(y>l)break;const A=q(y);if(A<-4)continue;const Q=b(y);m?(e.moveTo(A,Q),m=!1):e.lineTo(A,Q)}e.stroke()}e.shadowBlur=0}e.font='10px "JetBrains Mono",ui-monospace,"SF Mono",Menlo,Consolas,"Liberation Mono",monospace',e.textAlign="center";for(let r=R;r<=w;r++){const m=q(r+s.rPk),i=q(r+s.tOff);r+s.rPk<=l&&m>16&&m<o-2&&(e.fillStyle="#A23B36",e.fillText("\u266AS1",m,Math.max(10,g-P*Math.max(.3,c.r)-5))),r+s.tOff<=l&&i>16&&i<o-2&&(e.fillStyle="#1E3A5F",e.fillText("\u266AS2",i,g-P*.55-5))}e.fillStyle="#3D6B4F",e.shadowColor="#4A7C59",e.shadowBlur=8,e.beginPath(),e.arc(o-2,k,2.6,0,Math.PI*2),e.fill(),e.shadowBlur=0,e.fillStyle=`rgba(74,124,89,${.22+.78*(u||0)})`,e.beginPath(),e.arc(12,12,4+1.5*(u||0),0,Math.PI*2),e.fill(),e.fillStyle="rgba(34,49,63,0.45)",e.font='9px "JetBrains Mono",ui-monospace,"SF Mono",Menlo,Consolas,"Liberation Mono",monospace',e.textAlign="right",e.fillText(a==="en"?"25 mm/s \xB7 big square 0.2 s \xB7 1 mV":"25 mm/s \xB7 \u5927\u683C 0.2 s \xB7 1 mV",o-6,n-6),e.textAlign="left"}Object.assign(window,{ECG_MORPH,ECG_INFO,ECG_WINDOW_S:3,ecgTiming,ecgValue,buildEcgCurve,ecgSpan,drawEcgMonitor});
