@tsparticles/path-svg 4.0.0-alpha.5 → 4.0.0-beta.0
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/266.min.js +1 -0
- package/browser/SVGPathGenerator.js +28 -42
- package/browser/createSVGPaths.js +12 -0
- package/browser/index.js +7 -3
- package/browser/loadSVGFromString.js +24 -0
- package/browser/types.js +1 -0
- package/cjs/SVGPathGenerator.js +28 -42
- package/cjs/createSVGPaths.js +12 -0
- package/cjs/index.js +7 -3
- package/cjs/loadSVGFromString.js +24 -0
- package/cjs/types.js +1 -0
- package/dist_browser_SVGPathGenerator_js.js +22 -2
- package/esm/SVGPathGenerator.js +28 -42
- package/esm/createSVGPaths.js +12 -0
- package/esm/index.js +7 -3
- package/esm/loadSVGFromString.js +24 -0
- package/esm/types.js +1 -0
- package/package.json +3 -2
- package/report.html +3 -3
- package/tsparticles.path.svg.js +75 -23
- package/tsparticles.path.svg.min.js +2 -2
- package/types/SVGPathGenerator.d.ts +6 -3
- package/types/createSVGPaths.d.ts +2 -0
- package/types/loadSVGFromString.d.ts +9 -0
- package/types/types.d.ts +4 -0
- package/umd/SVGPathGenerator.js +28 -42
- package/umd/createSVGPaths.js +25 -0
- package/umd/index.js +7 -3
- package/umd/loadSVGFromString.js +37 -0
- package/umd/types.js +12 -0
- package/376.min.js +0 -2
- package/376.min.js.LICENSE.txt +0 -1
- package/tsparticles.path.svg.min.js.LICENSE.txt +0 -1
package/266.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";(this.webpackChunk_tsparticles_path_svg=this.webpackChunk_tsparticles_path_svg||[]).push([[266],{266(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;_res;_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,this._res=a.Vector.origin}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,this._res.x=0,this._res.y=0,this._res}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,
|
|
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,16 +8,26 @@ 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
|
-
|
|
11
|
+
_container;
|
|
12
|
+
_offset;
|
|
13
|
+
_paths;
|
|
14
|
+
_res;
|
|
15
|
+
_reverse;
|
|
16
|
+
_scale;
|
|
17
|
+
_size;
|
|
18
|
+
_width;
|
|
19
|
+
constructor(container) {
|
|
20
|
+
this._container = container;
|
|
10
21
|
this._paths = [];
|
|
11
22
|
this._reverse = false;
|
|
12
23
|
this._size = { width: 0, height: 0 };
|
|
13
24
|
this._scale = 1;
|
|
14
25
|
this._offset = { x: 0, y: 0, mode: PixelMode.percent };
|
|
15
26
|
this._width = 0;
|
|
27
|
+
this._res = Vector.origin;
|
|
16
28
|
}
|
|
17
29
|
generate(particle, delta) {
|
|
18
|
-
const container =
|
|
30
|
+
const container = this._container, pxRatio = container.retina.pixelRatio;
|
|
19
31
|
particle.svgDirection ??= getRandom() > half ? SVGPathDirection.normal : SVGPathDirection.reverse;
|
|
20
32
|
particle.svgPathIndex ??= Math.floor(getRandom() * this._paths.length);
|
|
21
33
|
particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * half).length;
|
|
@@ -63,21 +75,17 @@ export class SVGPathGenerator {
|
|
|
63
75
|
}
|
|
64
76
|
}
|
|
65
77
|
path = this._paths[particle.svgPathIndex];
|
|
66
|
-
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize =
|
|
78
|
+
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
|
|
67
79
|
particle.position.x =
|
|
68
|
-
(pos.x - this._size.width * half) * scale +
|
|
69
|
-
particle.svgInitialPosition.x +
|
|
70
|
-
offset.x +
|
|
71
|
-
particle.svgOffset.width;
|
|
80
|
+
(pos.x - this._size.width * half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
|
|
72
81
|
particle.position.y =
|
|
73
|
-
(pos.y - this._size.height * half) * scale +
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return Vector.origin;
|
|
82
|
+
(pos.y - this._size.height * half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
|
|
83
|
+
this._res.x = 0;
|
|
84
|
+
this._res.y = 0;
|
|
85
|
+
return this._res;
|
|
78
86
|
}
|
|
79
|
-
init(
|
|
80
|
-
const options =
|
|
87
|
+
init() {
|
|
88
|
+
const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
81
89
|
this._reverse = options.reverse ?? this._reverse;
|
|
82
90
|
this._scale = options.scale ?? minScale;
|
|
83
91
|
this._offset.x = position.x;
|
|
@@ -87,37 +95,15 @@ export class SVGPathGenerator {
|
|
|
87
95
|
if (options.url && !options.path) {
|
|
88
96
|
const url = options.url;
|
|
89
97
|
void (async () => {
|
|
90
|
-
const response = await fetch(url), data = await response.text();
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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");
|
|
98
|
+
const response = await fetch(url), data = await response.text(), { paths, size } = loadSVGFromString(data);
|
|
99
|
+
this._paths = paths;
|
|
100
|
+
this._size.width = size.width;
|
|
101
|
+
this._size.height = size.height;
|
|
108
102
|
})();
|
|
109
103
|
}
|
|
110
104
|
else if (options.path) {
|
|
111
105
|
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
|
-
}
|
|
106
|
+
this._paths = createSVGPaths(options.path.data);
|
|
121
107
|
this._size.height = path.size.height;
|
|
122
108
|
this._size.width = path.size.width;
|
|
123
109
|
}
|
|
@@ -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,12 @@
|
|
|
1
1
|
export const svgPathName = "svgPathGenerator";
|
|
2
2
|
export async function loadSVGPath(engine) {
|
|
3
|
-
engine.checkVersion("4.0.0-
|
|
3
|
+
engine.checkVersion("4.0.0-beta.0");
|
|
4
4
|
await engine.register(async (e) => {
|
|
5
|
-
const {
|
|
6
|
-
e
|
|
5
|
+
const { ensureBaseMoverLoaded } = await import("@tsparticles/plugin-move");
|
|
6
|
+
ensureBaseMoverLoaded(e);
|
|
7
|
+
e.addPathGenerator?.(svgPathName, async (container) => {
|
|
8
|
+
const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
|
|
9
|
+
return new SVGPathGenerator(container);
|
|
10
|
+
});
|
|
7
11
|
});
|
|
8
12
|
}
|
|
@@ -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/browser/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/cjs/SVGPathGenerator.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue,
|
|
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,16 +8,26 @@ 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
|
-
|
|
11
|
+
_container;
|
|
12
|
+
_offset;
|
|
13
|
+
_paths;
|
|
14
|
+
_res;
|
|
15
|
+
_reverse;
|
|
16
|
+
_scale;
|
|
17
|
+
_size;
|
|
18
|
+
_width;
|
|
19
|
+
constructor(container) {
|
|
20
|
+
this._container = container;
|
|
10
21
|
this._paths = [];
|
|
11
22
|
this._reverse = false;
|
|
12
23
|
this._size = { width: 0, height: 0 };
|
|
13
24
|
this._scale = 1;
|
|
14
25
|
this._offset = { x: 0, y: 0, mode: PixelMode.percent };
|
|
15
26
|
this._width = 0;
|
|
27
|
+
this._res = Vector.origin;
|
|
16
28
|
}
|
|
17
29
|
generate(particle, delta) {
|
|
18
|
-
const container =
|
|
30
|
+
const container = this._container, pxRatio = container.retina.pixelRatio;
|
|
19
31
|
particle.svgDirection ??= getRandom() > half ? SVGPathDirection.normal : SVGPathDirection.reverse;
|
|
20
32
|
particle.svgPathIndex ??= Math.floor(getRandom() * this._paths.length);
|
|
21
33
|
particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * half).length;
|
|
@@ -63,21 +75,17 @@ export class SVGPathGenerator {
|
|
|
63
75
|
}
|
|
64
76
|
}
|
|
65
77
|
path = this._paths[particle.svgPathIndex];
|
|
66
|
-
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize =
|
|
78
|
+
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
|
|
67
79
|
particle.position.x =
|
|
68
|
-
(pos.x - this._size.width * half) * scale +
|
|
69
|
-
particle.svgInitialPosition.x +
|
|
70
|
-
offset.x +
|
|
71
|
-
particle.svgOffset.width;
|
|
80
|
+
(pos.x - this._size.width * half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
|
|
72
81
|
particle.position.y =
|
|
73
|
-
(pos.y - this._size.height * half) * scale +
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return Vector.origin;
|
|
82
|
+
(pos.y - this._size.height * half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
|
|
83
|
+
this._res.x = 0;
|
|
84
|
+
this._res.y = 0;
|
|
85
|
+
return this._res;
|
|
78
86
|
}
|
|
79
|
-
init(
|
|
80
|
-
const options =
|
|
87
|
+
init() {
|
|
88
|
+
const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
81
89
|
this._reverse = options.reverse ?? this._reverse;
|
|
82
90
|
this._scale = options.scale ?? minScale;
|
|
83
91
|
this._offset.x = position.x;
|
|
@@ -87,37 +95,15 @@ export class SVGPathGenerator {
|
|
|
87
95
|
if (options.url && !options.path) {
|
|
88
96
|
const url = options.url;
|
|
89
97
|
void (async () => {
|
|
90
|
-
const response = await fetch(url), data = await response.text();
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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");
|
|
98
|
+
const response = await fetch(url), data = await response.text(), { paths, size } = loadSVGFromString(data);
|
|
99
|
+
this._paths = paths;
|
|
100
|
+
this._size.width = size.width;
|
|
101
|
+
this._size.height = size.height;
|
|
108
102
|
})();
|
|
109
103
|
}
|
|
110
104
|
else if (options.path) {
|
|
111
105
|
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
|
-
}
|
|
106
|
+
this._paths = createSVGPaths(options.path.data);
|
|
121
107
|
this._size.height = path.size.height;
|
|
122
108
|
this._size.width = path.size.width;
|
|
123
109
|
}
|
|
@@ -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,12 @@
|
|
|
1
1
|
export const svgPathName = "svgPathGenerator";
|
|
2
2
|
export async function loadSVGPath(engine) {
|
|
3
|
-
engine.checkVersion("4.0.0-
|
|
3
|
+
engine.checkVersion("4.0.0-beta.0");
|
|
4
4
|
await engine.register(async (e) => {
|
|
5
|
-
const {
|
|
6
|
-
e
|
|
5
|
+
const { ensureBaseMoverLoaded } = await import("@tsparticles/plugin-move");
|
|
6
|
+
ensureBaseMoverLoaded(e);
|
|
7
|
+
e.addPathGenerator?.(svgPathName, async (container) => {
|
|
8
|
+
const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
|
|
9
|
+
return new SVGPathGenerator(container);
|
|
10
|
+
});
|
|
7
11
|
});
|
|
8
12
|
}
|
|
@@ -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-
|
|
7
|
+
* v4.0.0-beta.0
|
|
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
|
|
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 _res;\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 this._res = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.Vector.origin;\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 this._res.x = 0;\n this._res.y = 0;\n return this._res;\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
|
|
package/esm/SVGPathGenerator.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { PixelMode, Vector, getPosition, getRandom, half, randomInRangeValue,
|
|
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,16 +8,26 @@ 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
|
-
|
|
11
|
+
_container;
|
|
12
|
+
_offset;
|
|
13
|
+
_paths;
|
|
14
|
+
_res;
|
|
15
|
+
_reverse;
|
|
16
|
+
_scale;
|
|
17
|
+
_size;
|
|
18
|
+
_width;
|
|
19
|
+
constructor(container) {
|
|
20
|
+
this._container = container;
|
|
10
21
|
this._paths = [];
|
|
11
22
|
this._reverse = false;
|
|
12
23
|
this._size = { width: 0, height: 0 };
|
|
13
24
|
this._scale = 1;
|
|
14
25
|
this._offset = { x: 0, y: 0, mode: PixelMode.percent };
|
|
15
26
|
this._width = 0;
|
|
27
|
+
this._res = Vector.origin;
|
|
16
28
|
}
|
|
17
29
|
generate(particle, delta) {
|
|
18
|
-
const container =
|
|
30
|
+
const container = this._container, pxRatio = container.retina.pixelRatio;
|
|
19
31
|
particle.svgDirection ??= getRandom() > half ? SVGPathDirection.normal : SVGPathDirection.reverse;
|
|
20
32
|
particle.svgPathIndex ??= Math.floor(getRandom() * this._paths.length);
|
|
21
33
|
particle.svgSpeed ??= particle.velocity.mult((particle.retina.moveSpeed ?? defaultSpeed) * half).length;
|
|
@@ -63,21 +75,17 @@ export class SVGPathGenerator {
|
|
|
63
75
|
}
|
|
64
76
|
}
|
|
65
77
|
path = this._paths[particle.svgPathIndex];
|
|
66
|
-
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize =
|
|
78
|
+
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = this._container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
|
|
67
79
|
particle.position.x =
|
|
68
|
-
(pos.x - this._size.width * half) * scale +
|
|
69
|
-
particle.svgInitialPosition.x +
|
|
70
|
-
offset.x +
|
|
71
|
-
particle.svgOffset.width;
|
|
80
|
+
(pos.x - this._size.width * half) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
|
|
72
81
|
particle.position.y =
|
|
73
|
-
(pos.y - this._size.height * half) * scale +
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return Vector.origin;
|
|
82
|
+
(pos.y - this._size.height * half) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
|
|
83
|
+
this._res.x = 0;
|
|
84
|
+
this._res.y = 0;
|
|
85
|
+
return this._res;
|
|
78
86
|
}
|
|
79
|
-
init(
|
|
80
|
-
const options =
|
|
87
|
+
init() {
|
|
88
|
+
const options = this._container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
81
89
|
this._reverse = options.reverse ?? this._reverse;
|
|
82
90
|
this._scale = options.scale ?? minScale;
|
|
83
91
|
this._offset.x = position.x;
|
|
@@ -87,37 +95,15 @@ export class SVGPathGenerator {
|
|
|
87
95
|
if (options.url && !options.path) {
|
|
88
96
|
const url = options.url;
|
|
89
97
|
void (async () => {
|
|
90
|
-
const response = await fetch(url), data = await response.text();
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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");
|
|
98
|
+
const response = await fetch(url), data = await response.text(), { paths, size } = loadSVGFromString(data);
|
|
99
|
+
this._paths = paths;
|
|
100
|
+
this._size.width = size.width;
|
|
101
|
+
this._size.height = size.height;
|
|
108
102
|
})();
|
|
109
103
|
}
|
|
110
104
|
else if (options.path) {
|
|
111
105
|
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
|
-
}
|
|
106
|
+
this._paths = createSVGPaths(options.path.data);
|
|
121
107
|
this._size.height = path.size.height;
|
|
122
108
|
this._size.width = path.size.width;
|
|
123
109
|
}
|
|
@@ -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,12 @@
|
|
|
1
1
|
export const svgPathName = "svgPathGenerator";
|
|
2
2
|
export async function loadSVGPath(engine) {
|
|
3
|
-
engine.checkVersion("4.0.0-
|
|
3
|
+
engine.checkVersion("4.0.0-beta.0");
|
|
4
4
|
await engine.register(async (e) => {
|
|
5
|
-
const {
|
|
6
|
-
e
|
|
5
|
+
const { ensureBaseMoverLoaded } = await import("@tsparticles/plugin-move");
|
|
6
|
+
ensureBaseMoverLoaded(e);
|
|
7
|
+
e.addPathGenerator?.(svgPathName, async (container) => {
|
|
8
|
+
const { SVGPathGenerator } = await import("./SVGPathGenerator.js");
|
|
9
|
+
return new SVGPathGenerator(container);
|
|
10
|
+
});
|
|
7
11
|
});
|
|
8
12
|
}
|
|
@@ -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-
|
|
3
|
+
"version": "4.0.0-beta.0",
|
|
4
4
|
"description": "tsParticles svg path",
|
|
5
5
|
"homepage": "https://particles.js.org",
|
|
6
6
|
"repository": {
|
|
@@ -101,7 +101,8 @@
|
|
|
101
101
|
"./package.json": "./package.json"
|
|
102
102
|
},
|
|
103
103
|
"dependencies": {
|
|
104
|
-
"@tsparticles/engine": "4.0.0-
|
|
104
|
+
"@tsparticles/engine": "4.0.0-beta.0",
|
|
105
|
+
"@tsparticles/plugin-move": "4.0.0-beta.0"
|
|
105
106
|
},
|
|
106
107
|
"publishConfig": {
|
|
107
108
|
"access": "public"
|