UNPKG

diginext-pixi

Version:
1 lines 4.15 kB
"use strict";var _SvgSprite_instances,_SvgSprite_awake,__awaiter=this&&this.__awaiter||function(t,e,n,r){return new(n||(n=Promise))((function(i,s){function o(t){try{l(r.next(t))}catch(t){s(t)}}function a(t){try{l(r.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,a)}l((r=r.apply(t,e||[])).next())}))},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(t,e,n,r){if("a"===n&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?r:"a"===n?r.call(t):r?r.value:e.get(t)},__importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0});const InteractItem_1=__importDefault(require("../core/InteractItem")),Event_1=__importDefault(require("../data/Event")),getColorsSVG_1=__importDefault(require("../helper/svg/getColorsSVG")),pixi_js_1=require("pixi.js"),array_1=require("diginext-utils/dist/array"),scaleByWidth_1=__importDefault(require("../helper/scaleByWidth"));class SvgSprite extends InteractItem_1.default{constructor(t){super(),_SvgSprite_instances.add(this),this.type="SvgSprite",this.url="",this.baseContent="",this.currentContent="",this.props=Object.assign(Object.assign({},this.props),{props:t}),__classPrivateFieldGet(this,_SvgSprite_instances,"m",_SvgSprite_awake).call(this)}setupByUrl(t){return __awaiter(this,void 0,void 0,(function*(){return this.url=t,new Promise(((e,n)=>{const r=new XMLHttpRequest;r.open("GET",t,!0),r.onload=()=>__awaiter(this,void 0,void 0,(function*(){this.baseContent=r.response,this.currentContent=r.response;const t=yield this.addSpriteBySvgContent(r.response);this.emit(Event_1.default.LOADED),t&&e(!0)})),r.onerror=n,r.send()}))}))}addSpriteBySvgContent(t,e={maxSize:512}){return __awaiter(this,void 0,void 0,(function*(){const n=yield this.getTextureByContentSvg(t,e);return!!n&&(this.sprite=pixi_js_1.Sprite.from(n),this.sprite.anchor.set(.5),(0,scaleByWidth_1.default)(this.sprite,2048),this.addChild(this.sprite),!0)}))}changeTextureByContent(t,e={maxSize:512}){return __awaiter(this,void 0,void 0,(function*(){const n=yield this.getTextureByContentSvg(t,e),r=this.sprite;return!!n&&(r.texture.destroy(!0),r.texture=n,(0,scaleByWidth_1.default)(this.sprite,2048),!0)}))}getTextureByContentSvg(t,e={maxSize:512}){return new Promise(((n,r)=>{const i=new Blob([t],{type:"image/svg+xml"}),s=URL.createObjectURL(i),o=this.props.width||(null==e?void 0:e.maxSize),a=this.props.height||(null==e?void 0:e.maxSize);let l=new Image;l.onload=()=>__awaiter(this,void 0,void 0,(function*(){let t=document.createElement("canvas");t.width=o,t.height=a;let e=t.getContext("2d");null==e||e.drawImage(l,0,0,o,a);const r=pixi_js_1.Texture.from(t);n(r),t=null,e=null,l=null})),l.onerror=r,l.src=s}))}changeColorByIndex(t,e,n=!0){return __awaiter(this,void 0,void 0,(function*(){const r=this.getAllColors();return this.currentContent=this.currentContent.replace(r[t],e),n&&(yield this.changeTextureByContent(this.currentContent)),!0}))}changeColorByTone(t){return __awaiter(this,void 0,void 0,(function*(){this.tone=t;const e=this.getAllBaseColors();let n=this.baseContent;for(let r=0;r<e.length&&!(e.length>3&&r>=e.length-3);r++){const i=t[r%t.length];n=n.replace(e[r],i)}yield this.changeTextureByContent(n),this.currentContent=n}))}shuffleColor(){return __awaiter(this,void 0,void 0,(function*(){const t=this.getAllBaseColors();let e=this.getAllColors();t.length>5&&(e.length=t.length-3);let n=this.baseContent;this.tone?e=(0,array_1.getRandom)(this.tone):(0,array_1.shuffle)(e);for(let r=0;r<t.length&&!(t.length>3&&r>=t.length-3);r++)n=n.replace(t[r],e[r]);yield this.changeTextureByContent(this.currentContent),this.currentContent=n}))}getAllBaseColors(){return(0,getColorsSVG_1.default)(this.baseContent)}getAllColors(){return(0,getColorsSVG_1.default)(this.currentContent)}}exports.default=SvgSprite,_SvgSprite_instances=new WeakSet,_SvgSprite_awake=function(){};