@tsparticles/path-svg 4.0.0-alpha.2 → 4.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/905.min.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";(this.webpackChunk_tsparticles_path_svg=this.webpackChunk_tsparticles_path_svg||[]).push([[905],{905(t,e,s){s.d(e,{SVGPathGenerator:()=>n});var i,h,a=s(303);(i=h||(h={}))[i.normal=0]="normal",i[i.reverse=1]="reverse";class n{_container;_offset;_paths;_reverse;_scale;_size;_width;constructor(t){this._container=t,this._paths=[],this._reverse=!1,this._size={width:0,height:0},this._scale=1,this._offset={x:0,y:0,mode:a.PixelMode.percent},this._width=0}generate(t,e){let s=this._container.retina.pixelRatio;t.svgDirection??=(0,a.getRandom)()>a.half?h.normal:h.reverse,t.svgPathIndex??=Math.floor((0,a.getRandom)()*this._paths.length),t.svgSpeed??=t.velocity.mult((t.retina.moveSpeed??1)*a.half).length,t.svgStep??=(0,a.randomInRangeValue)({min:0,max:this._paths[t.svgPathIndex].length})*s,t.svgOffset??={width:(0,a.randomInRangeValue)({min:-this._width*a.half,max:this._width*a.half})*s,height:(0,a.randomInRangeValue)({min:-this._width*a.half,max:this._width*a.half})*s},t.svgInitialPosition??=t.position.copy(),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],n=i.length;t.svgStep>=n?(t.svgPathIndex=t.svgPathIndex+1,t.svgPathIndex>=this._paths.length&&(this._reverse?(t.svgPathIndex=this._paths.length-1,t.svgDirection=h.reverse):(t.svgPathIndex=0,t.svgStep=0))):t.svgStep<=0&&(t.svgPathIndex=t.svgPathIndex-1,t.svgPathIndex<0&&(this._reverse?(t.svgPathIndex=0,t.svgDirection=h.normal):(t.svgPathIndex=this._paths.length-1,i=this._paths[t.svgPathIndex],t.svgStep=i.length)));let r=(i=this._paths[t.svgPathIndex]).element.getPointAtLength(t.svgStep),g=this._container.canvas.size,o=(0,a.getPosition)(this._offset,g),l=this._scale*s;return t.position.x=(r.x-this._size.width*a.half)*l+t.svgInitialPosition.x+o.x+t.svgOffset.width,t.position.y=(r.y-this._size.height*a.half)*l+t.svgInitialPosition.y+o.y+t.svgOffset.height,a.Vector.origin}init(){let t=this._container.actualOptions.particles.move.path.options,e=t.position??this._offset;if(this._reverse=t.reverse??this._reverse,this._scale=t.scale??1,this._offset.x=e.x,this._offset.y=e.y,this._offset.mode=e.mode,this._width=t.width??0,t.url&&!t.path){let e=t.url;(async()=>{let t=await fetch(e),{paths:s,size:i}=function(t){let e=new DOMParser().parseFromString(t,"image/svg+xml").querySelector("svg");if(!e)return{paths:[],size:{width:0,height:0}};let s=e.querySelectorAll("path"),i=[];for(let t of s)t instanceof SVGPathElement&&i.push({element:t,length:t.getTotalLength()});return{paths:i,size:{width:Number.parseFloat(e.getAttribute("width")??"0"),height:Number.parseFloat(e.getAttribute("height")??"0")}}}(await t.text());this._paths=s,this._size.width=i.width,this._size.height=i.height})()}else if(t.path){var s;let e,i=t.path;this._paths=(s=t.path.data,e=(0,a.safeDocument)(),s.map(t=>{let s=e.createElementNS("http://www.w3.org/2000/svg","path");return s.setAttribute("d",t),{element:s,length:s.getTotalLength()}})),this._size.height=i.size.height,this._size.width=i.size.width}}reset(){}update(){}}}}]);
@@ -1,4 +1,6 @@
1
- import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue, safeDocument, } from "@tsparticles/engine";
1
+ import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue, } from "@tsparticles/engine";
2
+ import { createSVGPaths } from "./createSVGPaths.js";
3
+ import { loadSVGFromString } from "./loadSVGFromString.js";
2
4
  var SVGPathDirection;
3
5
  (function (SVGPathDirection) {
4
6
  SVGPathDirection[SVGPathDirection["normal"] = 0] = "normal";
@@ -6,7 +8,15 @@ var SVGPathDirection;
6
8
  })(SVGPathDirection || (SVGPathDirection = {}));
7
9
  const defaultSpeed = 1, minStep = 0, minIndex = 0, minWidth = 0, minScale = 1;
8
10
  export class SVGPathGenerator {
9
- constructor() {
11
+ _container;
12
+ _offset;
13
+ _paths;
14
+ _reverse;
15
+ _scale;
16
+ _size;
17
+ _width;
18
+ constructor(container) {
19
+ this._container = container;
10
20
  this._paths = [];
11
21
  this._reverse = false;
12
22
  this._size = { width: 0, height: 0 };
@@ -15,7 +25,7 @@ export class SVGPathGenerator {
15
25
  this._width = 0;
16
26
  }
17
27
  generate(particle, delta) {
18
- const container = particle.container, pxRatio = container.retina.pixelRatio;
28
+ const container = this._container, pxRatio = container.retina.pixelRatio;
19
29
  particle.svgDirection ??= getRandom() > half ? SVGPathDirection.normal : SVGPathDirection.reverse;
20
30
  particle.svgPathIndex ??= Math.floor(getRandom() * this._paths.length);
21
31
  particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * half).length;
@@ -63,21 +73,15 @@ export class SVGPathGenerator {
63
73
  }
64
74
  }
65
75
  path = this._paths[particle.svgPathIndex];
66
- const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = particle.container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
76
+ const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
67
77
  particle.position.x =
68
- (pos.x - this._size.width * half) * scale +
69
- particle.svgInitialPosition.x +
70
- offset.x +
71
- particle.svgOffset.width;
78
+ (pos.x - this._size.width * half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
72
79
  particle.position.y =
73
- (pos.y - this._size.height * half) * scale +
74
- particle.svgInitialPosition.y +
75
- offset.y +
76
- particle.svgOffset.height;
80
+ (pos.y - this._size.height * half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
77
81
  return Vector.origin;
78
82
  }
79
- init(container) {
80
- const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
83
+ init() {
84
+ const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
81
85
  this._reverse = options.reverse ?? this._reverse;
82
86
  this._scale = options.scale ?? minScale;
83
87
  this._offset.x = position.x;
@@ -87,37 +91,15 @@ export class SVGPathGenerator {
87
91
  if (options.url && !options.path) {
88
92
  const url = options.url;
89
93
  void (async () => {
90
- const response = await fetch(url), data = await response.text();
91
- const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), firstIndex = 0, svg = doc.getElementsByTagName("svg")[firstIndex];
92
- let svgPaths = svg.getElementsByTagName("path");
93
- if (!svgPaths.length) {
94
- svgPaths = doc.getElementsByTagName("path");
95
- }
96
- this._paths = [];
97
- for (let i = 0; i < svgPaths.length; i++) {
98
- const path = svgPaths.item(i);
99
- if (path) {
100
- this._paths.push({
101
- element: path,
102
- length: path.getTotalLength(),
103
- });
104
- }
105
- }
106
- this._size.height = Number.parseFloat(svg.getAttribute("height") ?? "0");
107
- this._size.width = Number.parseFloat(svg.getAttribute("width") ?? "0");
94
+ const response = await fetch(url), data = await response.text(), { paths, size } = loadSVGFromString(data);
95
+ this._paths = paths;
96
+ this._size.width = size.width;
97
+ this._size.height = size.height;
108
98
  })();
109
99
  }
110
100
  else if (options.path) {
111
101
  const path = options.path;
112
- this._paths = [];
113
- for (const item of path.data) {
114
- const element = safeDocument().createElementNS("http://www.w3.org/2000/svg", "path");
115
- element.setAttribute("d", item);
116
- this._paths.push({
117
- element,
118
- length: element.getTotalLength(),
119
- });
120
- }
102
+ this._paths = createSVGPaths(options.path.data);
121
103
  this._size.height = path.size.height;
122
104
  this._size.width = path.size.width;
123
105
  }
@@ -0,0 +1,12 @@
1
+ import { safeDocument } from "@tsparticles/engine";
2
+ export function createSVGPaths(data) {
3
+ const doc = safeDocument();
4
+ return data.map(d => {
5
+ const element = doc.createElementNS("http://www.w3.org/2000/svg", "path");
6
+ element.setAttribute("d", d);
7
+ return {
8
+ element,
9
+ length: element.getTotalLength(),
10
+ };
11
+ });
12
+ }
package/browser/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  export const svgPathName = "svgPathGenerator";
2
- export function loadSVGPath(engine) {
3
- engine.checkVersion("4.0.0-alpha.2");
4
- engine.register(async (e) => {
5
- const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
6
- e.addPathGenerator(svgPathName, new SVGPathGenerator());
2
+ export async function loadSVGPath(engine) {
3
+ engine.checkVersion("4.0.0-alpha.21");
4
+ await engine.register(e => {
5
+ e.addPathGenerator(svgPathName, async (container) => {
6
+ const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
7
+ return new SVGPathGenerator(container);
8
+ });
7
9
  });
8
10
  }
@@ -0,0 +1,24 @@
1
+ export function loadSVGFromString(svgText) {
2
+ const parser = new DOMParser(), doc = parser.parseFromString(svgText, "image/svg+xml"), svg = doc.querySelector("svg");
3
+ if (!svg) {
4
+ return { paths: [], size: { width: 0, height: 0 } };
5
+ }
6
+ const pathElements = svg.querySelectorAll("path"), paths = [];
7
+ for (const node of pathElements) {
8
+ if (!(node instanceof SVGPathElement)) {
9
+ continue;
10
+ }
11
+ paths.push({
12
+ element: node,
13
+ length: node.getTotalLength(),
14
+ });
15
+ }
16
+ const size = {
17
+ width: Number.parseFloat(svg.getAttribute("width") ?? "0"),
18
+ height: Number.parseFloat(svg.getAttribute("height") ?? "0"),
19
+ };
20
+ return {
21
+ paths,
22
+ size,
23
+ };
24
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,6 @@
1
- import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue, safeDocument, } from "@tsparticles/engine";
1
+ import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue, } from "@tsparticles/engine";
2
+ import { createSVGPaths } from "./createSVGPaths.js";
3
+ import { loadSVGFromString } from "./loadSVGFromString.js";
2
4
  var SVGPathDirection;
3
5
  (function (SVGPathDirection) {
4
6
  SVGPathDirection[SVGPathDirection["normal"] = 0] = "normal";
@@ -6,7 +8,15 @@ var SVGPathDirection;
6
8
  })(SVGPathDirection || (SVGPathDirection = {}));
7
9
  const defaultSpeed = 1, minStep = 0, minIndex = 0, minWidth = 0, minScale = 1;
8
10
  export class SVGPathGenerator {
9
- constructor() {
11
+ _container;
12
+ _offset;
13
+ _paths;
14
+ _reverse;
15
+ _scale;
16
+ _size;
17
+ _width;
18
+ constructor(container) {
19
+ this._container = container;
10
20
  this._paths = [];
11
21
  this._reverse = false;
12
22
  this._size = { width: 0, height: 0 };
@@ -15,7 +25,7 @@ export class SVGPathGenerator {
15
25
  this._width = 0;
16
26
  }
17
27
  generate(particle, delta) {
18
- const container = particle.container, pxRatio = container.retina.pixelRatio;
28
+ const container = this._container, pxRatio = container.retina.pixelRatio;
19
29
  particle.svgDirection ??= getRandom() > half ? SVGPathDirection.normal : SVGPathDirection.reverse;
20
30
  particle.svgPathIndex ??= Math.floor(getRandom() * this._paths.length);
21
31
  particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * half).length;
@@ -63,21 +73,15 @@ export class SVGPathGenerator {
63
73
  }
64
74
  }
65
75
  path = this._paths[particle.svgPathIndex];
66
- const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = particle.container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
76
+ const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
67
77
  particle.position.x =
68
- (pos.x - this._size.width * half) * scale +
69
- particle.svgInitialPosition.x +
70
- offset.x +
71
- particle.svgOffset.width;
78
+ (pos.x - this._size.width * half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
72
79
  particle.position.y =
73
- (pos.y - this._size.height * half) * scale +
74
- particle.svgInitialPosition.y +
75
- offset.y +
76
- particle.svgOffset.height;
80
+ (pos.y - this._size.height * half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
77
81
  return Vector.origin;
78
82
  }
79
- init(container) {
80
- const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
83
+ init() {
84
+ const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
81
85
  this._reverse = options.reverse ?? this._reverse;
82
86
  this._scale = options.scale ?? minScale;
83
87
  this._offset.x = position.x;
@@ -87,37 +91,15 @@ export class SVGPathGenerator {
87
91
  if (options.url && !options.path) {
88
92
  const url = options.url;
89
93
  void (async () => {
90
- const response = await fetch(url), data = await response.text();
91
- const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), firstIndex = 0, svg = doc.getElementsByTagName("svg")[firstIndex];
92
- let svgPaths = svg.getElementsByTagName("path");
93
- if (!svgPaths.length) {
94
- svgPaths = doc.getElementsByTagName("path");
95
- }
96
- this._paths = [];
97
- for (let i = 0; i < svgPaths.length; i++) {
98
- const path = svgPaths.item(i);
99
- if (path) {
100
- this._paths.push({
101
- element: path,
102
- length: path.getTotalLength(),
103
- });
104
- }
105
- }
106
- this._size.height = Number.parseFloat(svg.getAttribute("height") ?? "0");
107
- this._size.width = Number.parseFloat(svg.getAttribute("width") ?? "0");
94
+ const response = await fetch(url), data = await response.text(), { paths, size } = loadSVGFromString(data);
95
+ this._paths = paths;
96
+ this._size.width = size.width;
97
+ this._size.height = size.height;
108
98
  })();
109
99
  }
110
100
  else if (options.path) {
111
101
  const path = options.path;
112
- this._paths = [];
113
- for (const item of path.data) {
114
- const element = safeDocument().createElementNS("http://www.w3.org/2000/svg", "path");
115
- element.setAttribute("d", item);
116
- this._paths.push({
117
- element,
118
- length: element.getTotalLength(),
119
- });
120
- }
102
+ this._paths = createSVGPaths(options.path.data);
121
103
  this._size.height = path.size.height;
122
104
  this._size.width = path.size.width;
123
105
  }
@@ -0,0 +1,12 @@
1
+ import { safeDocument } from "@tsparticles/engine";
2
+ export function createSVGPaths(data) {
3
+ const doc = safeDocument();
4
+ return data.map(d => {
5
+ const element = doc.createElementNS("http://www.w3.org/2000/svg", "path");
6
+ element.setAttribute("d", d);
7
+ return {
8
+ element,
9
+ length: element.getTotalLength(),
10
+ };
11
+ });
12
+ }
package/cjs/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  export const svgPathName = "svgPathGenerator";
2
- export function loadSVGPath(engine) {
3
- engine.checkVersion("4.0.0-alpha.2");
4
- engine.register(async (e) => {
5
- const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
6
- e.addPathGenerator(svgPathName, new SVGPathGenerator());
2
+ export async function loadSVGPath(engine) {
3
+ engine.checkVersion("4.0.0-alpha.21");
4
+ await engine.register(e => {
5
+ e.addPathGenerator(svgPathName, async (container) => {
6
+ const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
7
+ return new SVGPathGenerator(container);
8
+ });
7
9
  });
8
10
  }
@@ -0,0 +1,24 @@
1
+ export function loadSVGFromString(svgText) {
2
+ const parser = new DOMParser(), doc = parser.parseFromString(svgText, "image/svg+xml"), svg = doc.querySelector("svg");
3
+ if (!svg) {
4
+ return { paths: [], size: { width: 0, height: 0 } };
5
+ }
6
+ const pathElements = svg.querySelectorAll("path"), paths = [];
7
+ for (const node of pathElements) {
8
+ if (!(node instanceof SVGPathElement)) {
9
+ continue;
10
+ }
11
+ paths.push({
12
+ element: node,
13
+ length: node.getTotalLength(),
14
+ });
15
+ }
16
+ const size = {
17
+ width: Number.parseFloat(svg.getAttribute("width") ?? "0"),
18
+ height: Number.parseFloat(svg.getAttribute("height") ?? "0"),
19
+ };
20
+ return {
21
+ paths,
22
+ size,
23
+ };
24
+ }
package/cjs/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -4,7 +4,7 @@
4
4
  * Demo / Generator : https://particles.js.org/
5
5
  * GitHub : https://www.github.com/matteobruni/tsparticles
6
6
  * How to use? : Check the GitHub README
7
- * v4.0.0-alpha.2
7
+ * v4.0.0-alpha.21
8
8
  */
9
9
  "use strict";
10
10
  /*
@@ -23,7 +23,27 @@
23
23
  \******************************************/
24
24
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
25
25
 
26
- eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ SVGPathGenerator: () => (/* binding */ SVGPathGenerator)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nvar SVGPathDirection;\n(function (SVGPathDirection) {\n SVGPathDirection[SVGPathDirection[\"normal\"] = 0] = \"normal\";\n SVGPathDirection[SVGPathDirection[\"reverse\"] = 1] = \"reverse\";\n})(SVGPathDirection || (SVGPathDirection = {}));\nconst defaultSpeed = 1,\n minStep = 0,\n minIndex = 0,\n minWidth = 0,\n minScale = 1;\nclass SVGPathGenerator {\n constructor() {\n this._paths = [];\n this._reverse = false;\n this._size = {\n width: 0,\n height: 0\n };\n this._scale = 1;\n this._offset = {\n x: 0,\n y: 0,\n mode: _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.PixelMode.percent\n };\n this._width = 0;\n }\n generate(particle, delta) {\n const container = particle.container,\n pxRatio = container.retina.pixelRatio;\n particle.svgDirection ??= (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() > _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half ? SVGPathDirection.normal : SVGPathDirection.reverse;\n particle.svgPathIndex ??= Math.floor((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() * this._paths.length);\n particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half).length;\n particle.svgStep ??= (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.randomInRangeValue)({\n min: 0,\n max: this._paths[particle.svgPathIndex].length\n }) * pxRatio;\n particle.svgOffset ??= {\n width: (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.randomInRangeValue)({\n min: -this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half,\n max: this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half\n }) * pxRatio,\n height: (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.randomInRangeValue)({\n min: -this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half,\n max: this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half\n }) * pxRatio\n };\n particle.svgInitialPosition ??= particle.position.copy();\n particle.velocity.x = 0;\n particle.velocity.y = 0;\n if (particle.svgDirection === SVGPathDirection.normal) {\n particle.svgStep += particle.svgSpeed * delta.factor;\n } else {\n particle.svgStep -= particle.svgSpeed * delta.factor;\n }\n let path = this._paths[particle.svgPathIndex];\n const pathLength = path.length,\n indexOffset = 1;\n if (particle.svgStep >= pathLength) {\n particle.svgPathIndex = particle.svgPathIndex + indexOffset;\n if (particle.svgPathIndex >= this._paths.length) {\n if (this._reverse) {\n particle.svgPathIndex = this._paths.length - indexOffset;\n particle.svgDirection = SVGPathDirection.reverse;\n } else {\n particle.svgPathIndex = 0;\n particle.svgStep = 0;\n }\n }\n } else if (particle.svgStep <= minStep) {\n particle.svgPathIndex = particle.svgPathIndex - indexOffset;\n if (particle.svgPathIndex < minIndex) {\n if (this._reverse) {\n particle.svgPathIndex = 0;\n particle.svgDirection = SVGPathDirection.normal;\n } else {\n particle.svgPathIndex = this._paths.length - indexOffset;\n path = this._paths[particle.svgPathIndex];\n particle.svgStep = path.length;\n }\n }\n }\n path = this._paths[particle.svgPathIndex];\n const pathElement = path.element,\n pos = pathElement.getPointAtLength(particle.svgStep),\n canvasSize = particle.container.canvas.size,\n offset = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getPosition)(this._offset, canvasSize),\n scale = this._scale * pxRatio;\n particle.position.x = (pos.x - this._size.width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;\n particle.position.y = (pos.y - this._size.height * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;\n return _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.Vector.origin;\n }\n init(container) {\n const options = container.actualOptions.particles.move.path.options,\n position = options.position ?? this._offset;\n this._reverse = options.reverse ?? this._reverse;\n this._scale = options.scale ?? minScale;\n this._offset.x = position.x;\n this._offset.y = position.y;\n this._offset.mode = position.mode;\n this._width = options.width ?? minWidth;\n if (options.url && !options.path) {\n const url = options.url;\n void (async () => {\n const response = await fetch(url),\n data = await response.text();\n const parser = new DOMParser(),\n doc = parser.parseFromString(data, \"image/svg+xml\"),\n firstIndex = 0,\n svg = doc.getElementsByTagName(\"svg\")[firstIndex];\n let svgPaths = svg.getElementsByTagName(\"path\");\n if (!svgPaths.length) {\n svgPaths = doc.getElementsByTagName(\"path\");\n }\n this._paths = [];\n for (let i = 0; i < svgPaths.length; i++) {\n const path = svgPaths.item(i);\n if (path) {\n this._paths.push({\n element: path,\n length: path.getTotalLength()\n });\n }\n }\n this._size.height = Number.parseFloat(svg.getAttribute(\"height\") ?? \"0\");\n this._size.width = Number.parseFloat(svg.getAttribute(\"width\") ?? \"0\");\n })();\n } else if (options.path) {\n const path = options.path;\n this._paths = [];\n for (const item of path.data) {\n const element = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.safeDocument)().createElementNS(\"http://www.w3.org/2000/svg\", \"path\");\n element.setAttribute(\"d\", item);\n this._paths.push({\n element,\n length: element.getTotalLength()\n });\n }\n this._size.height = path.size.height;\n this._size.width = path.size.width;\n }\n }\n reset() {}\n update() {}\n}\n\n//# sourceURL=webpack://@tsparticles/path-svg/./dist/browser/SVGPathGenerator.js?\n}");
26
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ SVGPathGenerator: () => (/* binding */ SVGPathGenerator)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _createSVGPaths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./createSVGPaths.js */ \"./dist/browser/createSVGPaths.js\");\n/* harmony import */ var _loadSVGFromString_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./loadSVGFromString.js */ \"./dist/browser/loadSVGFromString.js\");\n\n\n\nvar SVGPathDirection;\n(function(SVGPathDirection) {\n SVGPathDirection[SVGPathDirection[\"normal\"] = 0] = \"normal\";\n SVGPathDirection[SVGPathDirection[\"reverse\"] = 1] = \"reverse\";\n})(SVGPathDirection || (SVGPathDirection = {}));\nconst defaultSpeed = 1, minStep = 0, minIndex = 0, minWidth = 0, minScale = 1;\nclass SVGPathGenerator {\n _container;\n _offset;\n _paths;\n _reverse;\n _scale;\n _size;\n _width;\n constructor(container){\n this._container = container;\n this._paths = [];\n this._reverse = false;\n this._size = {\n width: 0,\n height: 0\n };\n this._scale = 1;\n this._offset = {\n x: 0,\n y: 0,\n mode: _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.PixelMode.percent\n };\n this._width = 0;\n }\n generate(particle, delta) {\n const container = this._container, pxRatio = container.retina.pixelRatio;\n particle.svgDirection ??= (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() > _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half ? SVGPathDirection.normal : SVGPathDirection.reverse;\n particle.svgPathIndex ??= Math.floor((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() * this._paths.length);\n particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half).length;\n particle.svgStep ??= (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.randomInRangeValue)({\n min: 0,\n max: this._paths[particle.svgPathIndex].length\n }) * pxRatio;\n particle.svgOffset ??= {\n width: (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.randomInRangeValue)({\n min: -this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half,\n max: this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half\n }) * pxRatio,\n height: (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.randomInRangeValue)({\n min: -this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half,\n max: this._width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half\n }) * pxRatio\n };\n particle.svgInitialPosition ??= particle.position.copy();\n particle.velocity.x = 0;\n particle.velocity.y = 0;\n if (particle.svgDirection === SVGPathDirection.normal) {\n particle.svgStep += particle.svgSpeed * delta.factor;\n } else {\n particle.svgStep -= particle.svgSpeed * delta.factor;\n }\n let path = this._paths[particle.svgPathIndex];\n const pathLength = path.length, indexOffset = 1;\n if (particle.svgStep >= pathLength) {\n particle.svgPathIndex = particle.svgPathIndex + indexOffset;\n if (particle.svgPathIndex >= this._paths.length) {\n if (this._reverse) {\n particle.svgPathIndex = this._paths.length - indexOffset;\n particle.svgDirection = SVGPathDirection.reverse;\n } else {\n particle.svgPathIndex = 0;\n particle.svgStep = 0;\n }\n }\n } else if (particle.svgStep <= minStep) {\n particle.svgPathIndex = particle.svgPathIndex - indexOffset;\n if (particle.svgPathIndex < minIndex) {\n if (this._reverse) {\n particle.svgPathIndex = 0;\n particle.svgDirection = SVGPathDirection.normal;\n } else {\n particle.svgPathIndex = this._paths.length - indexOffset;\n path = this._paths[particle.svgPathIndex];\n particle.svgStep = path.length;\n }\n }\n }\n path = this._paths[particle.svgPathIndex];\n const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getPosition)(this._offset, canvasSize), scale = this._scale * pxRatio;\n particle.position.x = (pos.x - this._size.width * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;\n particle.position.y = (pos.y - this._size.height * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;\n return _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.Vector.origin;\n }\n init() {\n const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;\n this._reverse = options.reverse ?? this._reverse;\n this._scale = options.scale ?? minScale;\n this._offset.x = position.x;\n this._offset.y = position.y;\n this._offset.mode = position.mode;\n this._width = options.width ?? minWidth;\n if (options.url && !options.path) {\n const url = options.url;\n void (async ()=>{\n const response = await fetch(url), data = await response.text(), { paths, size } = (0,_loadSVGFromString_js__WEBPACK_IMPORTED_MODULE_2__.loadSVGFromString)(data);\n this._paths = paths;\n this._size.width = size.width;\n this._size.height = size.height;\n })();\n } else if (options.path) {\n const path = options.path;\n this._paths = (0,_createSVGPaths_js__WEBPACK_IMPORTED_MODULE_1__.createSVGPaths)(options.path.data);\n this._size.height = path.size.height;\n this._size.width = path.size.width;\n }\n }\n reset() {}\n update() {}\n}\n\n\n//# sourceURL=webpack://@tsparticles/path-svg/./dist/browser/SVGPathGenerator.js?\n}");
27
+
28
+ /***/ },
29
+
30
+ /***/ "./dist/browser/createSVGPaths.js"
31
+ /*!****************************************!*\
32
+ !*** ./dist/browser/createSVGPaths.js ***!
33
+ \****************************************/
34
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
35
+
36
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ createSVGPaths: () => (/* binding */ createSVGPaths)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nfunction createSVGPaths(data) {\n const doc = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.safeDocument)();\n return data.map((d)=>{\n const element = doc.createElementNS(\"http://www.w3.org/2000/svg\", \"path\");\n element.setAttribute(\"d\", d);\n return {\n element,\n length: element.getTotalLength()\n };\n });\n}\n\n\n//# sourceURL=webpack://@tsparticles/path-svg/./dist/browser/createSVGPaths.js?\n}");
37
+
38
+ /***/ },
39
+
40
+ /***/ "./dist/browser/loadSVGFromString.js"
41
+ /*!*******************************************!*\
42
+ !*** ./dist/browser/loadSVGFromString.js ***!
43
+ \*******************************************/
44
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
45
+
46
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ loadSVGFromString: () => (/* binding */ loadSVGFromString)\n/* harmony export */ });\nfunction loadSVGFromString(svgText) {\n const parser = new DOMParser(), doc = parser.parseFromString(svgText, \"image/svg+xml\"), svg = doc.querySelector(\"svg\");\n if (!svg) {\n return {\n paths: [],\n size: {\n width: 0,\n height: 0\n }\n };\n }\n const pathElements = svg.querySelectorAll(\"path\"), paths = [];\n for (const node of pathElements){\n if (!(node instanceof SVGPathElement)) {\n continue;\n }\n paths.push({\n element: node,\n length: node.getTotalLength()\n });\n }\n const size = {\n width: Number.parseFloat(svg.getAttribute(\"width\") ?? \"0\"),\n height: Number.parseFloat(svg.getAttribute(\"height\") ?? \"0\")\n };\n return {\n paths,\n size\n };\n}\n\n\n//# sourceURL=webpack://@tsparticles/path-svg/./dist/browser/loadSVGFromString.js?\n}");
27
47
 
28
48
  /***/ }
29
49
 
@@ -1,4 +1,6 @@
1
- import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue, safeDocument, } from "@tsparticles/engine";
1
+ import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue, } from "@tsparticles/engine";
2
+ import { createSVGPaths } from "./createSVGPaths.js";
3
+ import { loadSVGFromString } from "./loadSVGFromString.js";
2
4
  var SVGPathDirection;
3
5
  (function (SVGPathDirection) {
4
6
  SVGPathDirection[SVGPathDirection["normal"] = 0] = "normal";
@@ -6,7 +8,15 @@ var SVGPathDirection;
6
8
  })(SVGPathDirection || (SVGPathDirection = {}));
7
9
  const defaultSpeed = 1, minStep = 0, minIndex = 0, minWidth = 0, minScale = 1;
8
10
  export class SVGPathGenerator {
9
- constructor() {
11
+ _container;
12
+ _offset;
13
+ _paths;
14
+ _reverse;
15
+ _scale;
16
+ _size;
17
+ _width;
18
+ constructor(container) {
19
+ this._container = container;
10
20
  this._paths = [];
11
21
  this._reverse = false;
12
22
  this._size = { width: 0, height: 0 };
@@ -15,7 +25,7 @@ export class SVGPathGenerator {
15
25
  this._width = 0;
16
26
  }
17
27
  generate(particle, delta) {
18
- const container = particle.container, pxRatio = container.retina.pixelRatio;
28
+ const container = this._container, pxRatio = container.retina.pixelRatio;
19
29
  particle.svgDirection ??= getRandom() > half ? SVGPathDirection.normal : SVGPathDirection.reverse;
20
30
  particle.svgPathIndex ??= Math.floor(getRandom() * this._paths.length);
21
31
  particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * half).length;
@@ -63,21 +73,15 @@ export class SVGPathGenerator {
63
73
  }
64
74
  }
65
75
  path = this._paths[particle.svgPathIndex];
66
- const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = particle.container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
76
+ const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
67
77
  particle.position.x =
68
- (pos.x - this._size.width * half) * scale +
69
- particle.svgInitialPosition.x +
70
- offset.x +
71
- particle.svgOffset.width;
78
+ (pos.x - this._size.width * half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
72
79
  particle.position.y =
73
- (pos.y - this._size.height * half) * scale +
74
- particle.svgInitialPosition.y +
75
- offset.y +
76
- particle.svgOffset.height;
80
+ (pos.y - this._size.height * half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
77
81
  return Vector.origin;
78
82
  }
79
- init(container) {
80
- const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
83
+ init() {
84
+ const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
81
85
  this._reverse = options.reverse ?? this._reverse;
82
86
  this._scale = options.scale ?? minScale;
83
87
  this._offset.x = position.x;
@@ -87,37 +91,15 @@ export class SVGPathGenerator {
87
91
  if (options.url && !options.path) {
88
92
  const url = options.url;
89
93
  void (async () => {
90
- const response = await fetch(url), data = await response.text();
91
- const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), firstIndex = 0, svg = doc.getElementsByTagName("svg")[firstIndex];
92
- let svgPaths = svg.getElementsByTagName("path");
93
- if (!svgPaths.length) {
94
- svgPaths = doc.getElementsByTagName("path");
95
- }
96
- this._paths = [];
97
- for (let i = 0; i < svgPaths.length; i++) {
98
- const path = svgPaths.item(i);
99
- if (path) {
100
- this._paths.push({
101
- element: path,
102
- length: path.getTotalLength(),
103
- });
104
- }
105
- }
106
- this._size.height = Number.parseFloat(svg.getAttribute("height") ?? "0");
107
- this._size.width = Number.parseFloat(svg.getAttribute("width") ?? "0");
94
+ const response = await fetch(url), data = await response.text(), { paths, size } = loadSVGFromString(data);
95
+ this._paths = paths;
96
+ this._size.width = size.width;
97
+ this._size.height = size.height;
108
98
  })();
109
99
  }
110
100
  else if (options.path) {
111
101
  const path = options.path;
112
- this._paths = [];
113
- for (const item of path.data) {
114
- const element = safeDocument().createElementNS("http://www.w3.org/2000/svg", "path");
115
- element.setAttribute("d", item);
116
- this._paths.push({
117
- element,
118
- length: element.getTotalLength(),
119
- });
120
- }
102
+ this._paths = createSVGPaths(options.path.data);
121
103
  this._size.height = path.size.height;
122
104
  this._size.width = path.size.width;
123
105
  }
@@ -0,0 +1,12 @@
1
+ import { safeDocument } from "@tsparticles/engine";
2
+ export function createSVGPaths(data) {
3
+ const doc = safeDocument();
4
+ return data.map(d => {
5
+ const element = doc.createElementNS("http://www.w3.org/2000/svg", "path");
6
+ element.setAttribute("d", d);
7
+ return {
8
+ element,
9
+ length: element.getTotalLength(),
10
+ };
11
+ });
12
+ }
package/esm/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  export const svgPathName = "svgPathGenerator";
2
- export function loadSVGPath(engine) {
3
- engine.checkVersion("4.0.0-alpha.2");
4
- engine.register(async (e) => {
5
- const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
6
- e.addPathGenerator(svgPathName, new SVGPathGenerator());
2
+ export async function loadSVGPath(engine) {
3
+ engine.checkVersion("4.0.0-alpha.21");
4
+ await engine.register(e => {
5
+ e.addPathGenerator(svgPathName, async (container) => {
6
+ const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
7
+ return new SVGPathGenerator(container);
8
+ });
7
9
  });
8
10
  }
@@ -0,0 +1,24 @@
1
+ export function loadSVGFromString(svgText) {
2
+ const parser = new DOMParser(), doc = parser.parseFromString(svgText, "image/svg+xml"), svg = doc.querySelector("svg");
3
+ if (!svg) {
4
+ return { paths: [], size: { width: 0, height: 0 } };
5
+ }
6
+ const pathElements = svg.querySelectorAll("path"), paths = [];
7
+ for (const node of pathElements) {
8
+ if (!(node instanceof SVGPathElement)) {
9
+ continue;
10
+ }
11
+ paths.push({
12
+ element: node,
13
+ length: node.getTotalLength(),
14
+ });
15
+ }
16
+ const size = {
17
+ width: Number.parseFloat(svg.getAttribute("width") ?? "0"),
18
+ height: Number.parseFloat(svg.getAttribute("height") ?? "0"),
19
+ };
20
+ return {
21
+ paths,
22
+ size,
23
+ };
24
+ }
package/esm/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/path-svg",
3
- "version": "4.0.0-alpha.2",
3
+ "version": "4.0.0-alpha.21",
4
4
  "description": "tsParticles svg path",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -101,7 +101,7 @@
101
101
  "./package.json": "./package.json"
102
102
  },
103
103
  "dependencies": {
104
- "@tsparticles/engine": "4.0.0-alpha.2"
104
+ "@tsparticles/engine": "4.0.0-alpha.21"
105
105
  },
106
106
  "publishConfig": {
107
107
  "access": "public"