UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 3.31 kB
import{Milliseconds as t}from"../../../../../core/time.js";import{imageTypeToCanvas as e}from"../../../../../geometry/support/meshUtils/exporters/gltf/imageutils.js";import{AnimatedSymbolRepeatType as r}from"../../../../../symbols/cim/enums.js";import{getMaterialGroup as i,getRandomValue as a}from"../grouping.js";function n(e){return t(e.frameDurations.reduce(((t,e)=>t+e),0))}function s(t){const{width:e,height:r}=t;return{frameDurations:t.frameDurations.reverse(),getFrame:e=>{const r=t.frameDurations.length-1-e;return t.getFrame(r)},width:e,height:r}}function o(e,r){const{width:i,height:a,getFrame:s}=e,o=r/n(e);return{frameDurations:e.frameDurations.map((e=>t(e*o))),getFrame:s,width:i,height:a}}function m(e,r){const{width:i,height:a,getFrame:n}=e,s=e.frameDurations.slice(),o=s.shift();return s.unshift(t(o+r)),{frameDurations:s,getFrame:n,width:i,height:a}}function h(e,r){const{width:i,height:a,getFrame:n}=e,s=e.frameDurations.slice(),o=s.pop();return s.push(t(o+r)),{frameDurations:s,getFrame:n,width:i,height:a}}class c{constructor(t,e,r,i){this._animation=t,this._repeatType=r,this._onFrameData=i,this._direction=1,this._currentFrame=0,this.timeToFrame=this._animation.frameDurations[this._currentFrame];let a=0;for(;e>a;)a+=this.timeToFrame,this.nextFrame();const n=this._animation.getFrame(this._currentFrame);this._onFrameData(n)}nextFrame(){if(this._currentFrame+=this._direction,this._direction>0){if(this._currentFrame===this._animation.frameDurations.length)switch(this._repeatType){case r.None:this._currentFrame-=this._direction;break;case r.Loop:this._currentFrame=0;break;case r.Oscillate:this._currentFrame-=this._direction,this._direction=-1}}else if(-1===this._currentFrame)switch(this._repeatType){case r.None:this._currentFrame-=this._direction;break;case r.Loop:this._currentFrame=this._animation.frameDurations.length-1;break;case r.Oscillate:this._currentFrame-=this._direction,this._direction=1}this.timeToFrame=this._animation.frameDurations[this._currentFrame];const t=this._animation.getFrame(this._currentFrame);this._onFrameData(t)}}function u(e,u,f,l){let g,{repeatType:F}=u;if(null==F&&(F=r.Loop),!0===u.reverseAnimation&&(e=s(e)),null!=u.duration&&(e=o(e,t(1e3*u.duration))),null!=u.repeatDelay){const i=1e3*u.repeatDelay;F===r.Loop?e=h(e,t(i)):F===r.Oscillate&&(e=m(h(e,t(i/2)),t(i/2)))}if(null!=u.startTimeOffset)g=t(1e3*u.startTimeOffset);else if(null!=u.randomizeStartTime){const r=i(f),s=82749913,o=null!=u.randomizeStartSeed?u.randomizeStartSeed:s,m=a(r,o);g=t(m*n(e))}else g=t(0);return new c(e,g,F,l)}function f(t,e,r,i){const a=null==e.playAnimation||e.playAnimation,n=u(t,e,r,i);let s,o=n.timeToFrame;function m(){s=a?setTimeout((()=>{n.nextFrame(),o=n.timeToFrame,m()}),o):void 0}return m(),{remove:()=>{a&&clearTimeout(s)}}}const l=document.createElement("canvas"),g=l.getContext("2d");function F(t,r,i){l.width=r,l.height=i;const a=[],n=t.frameDurations.length;for(let s=0;s<n;s++){const n=t.getFrame(s);g.clearRect(0,0,r,i),n instanceof ImageData?g.drawImage(e(n),0,0,r,i):g.drawImage(n,0,0,r,i),a.push(g.getImageData(0,0,r,i))}return{width:r,height:i,frameDurations:t.frameDurations,getFrame:t=>a[t]}}export{c as Player,o as adjustDuration,h as appendDelay,u as createPlayer,n as getDuration,f as play,m as prependDelay,F as resize,s as reverse};