UNPKG

diginext-pixi

Version:
1 lines 6.39 kB
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(t,e,i,s){void 0===s&&(s=i);var h=Object.getOwnPropertyDescriptor(e,i);h&&!("get"in h?!e.__esModule:h.writable||h.configurable)||(h={enumerable:!0,get:function(){return e[i]}}),Object.defineProperty(t,s,h)}:function(t,e,i,s){void 0===s&&(s=i),t[s]=e[i]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:!0,value:e})}:function(t,e){t.default=e}),__importStar=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var i in t)"default"!==i&&Object.prototype.hasOwnProperty.call(t,i)&&__createBinding(e,t,i);return __setModuleDefault(e,t),e};Object.defineProperty(exports,"__esModule",{value:!0});const PIXI=__importStar(require("pixi.js")),dashLineOptionsDefault={dash:[10,5],width:1,color:16777215,alpha:1,scale:1,useTexture:!1,alignment:.5};class DashLine{constructor(t,e={}){var i;this.cursor=new PIXI.Point,this.scale=1,this.graphics=t,e=Object.assign(Object.assign({},dashLineOptionsDefault),e),this.dash=e.dash,this.dashSize=(null===(i=this.dash)||void 0===i?void 0:i.reduce(((t,e)=>t+e)))||1,this.useTexture=e.useTexture||!1,this.options=e,this.setLineStyle()}setLineStyle(){const t=this.options;if(this.useTexture){const e=DashLine.getTexture(t,this.dashSize||1);this.graphics.lineTextureStyle({width:((null==t?void 0:t.width)||1)*((null==t?void 0:t.scale)||1),color:t.color,alpha:t.alpha,texture:e,alignment:t.alignment}),this.activeTexture=e}else this.graphics.lineStyle({width:((null==t?void 0:t.width)||1)*((null==t?void 0:t.scale)||1),color:t.color,alpha:t.alpha,cap:t.cap,join:t.join,alignment:t.alignment});this.scale=t.scale||1}static distance(t,e,i,s){return Math.sqrt(Math.pow(i-t,2)+Math.pow(s-e,2))}moveTo(t,e){return this.lineLength=0,this.cursor.set(t,e),this.start=new PIXI.Point(t,e),this.graphics.moveTo(this.cursor.x,this.cursor.y),this}lineTo(t,e,i){var s,h,o,n,a,l;void 0===typeof this.lineLength&&this.moveTo(0,0);const r=DashLine.distance(this.cursor.x,this.cursor.y,t,e),c=Math.atan2(e-this.cursor.y,t-this.cursor.x),d=i&&t===(null===(s=this.start)||void 0===s?void 0:s.x)&&e===this.start.y;if(this.useTexture)if(this.graphics.moveTo(this.cursor.x,this.cursor.y),this.adjustLineStyle(c),d&&((null===(h=this.dash)||void 0===h?void 0:h.length)||0)%2==0){this.dash=this.dash||[];const i=Math.min(this.dash[this.dash.length-1],r);this.graphics.lineTo(t-Math.cos(c)*i,e-Math.sin(c)*i),this.graphics.closePath()}else this.graphics.lineTo(t,e);else{const t=Math.cos(c),e=Math.sin(c);let i=this.cursor.x,s=this.cursor.y;const h=(this.lineLength||0)%(this.dashSize*this.scale);let u=0,p=0,v=0;this.dash=this.dash||[];for(let t=0;t<this.dash.length;t++){const e=this.dash[t]*this.scale;if(h<v+e){u=t,p=h-v;break}v+=e}let g=r;for(;g>0;){const h=this.dash[u]*this.scale-p;let r=g>h?h:g;if(d){if(DashLine.distance(i+t*r,s+e*r,(null===(o=this.start)||void 0===o?void 0:o.x)||0,(null===(n=this.start)||void 0===n?void 0:n.y)||0)<=r){if(u%2==0){const h=DashLine.distance(i,s,(null===(a=this.start)||void 0===a?void 0:a.x)||0,(null===(l=this.start)||void 0===l?void 0:l.y)||0)-this.dash[this.dash.length-1]*this.scale;i+=t*h,s+=e*h,this.graphics.lineTo(i,s)}break}}i+=t*r,s+=e*r,u%2?this.graphics.moveTo(i,s):this.graphics.lineTo(i,s),g-=r,u++,u=u===this.dash.length?0:u,p=0}}return this.lineLength=(this.lineLength||0)+r,this.cursor.set(t,e),this}closePath(){var t,e;this.lineTo((null===(t=this.start)||void 0===t?void 0:t.x)||0,(null===(e=this.start)||void 0===e?void 0:e.y)||0,!0)}drawCircle(t,e,i,s=80,h){const o=2*Math.PI/s;let n,a=0;h?(n=new PIXI.Point(t+Math.cos(a)*i,e+Math.sin(a)*i),h.apply(n,n),this.moveTo(n[0],n[1])):(n=new PIXI.Point(t+Math.cos(a)*i,e+Math.sin(a)*i),this.moveTo(n.x,n.y)),a+=o;for(let h=1;h<s+1;h++){const l=h===s?n:[t+Math.cos(a)*i,e+Math.sin(a)*i];this.lineTo(l[0],l[1]),a+=o}return this}drawEllipse(t,e,i,s,h=80,o){const n=2*Math.PI/h;let a;const l=new PIXI.Point;for(let h=0;h<2*Math.PI;h+=n){let n=t-i*Math.sin(h),r=e-s*Math.cos(h);o&&(l.set(n,r),o.apply(l,l),n=l.x,r=l.y),0===h?(this.moveTo(n,r),a={x:n,y:r}):this.lineTo(n,r)}return this.lineTo(a.x,a.y,!0),this}drawPolygon(t,e){const i=new PIXI.Point;if("number"==typeof t[0])if(e){i.set(t[0],t[1]),e.apply(i,i),this.moveTo(i.x,i.y);for(let s=2;s<t.length;s+=2)i.set(t[s],t[s+1]),e.apply(i,i),this.lineTo(i.x,i.y,s===t.length-2)}else{this.moveTo(t[0],t[1]);for(let e=2;e<t.length;e+=2)this.lineTo(t[e],t[e+1],e===t.length-2)}else if(e){const s=t[0];i.copyFrom(s),e.apply(i,i),this.moveTo(i.x,i.y);for(let s=1;s<t.length;s++){const h=t[s];i.copyFrom(h),e.apply(i,i),this.lineTo(i.x,i.y,s===t.length-1)}}else{const e=t[0];this.moveTo(e.x,e.y);for(let e=1;e<t.length;e++){const i=t[e];this.lineTo(i.x,i.y,e===t.length-1)}}return this}drawRect(t,e,i,s,h){if(h){const o=new PIXI.Point;o.set(t,e),h.apply(o,o),this.moveTo(o.x,o.y),o.set(t+i,e),h.apply(o,o),this.lineTo(o.x,o.y),o.set(t+i,e+s),h.apply(o,o),this.lineTo(o.x,o.y),o.set(t,e+s),h.apply(o,o),this.lineTo(o.x,o.y),o.set(t,e),h.apply(o,o),this.lineTo(o.x,o.y,!0)}else this.moveTo(t,e).lineTo(t+i,e).lineTo(t+i,e+s).lineTo(t,e+s).lineTo(t,e,!0);return this}adjustLineStyle(t){const e=this.graphics.line;e.matrix=new PIXI.Matrix,t&&e.matrix.rotate(t),1!==this.scale&&e.matrix.scale(this.scale,this.scale);const i=-(this.lineLength||0);e.matrix.translate(this.cursor.x+i*Math.cos(t),this.cursor.y+i*Math.sin(t)),this.graphics.lineStyle(e)}static getTexture(t,e){var i,s,h,o,n;const a=(null===(i=null==t?void 0:t.dash)||void 0===i?void 0:i.toString())||"";if(DashLine.dashTextureCache[a])return DashLine.dashTextureCache[a];const l=document.createElement("canvas");l.width=e,l.height=Math.ceil(t.width||0);const r=l.getContext("2d");if(!r)return void console.warn("Did not get context from canvas");r.strokeStyle="white",r.globalAlpha=t.alpha,r.lineWidth=t.width;let c=0;const d=(t.width||0)/2;r.moveTo(c,d);for(let e=0;e<((null===(s=t.dash)||void 0===s?void 0:s.length)||0);e+=2)c+=(null===(h=t.dash)||void 0===h?void 0:h[e])||0,r.lineTo(c,d),(null===(o=t.dash)||void 0===o?void 0:o.length)!==e+1&&(c+=(null===(n=t.dash)||void 0===n?void 0:n[e+1])||0,r.moveTo(c,d));r.stroke();const u=DashLine.dashTextureCache[a]=PIXI.Texture.from(l);return u.baseTexture.scaleMode=PIXI.SCALE_MODES.NEAREST,u}}exports.default=DashLine,DashLine.dashTextureCache={};