@petkoneo/phaser3-rex-plugins
Version:
2 lines (1 loc) • 10.5 kB
JavaScript
var t,e;t=void 0,e=function(){const t=Phaser.Renderer.WebGL.Utils;var e={renderWebGL:function(e,i,s,r){if(i.dirty&&(i.updateTexture(),i.dirty=!1),0!==i.width&&0!==i.height){s.addToRenderList(i);var h=i.frame,a=h.width,n=h.height,o=t.getTintAppendFloatAlpha,d=e.pipelines.set(i.pipeline,i),l=d.setTexture2D(h.glTexture,i);e.pipelines.preBatch(i),d.batchTexture(i,h.glTexture,a,n,i.x,i.y,a/i.resolution,n/i.resolution,i.scaleX,i.scaleY,i.rotation,i.flipX,i.flipY,i.scrollFactorX,i.scrollFactorY,i.displayOriginX,i.displayOriginY,0,0,a,n,o(i.tintTopLeft,s.alpha*i._alphaTL),o(i.tintTopRight,s.alpha*i._alphaTR),o(i.tintBottomLeft,s.alpha*i._alphaBL),o(i.tintBottomRight,s.alpha*i._alphaBR),i.tintFill,0,0,s,r,!1,l),e.pipelines.postBatch(i)}},renderCanvas:function(t,e,i,s){e.dirty&&(e.updateTexture(),e.dirty=!1),0!==e.width&&0!==e.height&&(i.addToRenderList(e),t.batchSprite(e,e.frame,i,s))}};const i=Phaser.Display.Color;var s={clear(){return this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},fill(t){return this.context.fillStyle=t,this.context.fillRect(0,0,this.canvas.width,this.canvas.height),this.dirty=!0,this},drawFrame(t,e,i,s,r,h,a,n,o,d){var l=this.scene.sys.textures.getFrame(t,e);if(!l)return this;var u=l.cutWidth,c=l.cutHeight;void 0===i&&(i=0),void 0===s&&(s=0),void 0===r&&(r=u),void 0===h&&(h=c),void 0===a&&(a=0),void 0===n&&(n=0),void 0===o&&(o=u),void 0===d&&(d=c);var v=l.cutX+a,g=l.cutY+n;return this.context.drawImage(l.source.image,v,g,o,d,i,s,r,h),this.dirty=!0,this},getDataURL(t,e){return this.canvas.toDataURL(t,e)},getPixel(t,e,s){void 0===s&&(s=new i);var r=this.context.getImageData(t,e,1,1);return s.setTo(r.data[0],r.data[1],r.data[2],r.data[3]),s},setPixel(t,e,i,s,r,h){if("number"!=typeof i){var a=i;i=a.red,s=a.green,r=a.blue,h=a.alpha}void 0===h&&(h=0!==i||0!==s||0!==r?255:0);var n=this.context.createImageData(1,1);return n.data[0]=i,n.data[1]=s,n.data[2]=r,n.data[3]=h,this.context.putImageData(n,t,e),this.dirty=!0,this}},r={updateTexture(t,e){if(t){var i=this.resolution;1!==i&&(this.context.save(),this.context.scale(i,i)),e?t.call(e,this.canvas,this.context):t(this.canvas,this.context),1!==i&&this.context.restore()}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.spectorMetadata={textureKey:"Canvas Game Object"}),this.dirty=!1;var s=this.input;return s&&!s.customHitArea&&(s.hitArea.width=this.width,s.hitArea.height=this.height),this},generateTexture(t,e,i,s,r){var h=this.canvas;return void 0===s?s=h.width:s*=this.resolution,void 0===r?r=h.height:r*=this.resolution,function(t,e,i,s,r,h,a){var n,o=t.sys.textures,d=t.renderer;void 0===s&&(s=0),void 0===r&&(r=0),void 0===h&&(h=e.width),void 0===a&&(a=e.height);var l=(n=o.exists(i)?o.get(i):o.createCanvas(i,h,a)).getSourceImage();l.width!==h&&(l.width=h),l.height!==a&&(l.height=a);var u=l.getContext("2d",{willReadFrequently:!0});u.clearRect(0,0,h,a),u.drawImage(e,s,r,h,a),d.gl&&n&&d.canvasToTexture(l,n.source[0].glTexture,!0,0)}(this.scene,h,t,e,i,s,r),this},loadTexture(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}},h=!1;!function(t){if(!h){void 0===t&&(t=60);var e=Phaser.VERSION.split("."),i=parseInt(e[0]);if(3===i){var s=parseInt(e[1]);s<t&&console.error(`Minimum supported version : ${i}.${s}`)}else console.error(`Can't supported version : ${i}`);h=!0}}();const a=Phaser.Display.Canvas.CanvasPool,n=Phaser.GameObjects.GameObject,o=Phaser.Utils.String.UUID;class d extends n{constructor(t,e,i,s,r,h){void 0===e&&(e=0),void 0===i&&(i=0),void 0===s&&(s=1),void 0===r&&(r=1),void 0===h&&(h=1),super(t,"rexCanvas"),this.renderer=t.sys.game.renderer,this._width=s,this._height=r,this.resolution=h,s=Math.max(Math.ceil(s*this.resolution),1),r=Math.max(Math.ceil(r*this.resolution),1),this.canvas=a.create(this,s,r),this.context=this.canvas.getContext("2d",{willReadFrequently:!0}),this.dirty=!1,this.setPosition(e,i),this.setOrigin(.5,.5),this.initPipeline(),this.initPostPipeline(!0),this._crop=this.resetCropObject(),this._textureKey=o(),this.texture=t.sys.textures.addCanvas(this._textureKey,this.canvas),this.frame=this.texture.get(),this.frame.source.resolution=this.resolution,this.renderer&&this.renderer.gl&&(this.renderer.deleteTexture(this.frame.source.glTexture),this.frame.source.glTexture=null),this.dirty=!0}preDestroy(){a.remove(this.canvas),this.canvas=null,this.context=null;var t=this.texture;t&&t.destroy()}setResolution(t){if(this.resolution===t)return this;this.resolution=t;var e=Math.max(Math.ceil(this.width*t),1),i=Math.max(Math.ceil(this.height*t),1);return this.canvas.width=e,this.canvas.height=i,this.frame.source.resolution=t,this.dirty=!0,this}get width(){return this._width}set width(t){this.setSize(t,this._height)}get height(){return this._height}set height(t){this.setSize(this._width,t)}setCanvasSize(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}setSize(t,e){return this.setCanvasSize(t,e),this}get displayWidth(){return this.scaleX*this._width}set displayWidth(t){this.scaleX=t/this._width}get displayHeight(){return this.scaleY*this._height}set displayHeight(t){this.scaleY=t/this._height}setDisplaySize(t,e){return this.displayWidth=t,this.displayHeight=e,this}getCanvas(t){return t||(this.dirty=!0),this.canvas}getContext(t){return t||(this.dirty=!0),this.context}needRedraw(){return this.dirty=!0,this}resize(t,e){return this.setSize(t,e),this}}const l=Phaser.GameObjects.Components;Phaser.Class.mixin(d,[l.Alpha,l.BlendMode,l.Crop,l.Depth,l.Flip,l.GetBounds,l.Mask,l.Origin,l.Pipeline,l.PostPipeline,l.ScrollFactor,l.Tint,l.Transform,l.Visible,e,s,r]);const u=Phaser.Utils.Objects.GetValue;class c{constructor(t,e,i,s,r){void 0===t&&(t=0),void 0===e&&(e=t),void 0===i&&(i=0),void 0===s&&(s=0),void 0===r&&(r=0),this.cornerRadius={},this._width=0,this._height=0,this.setTo(t,e,i,s,r)}setTo(t,e,i,s,r){return this.setPosition(t,e),this.setRadius(r),this.setSize(i,s),this}setPosition(t,e){return this.x=t,this.y=e,this}setRadius(t){return void 0===t&&(t=0),this.radius=t,this}setSize(t,e){return this.width=t,this.height=e,this}get minWidth(){var t=this.cornerRadius;return Math.max(t.tl.x+t.tr.x,t.bl.x+t.br.x)}get minHeight(){var t=this.cornerRadius;return Math.max(t.tl.y+t.bl.y,t.tr.y+t.br.y)}get width(){return this._width}set width(t){null==t&&(t=0),this._width=Math.max(t,this.minWidth)}get height(){return this._height}set height(t){null==t&&(t=0),this._height=Math.max(t,this.minHeight)}get radius(){var t=this.cornerRadius;return Math.max(t.tl.x,t.tl.y,t.tr.x,t.tr.y,t.bl.x,t.bl.y,t.br.x,t.br.y)}set radius(t){var e,i;"number"==typeof t?(e=t,i=t):(e=u(t,"x",0),i=u(t,"y",0));var s=this.cornerRadius;s.tl=v(u(t,"tl",void 0),e,i),s.tr=v(u(t,"tr",void 0),e,i),s.bl=v(u(t,"bl",void 0),e,i),s.br=v(u(t,"br",void 0),e,i)}get radiusTL(){var t=this.cornerRadius.tl;return Math.max(t.x,t.y)}set radiusTL(t){g(this.cornerRadius.tl,t)}get radiusTR(){var t=this.cornerRadius.tr;return Math.max(t.x,t.y)}set radiusTR(t){g(this.cornerRadius.tr,t)}get radiusBL(){var t=this.cornerRadius.bl;return Math.max(t.x,t.y)}set radiusBL(t){g(this.cornerRadius.bl,t)}get radiusBR(){var t=this.cornerRadius.br;return Math.max(t.x,t.y)}set radiusBR(t){g(this.cornerRadius.br,t)}}var v=function(t,e,i){return void 0===t?t={x:e,y:i}:"number"==typeof t&&(t={x:t,y:t}),x(t),t},g=function(t,e){"number"==typeof e?(t.x=e,t.y=e):(t.x=u(e,"x",0),t.y=u(e,"y",0)),x(t)},x=function(t){t.convex=t.x>=0||t.y>=0,t.x=Math.abs(t.x),t.y=Math.abs(t.y)};const y=Phaser.Math.DegToRad;var p=function(t){return!t.hasOwnProperty("convex")||t.convex},m=function(t){return t.x>0&&t.y>0},f=function(t,e,i,s,r,h,a,n,o){if(n&&a>h?a-=360:!n&&a<h&&(a+=360),h=y(h),a=y(a),null==o)t.ellipse(e,i,s,r,0,h,a,n);else for(var d,l,u,c=(a-h)/(o+=1),v=0;v<=o;v++)u=h+c*v,d=e+s*Math.cos(u),l=i+r*Math.sin(u),t.lineTo(d,l)};const T=Phaser.Utils.Objects.GetValue,b={circle:0,ellipse:1,roundRectangle:2};return class extends d{constructor(t,e,i,s,r,h){super(t,e,i),this.type="rexCircleMaskImage",this.setTexture(s,r,h)}setTexture(t,e,i){"object"==typeof e&&(i=e,e=void 0),"string"==typeof i&&(i={maskType:i});var s=T(i,"maskType",0),r=T(i,"backgroundColor",void 0),h=T(i,"strokeColor",void 0),a=T(i,"strokeWidth",null!=h?10:0);if(void 0===s?s=0:"string"==typeof s&&(s=b[s]),this._textureKey=t,this._frameName=e,null===s)return this.loadTexture(t,e),this.dirty=!0,this;var n=this.scene.sys.textures.getFrame(t,e);if(!n)return this;n.cutWidth!==this.width||n.cutHeight!==this.height?this.setCanvasSize(n.cutWidth,n.cutHeight):this.clear();var o=this.canvas,d=this.context,l=o.width,u=o.height;null!=r&&(d.fillStyle=r,d.fillRect(0,0,l,u)),d.save(),d.beginPath();var v=a/2;switch(s){case 1:var g=(y=Math.floor(l/2))-v,x=(w=Math.floor(u/2))-v;d.ellipse(y,w,g,x,0,0,2*Math.PI);break;case 2:!function(t,e,i,s,r,h,a){var n,o,d,l,u,v=new c(e,i,s,r,h),g=v.minWidth,x=v.minHeight,y=s>=g?1:s/g,T=r>=x?1:r/x,b=v.cornerRadius;t.save(),t.beginPath(),t.translate(e,i),n=b.tl,m(n)?(o=n.x*y,d=n.y*T,p(n)?f(t,o,d,o,d,180,270,!1,a):f(t,0,0,o,d,90,0,!0,a),l=0,u=d):(t.lineTo(0,0),l=0,u=0),n=b.tr,m(n)?(o=n.x*y,d=n.y*T,p(n)?f(t,s-o,d,o,d,270,360,!1,a):f(t,s,0,o,d,180,90,!0,a)):t.lineTo(s,0),n=b.br,m(n)?(o=n.x*y,d=n.y*T,p(n)?f(t,s-o,r-d,o,d,0,90,!1,a):f(t,s,r,o,d,270,180,!0,a)):t.lineTo(s,r),n=b.bl,m(n)?(o=n.x*y,d=n.y*T,p(n)?f(t,o,r-d,o,d,90,180,!1,a):f(t,0,r,o,d,360,270,!0,a)):t.lineTo(0,r),t.lineTo(l,u),t.closePath(),t.restore()}(d,v,v,l-a,u-a,T(i,"radius",0),T(i,"iteration",void 0));break;default:var y=Math.floor(l/2),w=Math.floor(u/2),R=Math.min(y,w)-v;d.arc(y,w,R,0,2*Math.PI)}return null!=h&&(d.strokeStyle=h,d.lineWidth=a,d.stroke()),d.clip(),this.loadTexture(t,e),d.restore(),this.dirty=!0,this}resize(t,e){return this.setDisplaySize(t,e),this}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexcirclemaskimage=e();