UNPKG

pixi-game-camera

Version:

A flexible and non-opinionated way to add camera effects to your PIXI application via containers.

17 lines (16 loc) 17.3 kB
function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function i(t,i,n){return i&&e(t.prototype,i),n&&e(t,n),t}function n(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&o(t,e)}function s(t){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function o(t,e){return(o=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function a(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?a(t):e}function u(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var i,n=s(t);if(e){var r=s(this).constructor;i=Reflect.construct(n,arguments,r)}else i=n.apply(this,arguments);return h(this,i)}}var c=/iPhone/i,d=/iPod/i,l=/iPad/i,f=/\biOS-universal(?:.+)Mac\b/i,p=/\bAndroid(?:.+)Mobile\b/i,_=/Android/i,v=/(?:SD4930UR|\bSilk(?:.+)Mobile\b)/i,y=/Silk/i,m=/Windows Phone/i,x=/\bWindows(?:.+)ARM\b/i,b=/BlackBerry/i,g=/BB10/i,M=/Opera Mini/i,A=/\b(CriOS|Chrome)(?:.+)Mobile/i,S=/Mobile(?:.+)Firefox\b/i,k=function(t){return void 0!==t&&"MacIntel"===t.platform&&"number"==typeof t.maxTouchPoints&&t.maxTouchPoints>1&&"undefined"==typeof MSStream}; /*! * @pixi/settings - v5.3.7 * Compiled Tue, 29 Dec 2020 19:30:11 UTC * * @pixi/settings is licensed under the MIT License. * http://www.opensource.org/licenses/mit-license */ var I=function(t){var e={userAgent:"",platform:"",maxTouchPoints:0};t||"undefined"==typeof navigator?"string"==typeof t?e.userAgent=t:t&&t.userAgent&&(e={userAgent:t.userAgent,platform:t.platform,maxTouchPoints:t.maxTouchPoints||0}):e={userAgent:navigator.userAgent,platform:navigator.platform,maxTouchPoints:navigator.maxTouchPoints||0};var i=e.userAgent,n=i.split("[FBAN");void 0!==n[1]&&(i=n[0]),void 0!==(n=i.split("Twitter"))[1]&&(i=n[0]);var r=function(t){return function(e){return e.test(t)}}(i),s={apple:{phone:r(c)&&!r(m),ipod:r(d),tablet:!r(c)&&(r(l)||k(e))&&!r(m),universal:r(f),device:(r(c)||r(d)||r(l)||r(f)||k(e))&&!r(m)},amazon:{phone:r(v),tablet:!r(v)&&r(y),device:r(v)||r(y)},android:{phone:!r(m)&&r(v)||!r(m)&&r(p),tablet:!r(m)&&!r(v)&&!r(p)&&(r(y)||r(_)),device:!r(m)&&(r(v)||r(y)||r(p)||r(_))||r(/\bokhttp\b/i)},windows:{phone:r(m),tablet:r(x),device:r(m)||r(x)},other:{blackberry:r(b),blackberry10:r(g),opera:r(M),firefox:r(S),chrome:r(A),device:r(b)||r(g)||r(M)||r(S)||r(A)},any:!1,phone:!1,tablet:!1};return s.any=s.apple.device||s.android.device||s.windows.device||s.other.device,s.phone=s.apple.phone||s.android.phone||s.windows.phone,s.tablet=s.apple.tablet||s.android.tablet||s.windows.tablet,s}(window.navigator);var T,O={MIPMAP_TEXTURES:1,ANISOTROPIC_LEVEL:0,RESOLUTION:1,FILTER_RESOLUTION:1,SPRITE_MAX_TEXTURES:function(t){var e=!0;if(I.tablet||I.phone){var i;if(I.apple.device)if(i=navigator.userAgent.match(/OS (\d+)_(\d+)?/))parseInt(i[1],10)<11&&(e=!1);if(I.android.device)if(i=navigator.userAgent.match(/Android\s([0-9.]*)/))parseInt(i[1],10)<7&&(e=!1)}return e?t:4}(32),SPRITE_BATCH_SIZE:4096,RENDER_OPTIONS:{view:null,antialias:!1,autoDensity:!1,transparent:!1,backgroundColor:0,clearBeforeRender:!0,preserveDrawingBuffer:!1,width:800,height:600,legacy:!1},GC_MODE:0,GC_MAX_IDLE:3600,GC_MAX_CHECK_COUNT:600,WRAP_MODE:33071,SCALE_MODE:1,PRECISION_VERTEX:"highp",PRECISION_FRAGMENT:I.apple.device?"highp":"mediump",CAN_UPLOAD_SAME_BUFFER:!I.apple.device,CREATE_IMAGE_BITMAP:!1,ROUND_PIXELS:!1}; /*! * @pixi/ticker - v5.3.7 * Compiled Tue, 29 Dec 2020 19:30:11 UTC * * @pixi/ticker is licensed under the MIT License. * http://www.opensource.org/licenses/mit-license */O.TARGET_FPMS=.06,function(t){t[t.INTERACTION=50]="INTERACTION",t[t.HIGH=25]="HIGH",t[t.NORMAL=0]="NORMAL",t[t.LOW=-25]="LOW",t[t.UTILITY=-50]="UTILITY"}(T||(T={}));var P=function(){function t(t,e,i,n){void 0===e&&(e=null),void 0===i&&(i=0),void 0===n&&(n=!1),this.fn=t,this.context=e,this.priority=i,this.once=n,this.next=null,this.previous=null,this._destroyed=!1}return t.prototype.match=function(t,e){return void 0===e&&(e=null),this.fn===t&&this.context===e},t.prototype.emit=function(t){this.fn&&(this.context?this.fn.call(this.context,t):this.fn(t));var e=this.next;return this.once&&this.destroy(!0),this._destroyed&&(this.next=null),e},t.prototype.connect=function(t){this.previous=t,t.next&&(t.next.previous=this),this.next=t.next,t.next=this},t.prototype.destroy=function(t){void 0===t&&(t=!1),this._destroyed=!0,this.fn=null,this.context=null,this.previous&&(this.previous.next=this.next),this.next&&(this.next.previous=this.previous);var e=this.next;return this.next=t?null:e,this.previous=null,e},t}();!function(){function t(){var t=this;this._head=new P(null,null,1/0),this._requestId=null,this._maxElapsedMS=100,this._minElapsedMS=0,this.autoStart=!1,this.deltaTime=1,this.deltaMS=1/O.TARGET_FPMS,this.elapsedMS=1/O.TARGET_FPMS,this.lastTime=-1,this.speed=1,this.started=!1,this._protected=!1,this._lastFrame=-1,this._tick=function(e){t._requestId=null,t.started&&(t.update(e),t.started&&null===t._requestId&&t._head.next&&(t._requestId=requestAnimationFrame(t._tick)))}}t.prototype._requestIfNeeded=function(){null===this._requestId&&this._head.next&&(this.lastTime=performance.now(),this._lastFrame=this.lastTime,this._requestId=requestAnimationFrame(this._tick))},t.prototype._cancelIfNeeded=function(){null!==this._requestId&&(cancelAnimationFrame(this._requestId),this._requestId=null)},t.prototype._startIfPossible=function(){this.started?this._requestIfNeeded():this.autoStart&&this.start()},t.prototype.add=function(t,e,i){return void 0===i&&(i=T.NORMAL),this._addListener(new P(t,e,i))},t.prototype.addOnce=function(t,e,i){return void 0===i&&(i=T.NORMAL),this._addListener(new P(t,e,i,!0))},t.prototype._addListener=function(t){var e=this._head.next,i=this._head;if(e){for(;e;){if(t.priority>e.priority){t.connect(i);break}i=e,e=e.next}t.previous||t.connect(i)}else t.connect(i);return this._startIfPossible(),this},t.prototype.remove=function(t,e){for(var i=this._head.next;i;)i=i.match(t,e)?i.destroy():i.next;return this._head.next||this._cancelIfNeeded(),this},Object.defineProperty(t.prototype,"count",{get:function(){if(!this._head)return 0;for(var t=0,e=this._head;e=e.next;)t++;return t},enumerable:!1,configurable:!0}),t.prototype.start=function(){this.started||(this.started=!0,this._requestIfNeeded())},t.prototype.stop=function(){this.started&&(this.started=!1,this._cancelIfNeeded())},t.prototype.destroy=function(){if(!this._protected){this.stop();for(var t=this._head.next;t;)t=t.destroy(!0);this._head.destroy(),this._head=null}},t.prototype.update=function(t){var e;if(void 0===t&&(t=performance.now()),t>this.lastTime){if((e=this.elapsedMS=t-this.lastTime)>this._maxElapsedMS&&(e=this._maxElapsedMS),e*=this.speed,this._minElapsedMS){var i=t-this._lastFrame|0;if(i<this._minElapsedMS)return;this._lastFrame=t-i%this._minElapsedMS}this.deltaMS=e,this.deltaTime=this.deltaMS*O.TARGET_FPMS;for(var n=this._head,r=n.next;r;)r=r.emit(this.deltaTime);n.next||this._cancelIfNeeded()}else this.deltaTime=this.deltaMS=this.elapsedMS=0;this.lastTime=t},Object.defineProperty(t.prototype,"FPS",{get:function(){return 1e3/this.elapsedMS},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"minFPS",{get:function(){return 1e3/this._maxElapsedMS},set:function(t){var e=Math.min(this.maxFPS,t),i=Math.min(Math.max(0,e)/1e3,O.TARGET_FPMS);this._maxElapsedMS=1/i},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"maxFPS",{get:function(){return this._minElapsedMS?Math.round(1e3/this._minElapsedMS):0},set:function(t){if(0===t)this._minElapsedMS=0;else{var e=Math.max(this.minFPS,t);this._minElapsedMS=1/(e/1e3)}},enumerable:!1,configurable:!0}),Object.defineProperty(t,"shared",{get:function(){if(!t._shared){var e=t._shared=new t;e.autoStart=!0,e._protected=!0}return t._shared},enumerable:!1,configurable:!0}),Object.defineProperty(t,"system",{get:function(){if(!t._system){var e=t._system=new t;e.autoStart=!0,e._protected=!0}return t._system},enumerable:!1,configurable:!0})}();var E=function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),n(this,"ticker",void 0),Object.assign(this,i)},w=function(){function e(i){t(this,e),n(this,"_options",void 0),n(this,"_ticker",void 0),this._options=new E(i),this._options.ticker&&(this._ticker=this._options.ticker)}return i(e,[{key:"effect",value:function(t){this._addToTicker(t)}},{key:"_addToTicker",value:function(t){var e,i=this,n=t.update.bind(t);this._ticker?(t.finished.add((function(){var t;return null===(t=i._ticker)||void 0===t?void 0:t.remove(n,i)})),null===(e=this._ticker)||void 0===e||e.add(n,this,T.NORMAL)):t.start()}}]),e}();var R=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")};function L(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var F=function(t,e,i){return e&&L(t.prototype,e),i&&L(t,i),t};var N=function(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t},C=function(){function t(e,i){R(this,t),N(this,"fn",void 0),N(this,"_once",void 0),N(this,"_delete",!1),N(this,"_timesCalled",0),N(this,"paused",!1),this.fn=e,this._once=i}return F(t,[{key:"run",value:function(){this.paused||(this.fn.apply(this,arguments),this._timesCalled++,this._once&&(this._delete=!0))}},{key:"once",get:function(){return this._once}},{key:"delete",get:function(){return this._delete}},{key:"timesCalled",get:function(){return this._timesCalled}}]),t}(),G=function(){function t(){R(this,t),N(this,"_tasks",new Array)}return F(t,[{key:"add",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];return this._tasks.push(new C(t,e)),this}},{key:"dispatch",value:function(){for(var t=0;t<this.tasks.length;++t){var e=this.tasks[t];e.run.apply(e,arguments),e.delete&&this.tasks.splice(t,1)}}},{key:"remove",value:function(t){return this._tasks=this.tasks.filter((function(e){return e.fn.toString()!=t.toString()})),this}},{key:"removeAll",value:function(){return this._tasks=[],this}},{key:"pause",value:function(t){var e=this.tasks.find((function(e){return!e.paused&&t.toString()===e.fn.toString()}));return e&&(e.paused=!0),this}},{key:"resume",value:function(t){var e=this.tasks.find((function(e){return e.paused&&t.toString()===e.fn.toString()}));return e&&(e.paused=!1),this}},{key:"noop",value:function(t){var e=this.tasks.find((function(e){return t.toString()===e.fn.toString()}));return e&&(e.fn=function(){}),this}},{key:"tasks",get:function(){return this._tasks}},{key:"numTasks",get:function(){return this._tasks.length}}]),t}(),q=function(){function e(i){t(this,e),n(this,"container",void 0),n(this,"duration",0),n(this,"started",0),n(this,"current",0),n(this,"finished",new G),n(this,"useRAF",!1),n(this,"id",void 0),this.container=i,this.started=performance.now()}return i(e,[{key:"start",value:function(){var t=this;this.useRAF=!0,this.finished.add((function(){return cancelAnimationFrame(t.id)})),this.update()}},{key:"easeLinear",value:function(t){return+t}}]),e}(),Z=function(e){r(o,q);var s=u(o);function o(e,i,r,h,u,c){var d;return t(this,o),n(a(d=s.call(this,e)),"_filter",void 0),n(a(d),"_color",void 0),n(a(d),"_opacity",void 0),n(a(d),"_easing",void 0),n(a(d),"_fadeOut",!0),n(a(d),"_initialOpacity",void 0),d._color=r,d._opacity=h,d.duration=u,d._easing=c||d.easeLinear,d._filter=i,d._filter.width=d.container.width,d._filter.height=d.container.height,d._filter.alpha=0,d.container.addChild(d._filter),d._filter.tint=d._color,d._initialOpacity=d._filter.alpha,d._filter.alpha>d._opacity&&(d._fadeOut=!1),d}return i(o,[{key:"update",value:function(){var t=this;if(this.criteriaMet())return this._filter.alpha=this._opacity,void this.finished.dispatch();this.current=performance.now();var e=(this.current-this.started)/this.duration,i=1*this._easing(e);this._filter.alpha=this._fadeOut?i:this._initialOpacity-i,this.useRAF&&(this.id=requestAnimationFrame((function(){return t.update()})))}},{key:"criteriaMet",value:function(){return!!(this._fadeOut&&this._filter.alpha>=this._opacity-.01||!this._fadeOut&&this._filter.alpha<=this._opacity+.01)}}]),o}(),j=function(e){r(o,q);var s=u(o);function o(e,i,r){var h;return t(this,o),n(a(h=s.call(this,e)),"_intensity",5),n(a(h),"_initialPivot",void 0),h._intensity=i,h.duration=r,h._initialPivot={x:h.container.pivot.x,y:h.container.pivot.y},h.started=performance.now(),h}return i(o,[{key:"update",value:function(){var t=this;if(this.criteriaMet())return this.container.pivot.x=this._initialPivot.x,this.container.pivot.y=this._initialPivot.y,void this.finished.dispatch();this.current=performance.now();var e=Math.random()*this._intensity,i=Math.random()*this._intensity;this.container.pivot.x=e,this.container.pivot.y=i,this.useRAF&&(this.id=requestAnimationFrame((function(){return t.update()})))}},{key:"criteriaMet",value:function(){return this.current-this.started>=this.duration}}]),o}(),D=function(e){r(o,q);var s=u(o);function o(e,i,r,h){var u;return t(this,o),n(a(u=s.call(this,e)),"_coordinates",void 0),n(a(u),"_difference",void 0),n(a(u),"_xIsGreater",!1),n(a(u),"_yIsGreater",!1),u._coordinates={x:i,y:r},u.duration=h,u._coordinates.x>u.container.pivot.x&&(u._xIsGreater=!0),u._coordinates.y>u.container.pivot.y&&(u._yIsGreater=!0),u._difference={x:Math.abs(u._coordinates.x-u.container.pivot.x),y:Math.abs(u._coordinates.y-u.container.pivot.y)},u}return i(o,[{key:"update",value:function(){var t=this;if(this.criteriaMet())this.finished.dispatch();else{this.current=performance.now();var e=(this.current-this.started)/this.duration,i=(this.duration-this.current)/this.duration,n=this._xIsGreater?this._difference.x*e:this._difference.x*i,r=this._yIsGreater?this._difference.y*e:this._difference.y*i;this.container.pivot.x=n,this.container.pivot.y=r,this.useRAF&&(this.id=requestAnimationFrame((function(){return t.update()})))}}},{key:"criteriaMet",value:function(){return this.container.pivot.x>this._coordinates.x-5&&this.container.pivot.x<this._coordinates.x+5&&this.container.pivot.y>this._coordinates.y-5&&this.container.pivot.y<this._coordinates.y+5}}]),o}(),U=function(e){r(o,q);var s=u(o);function o(e,i,r,h){var u;return t(this,o),n(a(u=s.call(this,e)),"_initialAngle",void 0),n(a(u),"_desiredAngle",void 0),n(a(u),"_initialPivot",void 0),n(a(u),"_easing",void 0),u._initialAngle=e.angle,u._desiredAngle=i,u.duration=r,u._easing=h||u.easeLinear,u._initialPivot={x:u.container.pivot.x,y:u.container.pivot.y},0==u._initialPivot.x&&(u.container.pivot.x=u.container.width/2),0==u._initialPivot.y&&(u.container.pivot.y=u.container.height/2),u}return i(o,[{key:"update",value:function(){var t=this;if(this.criteriaMet())this.finished.dispatch();else{this.current=performance.now();var e=(this.current-this.started)/this.duration,i=this._easing(e),n=this._desiredAngle*i;this.container.angle=this._initialAngle+n,this.useRAF&&(this.id=requestAnimationFrame((function(){return t.update()})))}}},{key:"criteriaMet",value:function(){return this.container.angle>this._desiredAngle}}]),o}(),B=function(e){r(o,q);var s=u(o);function o(e,i,r,h,u){var c;return t(this,o),n(a(c=s.call(this,e)),"_desiredZoomLevel",void 0),n(a(c),"_easing",void 0),n(a(c),"_initialZoomLevel",void 0),n(a(c),"_xIsGreater",!1),n(a(c),"_yIsGreater",!1),c.duration=h,c._desiredZoomLevel={x:i,y:r},c._easing=u||c.easeLinear,c._initialZoomLevel={x:c.container.scale.x,y:c.container.scale.y},c._desiredZoomLevel.x>c._initialZoomLevel.x&&(c._xIsGreater=!0),c._desiredZoomLevel.y>c._initialZoomLevel.y&&(c._yIsGreater=!0),c}return i(o,[{key:"update",value:function(){var t=this;if(this.criteriaMet()||this.current>this.duration)this.finished.dispatch();else{this.current=performance.now();var e=(this.current-this.started)/this.duration,i=this._easing(e),n=this._desiredZoomLevel.x*i,r=this._desiredZoomLevel.y*i;this.container.scale.x=this._xIsGreater?this._initialZoomLevel.x+n/2:this._initialZoomLevel.x-n,this.container.scale.y=this._yIsGreater?this._initialZoomLevel.y+r/2:this._initialZoomLevel.y-r,this.useRAF&&(this.id=requestAnimationFrame((function(){return t.update()})))}}},{key:"criteriaMet",value:function(){return this.container.scale.x>this._desiredZoomLevel.x-.01&&this.container.scale.x<this._desiredZoomLevel.x+.01&&this.container.scale.y>this._desiredZoomLevel.y-.01&&this.container.scale.y<this._desiredZoomLevel.y+.01}}]),o}();export{w as Camera,q as Effect,Z as Fade,D as PanTo,U as Rotate,j as Shake,B as ZoomTo};