@tsparticles/path-svg
Version:
tsParticles svg path
2 lines • 4.01 kB
JavaScript
/*! For license information please see tsparticles.path.svg.min.js.LICENSE.txt */
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],e);else{var s="object"==typeof exports?e(require("@tsparticles/engine")):e(t.window);for(var i in s)("object"==typeof exports?exports:t)[i]=s[i]}}(this,(t=>(()=>{var e={303:e=>{e.exports=t}},s={};function i(t){var n=s[t];if(void 0!==n)return n.exports;var h=s[t]={exports:{}};return e[t](h,h.exports,i),h.exports}i.d=(t,e)=>{for(var s in e)i.o(e,s)&&!i.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),i.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var n={};i.r(n),i.d(n,{loadSVGPath:()=>g,svgPathName:()=>r});var h,o=i(303);!function(t){t[t.normal=0]="normal",t[t.reverse=1]="reverse"}(h||(h={}));class a{constructor(){this._paths=[],this._reverse=!1,this._size={width:0,height:0},this._scale=1,this._offset={x:0,y:0,mode:o.PixelMode.percent},this._width=0}generate(t,e){const s=t.container.retina.pixelRatio;void 0===t.svgDirection&&(t.svgDirection=(0,o.getRandom)()>o.half?h.normal:h.reverse),void 0===t.svgPathIndex&&(t.svgPathIndex=Math.floor((0,o.getRandom)()*this._paths.length)),void 0===t.svgSpeed&&(t.svgSpeed=t.velocity.mult((t.retina.moveSpeed??1)*o.half).length),void 0===t.svgStep&&(t.svgStep=(0,o.randomInRange)({min:0,max:this._paths[t.svgPathIndex].length})*s),void 0===t.svgOffset&&(t.svgOffset={width:(0,o.randomInRange)({min:-this._width*o.half,max:this._width*o.half})*s,height:(0,o.randomInRange)({min:-this._width*o.half,max:this._width*o.half})*s}),void 0===t.svgInitialPosition&&(t.svgInitialPosition={...t.position}),t.velocity.x=0,t.velocity.y=0,t.svgDirection===h.normal?t.svgStep+=t.svgSpeed*e.factor:t.svgStep-=t.svgSpeed*e.factor;let i=this._paths[t.svgPathIndex];if(i){const e=i.length,s=1;t.svgStep>=e?(t.svgPathIndex=t.svgPathIndex+s,t.svgPathIndex>=this._paths.length&&(this._reverse?(t.svgPathIndex=this._paths.length-s,t.svgDirection=h.reverse):(t.svgPathIndex=0,t.svgStep=0))):t.svgStep<=0&&(t.svgPathIndex=t.svgPathIndex-s,t.svgPathIndex<0&&(this._reverse?(t.svgPathIndex=0,t.svgDirection=h.normal):(t.svgPathIndex=this._paths.length-s,i=this._paths[t.svgPathIndex],t.svgStep=i.length))),i=this._paths[t.svgPathIndex]}if(i){const e=i.element.getPointAtLength(t.svgStep),n=t.container.canvas.size,h=(0,o.getPosition)(this._offset,n),a=this._scale*s;t.position.x=(e.x-this._size.width*o.half)*a+t.svgInitialPosition.x+h.x+t.svgOffset.width,t.position.y=(e.y-this._size.height*o.half)*a+t.svgInitialPosition.y+h.y+t.svgOffset.height}return o.Vector.origin}init(t){const e=t.actualOptions.particles.move.path.options,s=e.position??this._offset;if(this._reverse=e.reverse??this._reverse,this._scale=e.scale??1,this._offset.x=s.x,this._offset.y=s.y,this._offset.mode=s.mode,this._width=e.width??0,e.url&&!e.path){const t=e.url;(async()=>{const e=await fetch(t),s=await e.text(),i=(new DOMParser).parseFromString(s,"image/svg+xml"),n=i.getElementsByTagName("svg")[0];let h=n.getElementsByTagName("path");h.length||(h=i.getElementsByTagName("path")),this._paths=[];for(let t=0;t<h.length;t++){const e=h.item(t);e&&this._paths.push({element:e,length:e.getTotalLength()})}this._size.height=parseFloat(n.getAttribute("height")??"0"),this._size.width=parseFloat(n.getAttribute("width")??"0")})()}else if(e.path){const t=e.path;this._paths=[];for(const e of t.data){const t=document.createElementNS("http://www.w3.org/2000/svg","path");t.setAttribute("d",e),this._paths.push({element:t,length:t.getTotalLength()})}this._size.height=t.size.height,this._size.width=t.size.width}}reset(){}update(){}}const r="svgPathGenerator";async function g(t,e=!0){t.checkVersion("3.9.1"),await t.addPathGenerator(r,new a,e)}return n})()));