2 lines
1.2 KiB
JavaScript
2 lines
1.2 KiB
JavaScript
(function(){"use strict";function L(){return performance.now()*.001}let _=0,r=0,o=.5;const h=new Map,m=.1,M=new BroadcastChannel("strudeltick"),b=(s,e)=>{M.postMessage({type:s,payload:e})},C=E(L,(s,e,t,n)=>{const i=r*e,d=s-n,c=n+d,u=i*o,l=_+u,g=n-c-e,f=e*o,p=l+f,y=l+g*o;b("tick",{begin:l,end:p,cps:o,time:n,cycle:y}),r++},m);let k=!1;const I=s=>{h.set(s,{started:!0}),!k&&(C.start(),k=!0)},A=async s=>{h.set(s,{started:!1});const e=Array.from(h.values()).some(t=>t.started);!k||e||(C.stop(),w(0),k=!1)},w=s=>{r=0,_=s},D=s=>{const{type:e,payload:t}=s;switch(e){case"cpschange":{if(t.cps!==o){const n=r*m;_=_+n*o,o=t.cps,r=0}break}case"setcycle":{w(t.cycle);break}case"toggle":{t.started?I(s.id):A(s.id);break}}};self.onconnect=function(s){const e=s.ports[0];e.addEventListener("message",function(t){D(t.data)}),e.start()};function E(s,e,t=.05,n=.1,i=.1){let d=0,c=0,u=10**4,l=.01;const g=a=>t=a(t);i=i||n/2;const f=()=>{const a=s(),O=a+n+i;for(c===0&&(c=a+l);c<O;)c=Math.round(c*u)/u,c>=a&&e(c,t,d,a),c<a&&console.log("TOO LATE",c),c+=t,d++};let p;const y=()=>{T(),f(),p=setInterval(f,n*1e3)},T=()=>p!==void 0&&clearInterval(p);return{setDuration:g,start:y,stop:()=>{d=0,c=0,T()},pause:()=>T(),duration:t,interval:n,getPhase:()=>c,minLatency:l}}})();
|