-
Notifications
You must be signed in to change notification settings - Fork 2
/
EasySprite.min.js
1 lines (1 loc) · 28 KB
/
EasySprite.min.js
1
!function(){"use strict";var s=[];function l(){return alert("'"+this.name+"' failed to load."),!0}function _(){this.x=0,this.y=0,this.z=0,this.width=0,this.height=0,this.remove=!1,this.anchor=[],this.anchor.x=0,this.anchor.y=0,this.rotate=[],this.rotate.angle=0,this.rotate.relatif=!1,this.rotate.vAngle=0,this.clip=[],this.clip.x=0,this.clip.y=0,this.clip.width=0,this.clip.height=0,this.clip.frame=1,this.clip.speed=0,this.flip=[],this.flip.h=1,this.flip.v=1,this.scale=[],this.scale.width=0,this.scale.height=0,this.scroll=[],this.scroll.x=0,this.scroll.y=0,this.scroll.PositionX=0,this.scroll.PositionY=0,this.opacity=1,this.velocity=[],this.velocity.x=0,this.velocity.y=0,this.animations=[],this.load=!1,this.data=0,this.n=0,this.onClick=!1}function c(e,n,t,o,i){void 0===n&&(n=e.x+e.velocity.x),void 0===t&&(t=e.y+e.velocity.y),void 0!==o&&(e.scale.width=o),void 0!==i&&(e.scale.height=i),void 0===e.scale.width&&(e.scale.width=e.width),void 0===e.scale.height&&(e.scale.height=e.height),e.remove=!1,e.x=n,e.y=t,360<e.rotate.vAngle+e.rotate.angle&&(e.rotate.vAngle-=360),e.rotate.relatif=!0,e.rotate.vAngle+=e.rotate.angle,s.context.save(),s.context.globalAlpha=e.opacity,s.context.translate(e.x,e.y),s.context.rotate(e.rotate.vAngle*s.TO_RADIANS),s.context.scale(e.flip.h,e.flip.v),s.context.drawImage(e.image,e.clip.x,e.clip.y,e.clip.width,e.clip.height,-e.scale.width*e.anchor.x,-e.scale.height*e.anchor.y,e.scale.width,e.scale.height),s.context.restore()}function d(e,n,t,o,i,r){void 0===o&&(o="Arial"),void 0===i&&(i=20),void 0===r&&(r="red"),s.context.font=i.toString()+"px "+o,s.context.fillStyle=r,s.context.fillText(e,n,t)}function t(e,n,t){var o=e.getBoundingClientRect(),e=o.width/e.width;return"mousemove"==t?{x:(n.clientX-o.left)/e,y:(n.clientY-o.top)/e}:{x:(n.targetTouches[0].pageX-o.left)/e,y:(n.targetTouches[0].pageY-o.top)/e}}function o(){if(0==s.mousehide)for(var e in s.inputs)if(n()>s.inputs[e].x&&n()<s.inputs[e].x+s.inputs[e].scale.width&&i()>s.inputs[e].y&&i()<s.inputs[e].y+s.inputs[e].scale.height)return e}function n(){return-1===s.mouseX?-1:s.mouseX}function i(){return-1===s.mouseY?-1:s.mouseY}s.canvas="game",s.initkeyboard=!1,s.initgamepad=!1,s.initmouse=!1,s.mousehide=!1,s.TO_RADIANS=Math.PI/180,s.lastCalledTime=0,s.fps=0,s.fpsCount=0,s.inputs=[],s.key={},window.requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,window.KEYPAD_LT=6,window.KEYPAD_LB=4,window.KEYPAD_RT=7,window.Keypad_RB=5,window.KEYPAD_Y=3,window.KEYPAD_X=2,window.KEYPAD_B=1,window.KEYPAD_A=0,window.KEYPAD_START=9,window.KEYPAD_SELECT=8,window.KEYPAD_UP=12,window.KEYPAD_DOWN=13,window.KEYPAD_LEFT=14,window.KEYPAD_RIGHT=15,window.KEY_BACKSPACE=8,window.KEY_TAB=9,window.KEY_ENTER=13,window.KEY_SHIFT=16,window.KEY_CTRL=17,window.KEY_ALT=18,window.KEY_PAUSE=19,window.KEY_CAPSLOCK=20,window.KEY_ESCAPE=27,window.KEY_SPACE=32,window.KEY_PAGEUP=33,window.KEY_PAGEDOWN=34,window.KEY_END=35,window.KEY_HOME=36,window.KEY_LEFT=37,window.KEY_UP=38,window.KEY_RIGHT=39,window.KEY_DOWN=40,window.KEY_INSERT=45,window.KEY_DELETE=46,window.KEY_0=48,window.KEY_1=49,window.KEY_2=50,window.KEY_3=51,window.KEY_4=52,window.KEY_5=53,window.KEY_6=54,window.KEY_7=55,window.KEY_8=56,window.KEY_9=57,window.KEY_A=65,window.KEY_B=66,window.KEY_C=67,window.KEY_D=68,window.KEY_E=69,window.KEY_F=70,window.KEY_G=71,window.KEY_H=72,window.KEY_I=73,window.KEY_J=74,window.KEY_K=75,window.KEY_L=76,window.KEY_M=77,window.KEY_N=78,window.KEY_O=79,window.KEY_P=80,window.KEY_Q=81,window.KEY_R=82,window.KEY_S=83,window.KEY_T=84,window.KEY_U=85,window.KEY_V=86,window.KEY_W=87,window.KEY_X=88,window.KEY_Y=89,window.KEY_Z=90,window.KEY_LEFTWINDOW=91,window.KEY_RIGHTWINDOW=92,window.KEY_SELECT=93,window.KEY_NUMPAD0=96,window.KEY_NUMPAD1=97,window.KEY_NUMPAD2=98,window.KEY_NUMPAD3=99,window.KEY_NUMPAD4=100,window.KEY_NUMPAD5=101,window.KEY_NUMPAD6=102,window.KEY_NUMPAD7=103,window.KEY_NUMPAD8=104,window.KEY_NUMPAD9=105,window.KEY_MULTIPLY=106,window.KEY_ADD=107,window.KEY_SUBTRACT=109,window.KEY_DECIMALPOINT=110,window.KEY_DIVIDE=111,window.KEY_F1=112,window.KEY_F2=113,window.KEY_F3=114,window.KEY_F4=115,window.KEY_F5=116,window.KEY_F6=117,window.KEY_F7=118,window.KEY_F8=119,window.KEY_F9=120,window.KEY_F10=121,window.KEY_F11=122,window.KEY_F12=123,window.KEY_NUMLOCK=144,window.FPS=function(){return s.fpsCount+=1,1e3<=Date.now()-s.lastCalledTime&&(s.lastCalledTime=Date.now(),s.fps=s.fpsCount,s.fpsCount=0),s.fps},window.openScreen=function(e,n,t,o,i){var r=document.getElementById(e);return null===r?((r=document.createElement("canvas")).id=e,r.style.position="absolute",r.style.top=n.toString()+"px",r.style.left=t.toString()+"px",r.width=o,r.height=i,document.body.appendChild(r)):(0===o&&(o=r.width),0===i&&(i=r.height),r.width=o,r.height=i),s.canvas=e,s.x=n,s.y=t,s.width=o,s.height=i,s.context=r.getContext("2d"),s},window.clearScreen=function(e){s.context.fillStyle=e,s.context.fillRect(0,0,s.width,s.height)},window.screenWidth=function(){return s.width},window.screenHeight=function(){return s.height},window.centerScreen=function(){document.getElementById(s.canvas).style="display: block; margin: 0 auto;"},window.resizeScreen=function e(n){var t,o,i=document.getElementById(s.canvas),r=i.height/i.width;window.innerHeight/window.innerWidth<r?t=(o=window.innerHeight)/r:o=(t=window.innerWidth)*r,i.style.width=t+"px",i.style.height=o+"px",1==n&&((n=i.style).marginLeft="auto",n.marginRight="auto",(i=i.parentElement.style).textAlign="center",i.width="100%"),window.addEventListener("resize",e,!1)},window.debugScreen=function(e,n){void 0===e&&(e=20),void 0===n&&(n=40),d("x/y: "+s.x+"/"+s.y,e,n,"Arial",20,"red"),d("width/height: "+s.width+"/"+s.height,e,n+=25,"Arial",20,"red")},window.createSprite=function(e,n,t,o,i){void 0===o&&(o=!1),void 0===i&&(i=!1);var r=new _,d=new Image,a=document.createElement("canvas"),u=a.getContext("2d");return a.width=e,a.height=n,u.fillStyle=t,u.fillRect(0,0,e,n),d.onload=function(){r.width=e,r.height=n,r.clip.width=r.width,r.clip.height=r.height,r.scale.width=r.width,r.scale.height=r.height,0!=o&&o(r),0!=i&&((s.inputs[s.inputs.length]=r).onClick=i)},d.onerror=l,d.src=a.toDataURL("image/png"),r.image=d,r},window.loadSprite=function(e,n,t){void 0===n&&(n=!1),void 0===t&&(t=!1);var o=new Image,i=new _;return o.onload=function(){0==i.load&&(i.clip.width=this.width,i.clip.height=this.height,i.width=this.width,i.height=this.height,i.scale.width=this.width,i.scale.height=this.height,0!=n&&n(i),0!=t&&((s.inputs[s.inputs.length]=i).onClick=t))},o.onerror=l,o.name=e,o.src=e,i.image=o,i},window.anchorSprite=function(e,n,t){e.anchor.x=n,e.anchor.y=t},window.clipSprite=function(e,n,t,o,i){e.clip.x=n,e.clip.y=t,e.clip.width=o,e.clip.height=i,e.scale.width=o,e.scale.height=i},window.scaleSprite=function(e,n,t){e.scale.width=n,e.scale.height=t},window.copySprite=function(e){return Object.assign({},e)},window.spritePosition=function(e,n,t){void 0===t&&(t=0),e.x=n,e.y=t},window.spriteVelocity=function(e,n,t){void 0===t&&(t=0),e.velocity.x=n,e.velocity.y=t},window.displaySprite=c,window.flipSprite=function(e,n,t){void 0===t&&(t=1),e.flip.h=n,e.flip.v=t},window.opacitySprite=function(e,n){e.opacity=n},window.rotateSprite=function(e,n,t){!0===t?(e.rotate.angle=n,e.rotate.relatif=!0):(e.rotate.angle=n,e.rotate.relatif=!1)},window.scrollSprite=function(e,n,t,o,i,r,d){var a,u,s=r,l=d;for(void 0===r&&(r=e.scale.width),void 0===d&&(d=e.scale.height),e.scroll.PositionX+=o,e.scroll.PositionY+=i,e.scroll.PositionX>s&&(e.scroll.PositionX=0),e.scroll.PositionX<0&&(e.scroll.PositionX=s),e.scroll.PositionY>l&&(e.scroll.PositionY=0),e.scroll.PositionY<0&&(e.scroll.PositionY=l),a=0;a<2;a++)for(u=0;u<1;u++)c(e,n+s*a-e.scroll.PositionX,t+l*u-e.scroll.PositionY,r,d)},window.spriteCollision=function(e,n){if(0==e.remove&&0==n.remove&&e.x-e.clip.width*e.anchor.x<n.x+n.clip.width-n.clip.width*n.anchor.x&&e.x+e.clip.width-e.clip.width*e.anchor.x>n.x-n.clip.width*n.anchor.x&&e.y-e.clip.height*e.anchor.y<n.y+n.clip.height-n.clip.height*n.anchor.y&&e.clip.height+e.y-e.clip.height*e.anchor.y>n.y-n.clip.height*n.anchor.y)return!0},window.spriteWidth=function(e){return e.width},window.spriteHeight=function(e){return e.height},window.isSprite=function(e){return"object"==typeof e},window.removeSprite=function(e){e.remove=!0},window.debugSprite=function(e,n,t){var o,i="Arial",r="red";void 0===n&&(n=20),void 0===t&&(t=40),d("x/y: "+e.x+"/"+e.y,n,t,i,20,r),d("width/height: "+e.width+"/"+e.height,n,t+=25,i,20,r),d("scale width/height: "+e.scale.width+"/"+e.scale.height,n,t+=25,i,20,r),d("clip x/y: "+e.clip.x+"/"+e.clip.y,n,t+=25,i,20,r),d("clip width/height: "+e.clip.width+"/"+e.clip.height,n,t+=25,i,20,r),d("rotation: "+e.rotate.vAngle,n,t+=25,i,20,r),void 0!==e.currentAnimation&&(o=e.currentAnimation,d("current animation: "+e.currentAnimation,n,t+=25,i,20,r),d("current frame: "+e.animations[o].frame[e.animations[o].CurrentFrame],n,t+=25,i,20,r))},window.startDrawing=function(e){var n;return s.currentsprite=e,s.currentdraw=document.createElement("canvas"),s.currentdraw.id="temp",s.currentdraw.width=e.width,s.currentdraw.height=e.height,(n=s.currentdraw.getContext("2d")).drawImage(e.image,0,0),n},window.stopDrawing=function(){s.currentsprite.load=!0,s.currentsprite.image.src=s.currentdraw.toDataURL("image/png")},window.addAnimation=function(e,n,t,o,i,r){var d=new Array;d.frame=t,d.FrameRate=r,d.FrameTimeLife=0,d.CurrentAnimation=0,d.CurrentFrame=0,d.active=!1,e.animations[n]=d,e.clip.width=o,e.clip.height=i,e.scale.width=o,e.scale.height=i},window.animationActive=function(e,n){return e.animations[n].active},window.playAnimation=function(e,n){var t;void 0===e.animations[n]||(t=(new Date).getTime())-e.animations[n].FrameTimeLife>e.animations[n].FrameRate&&(e.animations[n].FrameTimeLife=t,e.currentAnimation=n,t=e.width/e.clip.width,e.clip.x=(e.animations[n].frame[e.animations[n].CurrentFrame]-Math.floor(e.animations[n].frame[e.animations[n].CurrentFrame]/t)*t)*e.clip.width,e.clip.y=Math.floor(e.animations[n].frame[e.animations[n].CurrentFrame]/t)*e.clip.height,e.animations[n].CurrentFrame<e.animations[n].frame.length-1?(e.animations[n].CurrentFrame+=1,e.animations[n].active=!0):(e.animations[n].CurrentFrame=0,e.animations[n].active=!1))},window.loadSound=function(e){return new Howl({src:[e]})},window.pauseSound=function(e){e.pause()},window.playSound=function(e,n){void 0===n&&(n=!1),!0===n&&(e._loop=!0),e.play()},window.resumeSound=function(e){e.stop().play()},window.soundLength=function(e){return e.duration()},window.soundVolume=function(e,n){void 0===n&&(n=1),e._volume=n},window.soundPosition=function(e,n){return e.playing()?void 0===n?e.seek():void e.seek(n):0},window.fadeSound=function(e,n,t,o){Sound.fade(n,t,o)},window.displayText=d,window.initGamepad=function(){s.keypad={_pressed:{}},window.addEventListener("gamepadconnected",()=>{s.initgamepad=!0,function n(){setTimeout(()=>{const e=navigator.getGamepads()[0];e.buttons.forEach((e,n)=>{e.pressed&&(s.keypad._pressed=n)}),n()},10)}()}),window.addEventListener("gamepaddisconnected",()=>{s.initgamepad=!1})},window.keypadPushed=function(e){return void 0===s.keypad?(d("initGamePad() is not call",10,20,"system",20,"rgba(255, 0, 0, 255)"),!1):1==s.initgamepad&&s.keypad._pressed==e?!(s.keypad._pressed=null):void 0},window.initKeyboard=function(){s.key={_released:{},_pressed:{},isDown:function(e){return this._pressed[e]},isReleased:function(e){return this._released[e]},onKeydown:function(e){this._pressed[e.keyCode]=!0,delete this._released[e.keyCode]},onKeyup:function(e){this._released[e.keyCode]=!0,delete this._pressed[e.keyCode]}},window.addEventListener("keyup",function(e){s.key.onKeyup(e)},!1),window.addEventListener("keydown",function(e){s.key.onKeydown(e)},!1),s.initkeyboard=!0},window.keyboardPushed=function(e){if(1==s.initkeyboard){if(1==s.key.isDown(e))return!0}else d("InitKeyboard() is not call",10,20,"system",20,"rgba(255, 0, 0, 255)")},window.keyboardReleased=function(e){if(1==s.initkeyboard){if(s.key.isReleased(e))return delete s.key._released[e],!0}else d("InitKeyboard() is not call",10,20,"system",20,"rgba(255, 0, 0, 255)")},window.isDown,window.initMouse=function(e){var n=document.getElementById(s.canvas);void 0===e&&(e=!1),1!=e&&1!=s.mousehide||(s.mousehide=!0,n.style.cursor="none"),n.addEventListener("click",function(e){s.mousebutton=!0;var n=o();n&&0==s.mousehide&&s.inputs[n].onClick(s.inputs[n])}),n.addEventListener("mousemove",function(e){e=t(n,e,"mousemove");s.mouseX=e.x,s.mouseY=e.y,0==s.mousehide&&(o()?n.style.cursor="pointer":n.style.cursor="auto")}),n.addEventListener("mouseout",function(){s.mouseX=-1,s.mouseY=-1}),n.addEventListener("mousedown",function(){s.mousebutton=!1,s.mousestate=!0}),n.addEventListener("mouseup",function(){s.mousestate=!1}),n.addEventListener("touchstart",function(){s.mousebutton=!1,s.mousestate=!0}),n.addEventListener("touchend",function(){s.mousestate=!1}),n.addEventListener("touchmove",function(e){e.preventDefault(),s.mousebutton=!1,s.mousestate=!0;e=t(n,e,"touchmove");s.mouseX=e.x,s.mouseY=e.y},!1),s.mouseX=-1,s.mouseY=-1,s.mousebutton=!1,s.mousestate=!1},window.mouseButton=function(){var e=s.mousebutton;return s.mousebutton=0,e},window.mouseState=function(){return s.mousestate},window.mouseX=n,window.mouseY=i,window.windowWidth=function(){return document.all?document.body.clientWidth:window.innerWidth},window.windowHeight=function(){return document.all?document.body.clientHeight:window.innerHeight},window.random=function(e,n){return n++,Math.floor(Math.random()*(n-e))+e},window.randomFloat=function(e,n){return Math.random()*(n-e)+e},window.rPad=function(e,n,t){return t=t||"0",(e+="").length>=n?e:new Array(n-e.length+1).join(t)+e}}(),function(){"use strict";var d,h=null,a=!0,r=!1;function e(){this.init()}function n(e){return e.src&&0!==e.src.length?void this.init(e):void console.error("An array of source files must be passed with any new Howl.")}(function(){try{"undefined"!=typeof AudioContext?h=new AudioContext:"undefined"!=typeof webkitAudioContext?h=new webkitAudioContext:a=!1}catch(e){a=!1}if(!a)if("undefined"!=typeof Audio)try{new Audio}catch(e){r=!0}else r=!0})(),a&&((d=void 0===h.createGain?h.createGainNode():h.createGain()).gain.value=1,d.connect(h.destination)),e.prototype={init:function(){var e=this||f;return e._codecs={},e._howls=[],e._muted=!1,e._volume=1,e.iOSAutoEnable=!0,e.noAudio=r,e.usingWebAudio=a,e.ctx=h,r||e._setupCodecs(),e},volume:function(e){var n=this||f;if(void 0!==(e=parseFloat(e))&&0<=e&&e<=1){n._volume=e,a&&(d.gain.value=e);for(var t=0;t<n._howls.length;t++)if(!n._howls[t]._webAudio)for(var o=n._howls[t]._getSoundIds(),i=0;i<o.length;i++){var r=n._howls[t]._soundById(o[i]);r&&r._node&&(r._node.volume=r._volume*e)}return n}return n._volume},mute:function(e){var n=this||f;n._muted=e,a&&(d.gain.value=e?0:n._volume);for(var t=0;t<n._howls.length;t++)if(!n._howls[t]._webAudio)for(var o=n._howls[t]._getSoundIds(),i=0;i<o.length;i++){var r=n._howls[t]._soundById(o[i]);r&&r._node&&(r._node.muted=!!e||r._muted)}return n},codecs:function(e){return(this||f)._codecs[e]},_setupCodecs:function(){var e=this||f,n=new Audio,t=n.canPlayType("audio/mpeg;").replace(/^no$/,"");return e._codecs={mp3:!(!t&&!n.canPlayType("audio/mp3;").replace(/^no$/,"")),mpeg:!!t,opus:!!n.canPlayType('audio/ogg; codecs="opus"').replace(/^no$/,""),ogg:!!n.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/,""),wav:!!n.canPlayType('audio/wav; codecs="1"').replace(/^no$/,""),aac:!!n.canPlayType("audio/aac;").replace(/^no$/,""),m4a:!!(n.canPlayType("audio/x-m4a;")||n.canPlayType("audio/m4a;")||n.canPlayType("audio/aac;")).replace(/^no$/,""),mp4:!!(n.canPlayType("audio/x-mp4;")||n.canPlayType("audio/mp4;")||n.canPlayType("audio/aac;")).replace(/^no$/,""),weba:!!n.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/,""),webm:!!n.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/,"")},e},_enableiOSAudio:function(){var t=this||f;if(!h||!t._iOSEnabled&&/iPhone|iPad|iPod/i.test(navigator.userAgent)){t._iOSEnabled=!1;var o=function(){var e=h.createBuffer(1,1,22050),n=h.createBufferSource();n.buffer=e,n.connect(h.destination),void 0===n.start?n.noteOn(0):n.start(0),setTimeout(function(){n.playbackState!==n.PLAYING_STATE&&n.playbackState!==n.FINISHED_STATE||(t._iOSEnabled=!0,t.iOSAutoEnable=!1,window.removeEventListener("touchstart",o,!1))},0)};return window.addEventListener("touchstart",o,!1),t}}};var f=new e;n.prototype={init:function(e){var n=this;return n._autoplay=e.autoplay||!1,n._ext=e.ext||null,n._html5=e.html5||!1,n._muted=e.mute||!1,n._loop=e.loop||!1,n._pool=e.pool||5,n._preload="boolean"!=typeof e.preload||e.preload,n._rate=e.rate||1,n._sprite=e.sprite||{},n._src="string"!=typeof e.src?e.src:[e.src],n._volume=void 0!==e.volume?e.volume:1,n._duration=0,n._loaded=!1,n._sounds=[],n._endTimers={},n._onend=e.onend?[{fn:e.onend}]:[],n._onfaded=e.onfaded?[{fn:e.onfaded}]:[],n._onload=e.onload?[{fn:e.onload}]:[],n._onloaderror=e.onloaderror?[{fn:e.onloaderror}]:[],n._onpause=e.onpause?[{fn:e.onpause}]:[],n._onplay=e.onplay?[{fn:e.onplay}]:[],n._webAudio=a&&!n._html5,void 0!==h&&h&&f.iOSAutoEnable&&f._enableiOSAudio(),f._howls.push(n),n._preload&&n.load(),n},load:function(){var e=this,n=null;if(!r){"string"==typeof e._src&&(e._src=[e._src]);for(var t=0;t<e._src.length;t++){var o,i=e._ext&&e._ext[t]?e._ext[t]:(o=e._src[t],(i=(i=/^data:audio\/([^;,]+);/i.exec(o))||/\.([^.]+)$/.exec(o.split("?",1)[0]))&&i[1].toLowerCase());if(f.codecs(i)){n=e._src[t];break}}return n?(e._src=n,new w(e),e._webAudio&&s(e),e):void e._emit("loaderror")}e._emit("loaderror")},play:function(n){var t=this,o=arguments,e=null;if("number"==typeof n)e=n,n=null;else if(void 0===n){n="__default";for(var i=0,r=0;r<t._sounds.length;r++)t._sounds[r]._paused&&!t._sounds[r]._ended&&(i++,e=t._sounds[r]._id);1===i?n=null:e=null}var d=e?t._soundById(e):t._inactiveSound();if(!d)return null;if(e&&!n&&(n=d._sprite||"__default"),!t._loaded&&!t._sprite[n])return t.once("load",function(){t.play(t._soundById(d._id)?d._id:void 0)}),d._id;if(e&&!d._paused)return d._id;var a=0<d._seek?d._seek:t._sprite[n][0]/1e3,u=(t._sprite[n][0]+t._sprite[n][1])/1e3-a,s=function(){var e=!(!d._loop&&!t._sprite[n][2]);t._emit("end",d._id),!t._webAudio&&e&&t.stop(d._id).play(d._id),t._webAudio&&e&&(t._emit("play",d._id),d._seek=d._start||0,d._playStart=h.currentTime,t._endTimers[d._id]=setTimeout(s,1e3*(d._stop-d._start)/Math.abs(t._rate))),t._webAudio&&!e&&(d._paused=!0,d._ended=!0,d._seek=d._start||0,t._clearTimer(d._id),d._node.bufferSource=null),t._webAudio||e||t.stop(d._id)};t._endTimers[d._id]=setTimeout(s,1e3*u/Math.abs(t._rate)),d._paused=!1,d._ended=!1,d._sprite=n,d._seek=a,d._start=t._sprite[n][0]/1e3,d._stop=(t._sprite[n][0]+t._sprite[n][1])/1e3,d._loop=!(!d._loop&&!t._sprite[n][2]);var l,_,c,w=d._node;return t._webAudio?(l=function(){t._refreshBuffer(d);var e=d._muted||t._muted?0:d._volume*f.volume();w.gain.setValueAtTime(e,h.currentTime),d._playStart=h.currentTime,void 0===w.bufferSource.start?d._loop?w.bufferSource.noteGrainOn(0,a,86400):w.bufferSource.noteGrainOn(0,a,u):d._loop?w.bufferSource.start(0,a,86400):w.bufferSource.start(0,a,u),t._endTimers[d._id]||(t._endTimers[d._id]=setTimeout(s,1e3*u/Math.abs(t._rate))),o[1]||setTimeout(function(){t._emit("play",d._id)},0)},t._loaded?l():(t.once("load",l),t._clearTimer(d._id))):(_=function(){w.currentTime=a,w.muted=d._muted||t._muted||f._muted||w.muted,w.volume=d._volume*f.volume(),w.playbackRate=t._rate,setTimeout(function(){w.play(),o[1]||t._emit("play",d._id)},0)},4===w.readyState||!w.readyState&&navigator.isCocoonJS?_():(c=function(){t._endTimers[d._id]=setTimeout(s,1e3*u/Math.abs(t._rate)),_(),w.removeEventListener("canplaythrough",c,!1)},w.addEventListener("canplaythrough",c,!1),t._clearTimer(d._id))),d._id},pause:function(e){var n=this;if(!n._loaded)return n.once("play",function(){n.pause(e)}),n;for(var t=n._getSoundIds(e),o=0;o<t.length;o++){n._clearTimer(t[o]);var i=n._soundById(t[o]);if(i&&!i._paused){if(i._seek=n.seek(t[o]),i._paused=!0,n._webAudio){if(!i._node.bufferSource)return n;void 0===i._node.bufferSource.stop?i._node.bufferSource.noteOff(0):i._node.bufferSource.stop(0),i._node.bufferSource=null}else isNaN(i._node.duration)||i._node.pause();arguments[1]||n._emit("pause",i._id)}}return n},stop:function(e){var n=this;if(!n._loaded)return void 0!==n._sounds[0]._sprite&&n.once("play",function(){n.stop(e)}),n;for(var t=n._getSoundIds(e),o=0;o<t.length;o++){n._clearTimer(t[o]);var i=n._soundById(t[o]);if(i&&!i._paused)if(i._seek=i._start||0,i._paused=!0,i._ended=!0,n._webAudio&&i._node){if(!i._node.bufferSource)return n;void 0===i._node.bufferSource.stop?i._node.bufferSource.noteOff(0):i._node.bufferSource.stop(0),i._node.bufferSource=null}else i._node&&!isNaN(i._node.duration)&&(i._node.pause(),i._node.currentTime=i._start||0)}return n},mute:function(e,n){var t=this;if(!t._loaded)return t.once("play",function(){t.mute(e,n)}),t;if(void 0===n){if("boolean"!=typeof e)return t._muted;t._muted=e}for(var o=t._getSoundIds(n),i=0;i<o.length;i++){var r=t._soundById(o[i]);r&&(r._muted=e,t._webAudio&&r._node?r._node.gain.setValueAtTime(e?0:r._volume*f.volume(),h.currentTime):r._node&&(r._node.muted=!!f._muted||e))}return t},volume:function(){var e,n,t,o=this,i=arguments;if(0===i.length)return o._volume;if(1===i.length?0<=o._getSoundIds().indexOf(i[0])?n=parseInt(i[0],10):e=parseFloat(i[0]):2===i.length&&(e=parseFloat(i[0]),n=parseInt(i[1],10)),!(void 0!==e&&0<=e&&e<=1))return(t=n?o._soundById(n):o._sounds[0])?t._volume:0;if(!o._loaded)return o.once("play",function(){o.volume.apply(o,i)}),o;void 0===n&&(o._volume=e),n=o._getSoundIds(n);for(var r=0;r<n.length;r++)(t=o._soundById(n[r]))&&(t._volume=e,o._webAudio&&t._node?t._node.gain.setValueAtTime(e*f.volume(),h.currentTime):t._node&&(t._node.volume=e*f.volume()));return o},fade:function(e,o,n,t){var i=this;if(!i._loaded)return i.once("play",function(){i.fade(e,o,n,t)}),i;i.volume(e,t);for(var r=i._getSoundIds(t),d=0;d<r.length;d++){var a,u,s,l,_=i._soundById(r[d]);_&&(i._webAudio?(a=h.currentTime,u=a+n/1e3,_._volume=e,_._node.gain.setValueAtTime(e,a),_._node.gain.linearRampToValueAtTime(o,u),setTimeout(function(e,n){setTimeout(function(){n._volume=o,i._emit("faded",e)},0<u-h.currentTime?Math.ceil(1e3*(u-h.currentTime)):0)}.bind(i,r[d],_),n)):(_=Math.abs(e-o),s=o<e?"out":"in",l=n/(_/.01),function(){var n=e,t=setInterval(function(e){n+="in"==s?.01:-.01,n=Math.max(0,n),n=Math.min(1,n),n=Math.round(100*n)/100,i.volume(n,e),n===o&&(clearInterval(t),i._emit("faded",e))}.bind(i,r[d]),l)}()))}return i},loop:function(){var e,n,t,o=this,i=arguments;if(0===i.length)return o._loop;if(1===i.length){if("boolean"!=typeof i[0])return!!(t=o._soundById(parseInt(i[0],10)))&&t._loop;e=i[0],o._loop=e}else 2===i.length&&(e=i[0],n=parseInt(i[1],10));for(var r=o._getSoundIds(n),d=0;d<r.length;d++)(t=o._soundById(r[d]))&&(t._loop=e,o._webAudio&&t._node&&(t._node.bufferSource.loop=e));return o},seek:function(){var e,n,t=this,o=arguments;if(0===o.length?n=t._sounds[0]._id:1===o.length?0<=t._getSoundIds().indexOf(o[0])?n=parseInt(o[0],10):(n=t._sounds[0]._id,e=parseFloat(o[0])):2===o.length&&(e=parseFloat(o[0]),n=parseInt(o[1],10)),void 0===n)return t;if(!t._loaded)return t.once("load",function(){t.seek.apply(t,o)}),t;var i=t._soundById(n);if(i){if(!(0<=e))return t._webAudio?i._seek+t.playing(n)?h.currentTime-i._playStart:0:i._node.currentTime;var r=t.playing(n);r&&t.pause(n,!0),i._seek=e,t._clearTimer(n),r&&t.play(n,!0)}return t},playing:function(e){e=this._soundById(e)||this._sounds[0];return!!e&&!e._paused},duration:function(){return this._duration},unload:function(){for(var e=this,n=e._sounds,t=0;t<n.length;t++){n[t]._paused||(e.stop(n[t]._id),e._emit("end",n[t]._id)),e._webAudio||(n[t]._node.src="",n[t]._node.removeEventListener("error",n[t]._errorFn,!1),n[t]._node.removeEventListener("canplaythrough",n[t]._loadFn,!1)),delete n[t]._node,e._clearTimer(n[t]._id);var o=f._howls.indexOf(e);0<=o&&f._howls.splice(o,1)}return u&&delete u[e._src],e=null},on:function(e,n,t){e=this["_on"+e];return"function"==typeof n&&e.push({id:t,fn:n}),this},off:function(e,n,t){var o=this["_on"+e];if(n){for(var i=0;i<o.length;i++)if(n===o[i].fn&&t===o[i].id){o.splice(i,1);break}}else o=[];return this},once:function(e,n,t){var o=this,i=function(){n.apply(o,arguments),o.off(e,i,t)};return o.on(e,i,t),o},_emit:function(e,n,t){for(var o=this["_on"+e],i=0;i<o.length;i++)o[i].id&&o[i].id!==n||setTimeout(function(e){e.call(this,n,t)}.bind(this,o[i].fn),0);return this},_clearTimer:function(e){return this._endTimers[e]&&(clearTimeout(this._endTimers[e]),delete this._endTimers[e]),this},_soundById:function(e){for(var n=0;n<this._sounds.length;n++)if(e===this._sounds[n]._id)return this._sounds[n];return null},_inactiveSound:function(){var e=this;e._drain();for(var n=0;n<e._sounds.length;n++)if(e._sounds[n]._ended)return e._sounds[n].reset();return new w(e)},_drain:function(){var e=this,n=e._pool,t=0,o=0;if(!(e._sounds.length<n)){for(o=0;o<e._sounds.length;o++)e._sounds[o]._ended&&t++;for(o=e._sounds.length-1;0<=o;o--){if(t<=n)return;e._sounds[o]._ended&&(e._webAudio&&e._sounds[o]._node&&e._sounds[o]._node.disconnect(0),e._sounds.splice(o,1),t--)}}},_getSoundIds:function(e){if(void 0!==e)return[e];for(var n=[],t=0;t<this._sounds.length;t++)n.push(this._sounds[t]._id);return n},_refreshBuffer:function(e){return e._node.bufferSource=h.createBufferSource(),e._node.bufferSource.buffer=u[this._src],e._node.bufferSource.connect(e._panner||e._node),e._node.bufferSource.loop=e._loop,e._loop&&(e._node.bufferSource.loopStart=e._start||0,e._node.bufferSource.loopEnd=e._stop),e._node.bufferSource.playbackRate.value=this._rate,this}};var u,s,l,_,c,w=function(e){this._parent=e,this.init()};w.prototype={init:function(){var e=this,n=e._parent;return e._muted=n._muted,e._loop=n._loop,e._volume=n._volume,e._muted=n._muted,e._seek=0,e._paused=!0,e._ended=!0,e._id=Math.round(Date.now()*Math.random()),n._sounds.push(e),e.create(),e},create:function(){var e=this,n=e._parent,t=f._muted||e._muted||e._parent._muted?0:e._volume*f.volume();return n._webAudio?(e._node=void 0===h.createGain?h.createGainNode():h.createGain(),e._node.gain.setValueAtTime(t,h.currentTime),e._node.paused=!0,e._node.connect(d)):(e._node=new Audio,e._errorFn=e._errorListener.bind(e),e._node.addEventListener("error",e._errorFn,!1),e._loadFn=e._loadListener.bind(e),e._node.addEventListener("canplaythrough",e._loadFn,!1),e._node.src=n._src,e._node.preload="auto",e._node.volume=t,e._node.load()),e},reset:function(){var e=this,n=e._parent;return e._muted=n._muted,e._loop=n._loop,e._volume=n._volume,e._muted=n._muted,e._seek=0,e._paused=!0,e._ended=!0,e._sprite=null,e._id=Math.round(Date.now()*Math.random()),e},_errorListener:function(){var e=this;e._node.error&&4===e._node.error.code&&(f.noAudio=!0),e._parent._emit("loaderror",e._id,e._node.error?e._node.error.code:0),e._node.removeEventListener("error",e._errorListener,!1)},_loadListener:function(){var e=this._parent;e._duration=Math.ceil(10*this._node.duration)/10,0===Object.keys(e._sprite).length&&(e._sprite={__default:[0,1e3*e._duration]}),e._loaded||(e._loaded=!0,e._emit("load")),e._autoplay&&e.play(),this._node.removeEventListener("canplaythrough",this._loadFn,!1)}},a&&(u={},s=function(e){var n=e._src;if(u[n])return e._duration=u[n].duration,void c(e);if(/^data:[^;]+;base64,/.test(n)){window.atob=window.atob||function(e){for(var n,t,o=String(e).replace(/=+$/,""),i=0,r=0,d="";t=o.charAt(r++);~t&&(n=i%4?64*n+t:t,i++%4)&&(d+=String.fromCharCode(255&n>>(-2*i&6))))t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(t);return d};for(var t=atob(n.split(",")[1]),o=new Uint8Array(t.length),i=0;i<t.length;++i)o[i]=t.charCodeAt(i);_(o.buffer,e)}else{var r=new XMLHttpRequest;r.open("GET",n,!0),r.responseType="arraybuffer",r.onload=function(){_(r.response,e)},r.onerror=function(){e._webAudio&&(e._html5=!0,e._webAudio=!1,e._sounds=[],delete u[n],e.load())},l(r)}},l=function(n){try{n.send()}catch(e){n.onerror()}},_=function(e,n){h.decodeAudioData(e,function(e){e&&(u[n._src]=e,c(n,e))},function(){n._emit("loaderror")})},c=function(e,n){n&&!e._duration&&(e._duration=n.duration),0===Object.keys(e._sprite).length&&(e._sprite={__default:[0,1e3*e._duration]}),e._loaded||(e._loaded=!0,e._emit("load")),e._autoplay&&e.play()}),"function"==typeof define&&define.amd&&define("howler",function(){return{Howler:f,Howl:n}}),"undefined"!=typeof exports&&(exports.Howler=f,exports.Howl=n),"undefined"!=typeof window&&(window.HowlerGlobal=e,window.Howler=f,window.Howl=n,window.Sound=w)}();