UNPKG

phaser3-rex-plugins

Version:
2 lines (1 loc) 10.1 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexcanvasplugin=e();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var i=0;i<e.length;i++){var r=e[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(n=r.key,a=void 0,"symbol"==typeof(a=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var r=i.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(n,"string"))?a:String(a)),r);}var n,a;}function r(t,e,r){return e&&i(t.prototype,e),r&&i(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function n(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}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&o(t,e);}function a(t){return a=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},a(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function s(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(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 Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var i,r=a(t);if(e){var n=a(this).constructor;i=Reflect.construct(r,arguments,n);}else i=r.apply(this,arguments);return function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return s(t)}(this,i)}}var u,c=Phaser.Renderer.WebGL.Utils,l={renderWebGL:function(t,e,i,r){if(e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height){i.addToRenderList(e);var n=e.frame,a=n.width,o=n.height,s=c.getTintAppendFloatAlpha,h=t.pipelines.set(e.pipeline,e),u=h.setTexture2D(n.glTexture,e);t.pipelines.preBatch(e),h.batchTexture(e,n.glTexture,a,o,e.x,e.y,a/e.resolution,o/e.resolution,e.scaleX,e.scaleY,e.rotation,e.flipX,e.flipY,e.scrollFactorX,e.scrollFactorY,e.displayOriginX,e.displayOriginY,0,0,a,o,s(e.tintTopLeft,i.alpha*e._alphaTL),s(e.tintTopRight,i.alpha*e._alphaTR),s(e.tintBottomLeft,i.alpha*e._alphaBL),s(e.tintBottomRight,i.alpha*e._alphaBR),e.tintFill,0,0,i,r,!1,u),t.pipelines.postBatch(e);}},renderCanvas:function(t,e,i,r){e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height&&(i.addToRenderList(e),t.batchSprite(e,e.frame,i,r));}},d=Phaser.Display.Color,f={clear:function(){return this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},fill:function(t){return this.context.fillStyle=t,this.context.fillRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},drawFrame:function(t,e,i,r,n,a,o,s,h,u){var c=this.scene.sys.textures.getFrame(t,e);if(!c)return this;var l=c.cutWidth,d=c.cutHeight;void 0===i&&(i=0),void 0===r&&(r=0),void 0===n&&(n=l),void 0===a&&(a=d),void 0===o&&(o=0),void 0===s&&(s=0),void 0===h&&(h=l),void 0===u&&(u=d);var f=c.cutX+o,v=c.cutY+s;return this.context.drawImage(c.source.image,f,v,h,u,i,r,n,a),this.dirty=!0,this},getDataURL:function(t,e){return this.canvas.toDataURL(t,e)},getPixel:function(t,e,i){void 0===i&&(i=new d);var r=this.context.getImageData(t,e,1,1);return i.setTo(r.data[0],r.data[1],r.data[2],r.data[3]),i},setPixel:function(t,e,i,r,n,a){if("number"!=typeof i){var o=i;i=o.red,r=o.green,n=o.blue,a=o.alpha;}void 0===a&&(a=0!==i||0!==r||0!==n?255:0);var s=this.context.createImageData(1,1);return s.data[0]=i,s.data[1]=r,s.data[2]=n,s.data[3]=a,this.context.putImageData(s,t,e),this.dirty=!0,this}},v={updateTexture:function(t,e){t&&(e?t.call(e,this.canvas,this.context):t(this.canvas,this.context)),this.canvas.width===this.frame.width&&this.canvas.height===this.frame.height||this.frame.setSize(this.canvas.width,this.canvas.height),this.renderer&&this.renderer.gl&&(this.frame.source.glTexture=this.renderer.canvasToTexture(this.canvas,this.frame.source.glTexture,!0),this.frame.glTexture=this.frame.source.glTexture),this.dirty=!1;var i=this.input;return i&&!i.customHitArea&&(i.hitArea.width=this.width,i.hitArea.height=this.height),this},generateTexture:function(t,e,i,r,n){var a=this.canvas;return void 0===r?r=a.width:r*=this.resolution,void 0===n?n=a.height:n*=this.resolution,function(t,e,i,r,n,a,o){var s,h=t.sys.textures,u=t.renderer;void 0===r&&(r=0),void 0===n&&(n=0),void 0===a&&(a=e.width),void 0===o&&(o=e.height);var c=(s=h.exists(i)?h.get(i):h.createCanvas(i,a,o)).getSourceImage();c.width!==a&&(c.width=a),c.height!==o&&(c.height=o);var l=c.getContext("2d",{willReadFrequently:!0});l.clearRect(0,0,a,o),l.drawImage(e,r,n,a,o),u.gl&&s&&u.canvasToTexture(c,s.source[0].glTexture,!0,0);}(this.scene,a,t,e,i,r,n),this},loadTexture:function(t,e){var i=this.scene.sys.textures.getFrame(t,e);return i?(this.width!==i.cutWidth||this.height!==i.cutHeight?this.setSize(i.cutWidth,i.cutHeight):this.clear(),this.drawFrame(t,e),this.dirty=!0,this):this}},p=!1;p||(void 0===u&&(u=60),parseInt(Phaser.VERSION.match(/\.(\d+)\./)[1])<u&&console.error("Minimum supported version : 3.".concat(u)),p=!0);var y=Phaser.Display.Canvas.CanvasPool,g=function(t){n(a,t);var i=h(a);function a(t,r,n,o,h){var u;return e(this,a),void 0===r&&(r=0),void 0===n&&(n=0),void 0===o&&(o=1),void 0===h&&(h=1),(u=i.call(this,t,"rexCanvas")).renderer=t.sys.game.renderer,u.resolution=1,u._width=o,u._height=h,o=Math.max(Math.ceil(o*u.resolution),1),h=Math.max(Math.ceil(h*u.resolution),1),u.canvas=y.create(s(u),o,h),u.context=u.canvas.getContext("2d",{willReadFrequently:!0}),u.dirty=!1,u.setPosition(r,n),u.setOrigin(.5,.5),u.initPipeline(),u.initPostPipeline(!0),u._crop=u.resetCropObject(),u.texture=t.sys.textures.addCanvas(null,u.canvas,!0),u.frame=u.texture.get(),u.frame.source.resolution=u.resolution,u.renderer&&u.renderer.gl&&(u.renderer.deleteTexture(u.frame.source.glTexture),u.frame.source.glTexture=null),u.dirty=!0,u}return r(a,[{key:"preDestroy",value:function(){y.remove(this.canvas),this.texture.destroy(),this.canvas=null,this.context=null;}},{key:"width",get:function(){return this._width},set:function(t){this.setSize(t,this._height);}},{key:"height",get:function(){return this._height},set:function(t){this.setSize(this._width,t);}},{key:"setCanvasSize",value:function(t,e){return this._width===t&&this._height===e||(this._width=t,this._height=e,this.updateDisplayOrigin(),t=Math.max(Math.ceil(t*this.resolution),1),e=Math.max(Math.ceil(e*this.resolution),1),this.canvas.width=t,this.canvas.height=e,this.frame.setSize(t,e),this.dirty=!0),this}},{key:"setSize",value:function(t,e){return this.setCanvasSize(t,e),this}},{key:"displayWidth",get:function(){return this.scaleX*this._width},set:function(t){this.scaleX=t/this._width;}},{key:"displayHeight",get:function(){return this.scaleY*this._height},set:function(t){this.scaleY=t/this._height;}},{key:"setDisplaySize",value:function(t,e){return this.displayWidth=t,this.displayHeight=e,this}},{key:"getCanvas",value:function(t){return t||(this.dirty=!0),this.canvas}},{key:"getContext",value:function(t){return t||(this.dirty=!0),this.context}},{key:"needRedraw",value:function(){return this.dirty=!0,this}},{key:"resize",value:function(t,e){return this.setSize(t,e),this}}]),a}(Phaser.GameObjects.GameObject),m=Phaser.GameObjects.Components;Phaser.Class.mixin(g,[m.Alpha,m.BlendMode,m.Crop,m.Depth,m.Flip,m.GetBounds,m.Mask,m.Origin,m.Pipeline,m.PostPipeline,m.ScrollFactor,m.Tint,m.Transform,m.Visible,l,f,v]);var b={loadFromURL:function(t,e){var i=this,r=new Image;return r.onload=function(){i.width!==r.width||i.height!==r.height?i.resize(r.width,r.height):i.clear(),i.context.drawImage(r,0,0),i.updateTexture(),e&&e(),r.onload=null,r.src="",r.remove();},r.src=t,this},loadFromURLPromise:function(t){var e=this;return new Promise((function(i,r){e.loadFromURL(t,i);}))},loadFromFile:function(t,e){var i=URL.createObjectURL(t);return this.loadFromURL(i,(function(){URL.revokeObjectURL(i),e&&e();})),this},loadFromFilePromise:function(t){var e=this;return new Promise((function(i,r){e.loadFromFile(t,i);}))}},x=function(t){n(a,t);var i=h(a);function a(){return e(this,a),i.apply(this,arguments)}return r(a)}(g);function w(t,e,i,r){var n=new x(this.scene,t,e,i,r);return this.scene.add.existing(n),n}Object.assign(x.prototype,b);var P=Phaser.Utils.Objects.GetAdvancedValue,T=Phaser.GameObjects.BuildGameObject;function O(t,e){void 0===t&&(t={}),void 0!==e&&(t.add=e);var i=P(t,"width",256),r=P(t,"height",i),n=new x(this.scene,0,0,i,r);T(this.scene,n,t);var a=P(t,"fill",null);return n.fill(a),n}var R=function(t){return null==t||""===t||0===t.length},j=function(t){n(a,Phaser.Plugins.BasePlugin);var i=h(a);function a(t){var r;return e(this,a),r=i.call(this,t),t.registerGameObject("rexCanvas",w,O),r}return r(a,[{key:"start",value:function(){this.game.events.on("destroy",this.destroy,this);}}]),a}();return function(e,i,r,n){if(void 0===n&&(n="."),"object"===t(e)){if(R(i)){if(null==r)return;"object"===t(r)&&(e=r);}else {"string"==typeof i&&(i=i.split(n));var a=i.pop(),o=function(e,i,r){var n=e;if(R(i));else {var a;"string"==typeof i&&(i=i.split("."));for(var o=0,s=i.length;o<s;o++){var h;null!=n[a=i[o]]&&"object"===t(n[a])||(h=o===s-1?void 0===r?{}:r:{},n[a]=h),n=n[a];}}return n}(e,i);o[a]=r;}}}(window,"RexPlugins.GameObjects.Canvas",x),j}));