@tsparticles/path-svg 3.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/LICENSE +21 -0
- package/README.md +74 -0
- package/browser/SVGPathGenerator.js +139 -0
- package/browser/index.js +5 -0
- package/cjs/SVGPathGenerator.js +143 -0
- package/cjs/index.js +9 -0
- package/esm/SVGPathGenerator.js +139 -0
- package/esm/index.js +5 -0
- package/package.json +97 -0
- package/report.html +39 -0
- package/tsparticles.path.svg.js +263 -0
- package/tsparticles.path.svg.min.js +2 -0
- package/tsparticles.path.svg.min.js.LICENSE.txt +1 -0
- package/types/SVGPathGenerator.d.ts +32 -0
- package/types/index.d.ts +3 -0
- package/umd/SVGPathGenerator.js +153 -0
- package/umd/index.js +19 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Matteo Bruni
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[](https://particles.js.org)
|
|
2
|
+
|
|
3
|
+
# tsParticles SVG Path
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/@tsparticles/path-svg)
|
|
6
|
+
[](https://www.npmjs.com/package/@tsparticles/path-svg)
|
|
7
|
+
[](https://www.npmjs.com/package/@tsparticles/path-svg) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/matteobruni/tsparticles) path plugin for on svg path movement.
|
|
10
|
+
|
|
11
|
+
## How to use it
|
|
12
|
+
|
|
13
|
+
### CDN / Vanilla JS / jQuery
|
|
14
|
+
|
|
15
|
+
The CDN/Vanilla version JS has one required file in vanilla configuration:
|
|
16
|
+
|
|
17
|
+
Including the `tsparticles.path.svg.min.js` file will export the function to load the path plugin:
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
loadSVGPath
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Usage
|
|
24
|
+
|
|
25
|
+
Once the scripts are loaded you can set up `tsParticles` and the path plugin like this:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
(async () => {
|
|
29
|
+
await loadSVGPath(tsParticles);
|
|
30
|
+
|
|
31
|
+
await tsParticles.load({
|
|
32
|
+
id: "tsparticles",
|
|
33
|
+
options: {
|
|
34
|
+
/* options */
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
})();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### ESM / CommonJS
|
|
41
|
+
|
|
42
|
+
This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
|
|
43
|
+
|
|
44
|
+
```shell
|
|
45
|
+
$ npm install @tsparticles/path-svg
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
or
|
|
49
|
+
|
|
50
|
+
```shell
|
|
51
|
+
$ yarn add @tsparticles/path-svg
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then you need to import it in the app, like this:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
const { tsParticles } = require("@tsparticles/engine");
|
|
58
|
+
const { loadSVGPath } = require("@tsparticles/path-svg");
|
|
59
|
+
|
|
60
|
+
(async () => {
|
|
61
|
+
await loadSVGPath(tsParticles);
|
|
62
|
+
})();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
or
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
69
|
+
import { loadSVGPath } from "@tsparticles/path-svg";
|
|
70
|
+
|
|
71
|
+
(async () => {
|
|
72
|
+
await loadSVGPath(tsParticles);
|
|
73
|
+
})();
|
|
74
|
+
```
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Vector, getPosition, getRandom, randomInRange, } from "@tsparticles/engine";
|
|
2
|
+
export class SVGPathGenerator {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._paths = [];
|
|
5
|
+
this._reverse = false;
|
|
6
|
+
this._size = { width: 0, height: 0 };
|
|
7
|
+
this._scale = 1;
|
|
8
|
+
this._offset = { x: 0, y: 0, mode: "percent" };
|
|
9
|
+
this._width = 0;
|
|
10
|
+
}
|
|
11
|
+
generate(particle, delta) {
|
|
12
|
+
const container = particle.container, pxRatio = container.retina.pixelRatio;
|
|
13
|
+
if (particle.svgDirection === undefined) {
|
|
14
|
+
particle.svgDirection = getRandom() > 0.5 ? 0 : 1;
|
|
15
|
+
}
|
|
16
|
+
if (particle.svgPathIndex === undefined) {
|
|
17
|
+
particle.svgPathIndex = Math.floor(Math.random() * this._paths.length);
|
|
18
|
+
}
|
|
19
|
+
if (particle.svgSpeed === undefined) {
|
|
20
|
+
particle.svgSpeed = particle.velocity.mult((particle.retina.moveSpeed ?? 1) / 2).length;
|
|
21
|
+
}
|
|
22
|
+
if (particle.svgStep === undefined) {
|
|
23
|
+
particle.svgStep = randomInRange({ min: 0, max: this._paths[particle.svgPathIndex].length }) * pxRatio;
|
|
24
|
+
}
|
|
25
|
+
if (particle.svgOffset === undefined) {
|
|
26
|
+
particle.svgOffset = {
|
|
27
|
+
width: randomInRange({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
28
|
+
height: randomInRange({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (particle.svgInitialPosition === undefined) {
|
|
32
|
+
particle.svgInitialPosition = { ...particle.position };
|
|
33
|
+
}
|
|
34
|
+
particle.velocity.x = 0;
|
|
35
|
+
particle.velocity.y = 0;
|
|
36
|
+
if (particle.svgDirection === 0) {
|
|
37
|
+
particle.svgStep += particle.svgSpeed * delta.factor;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
particle.svgStep -= particle.svgSpeed * delta.factor;
|
|
41
|
+
}
|
|
42
|
+
let path = this._paths[particle.svgPathIndex];
|
|
43
|
+
if (path) {
|
|
44
|
+
const pathLength = path.length;
|
|
45
|
+
if (particle.svgStep >= pathLength) {
|
|
46
|
+
particle.svgPathIndex = particle.svgPathIndex + 1;
|
|
47
|
+
if (particle.svgPathIndex >= this._paths.length) {
|
|
48
|
+
if (this._reverse) {
|
|
49
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
50
|
+
particle.svgDirection = 1;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
particle.svgPathIndex = 0;
|
|
54
|
+
particle.svgStep = 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (particle.svgStep <= 0) {
|
|
59
|
+
particle.svgPathIndex = particle.svgPathIndex - 1;
|
|
60
|
+
if (particle.svgPathIndex < 0) {
|
|
61
|
+
if (this._reverse) {
|
|
62
|
+
particle.svgPathIndex = 0;
|
|
63
|
+
particle.svgDirection = 0;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
67
|
+
path = this._paths[particle.svgPathIndex];
|
|
68
|
+
particle.svgStep = path.length;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
path = this._paths[particle.svgPathIndex];
|
|
73
|
+
}
|
|
74
|
+
if (path) {
|
|
75
|
+
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = particle.container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
|
|
76
|
+
particle.position.x =
|
|
77
|
+
(pos.x - this._size.width / 2) * scale +
|
|
78
|
+
particle.svgInitialPosition.x +
|
|
79
|
+
offset.x +
|
|
80
|
+
particle.svgOffset.width;
|
|
81
|
+
particle.position.y =
|
|
82
|
+
(pos.y - this._size.height / 2) * scale +
|
|
83
|
+
particle.svgInitialPosition.y +
|
|
84
|
+
offset.y +
|
|
85
|
+
particle.svgOffset.height;
|
|
86
|
+
}
|
|
87
|
+
return Vector.origin;
|
|
88
|
+
}
|
|
89
|
+
init(container) {
|
|
90
|
+
const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
91
|
+
this._reverse = options.reverse ?? this._reverse;
|
|
92
|
+
this._scale = options.scale ?? 1;
|
|
93
|
+
this._offset.x = position.x;
|
|
94
|
+
this._offset.y = position.y;
|
|
95
|
+
this._offset.mode = position.mode;
|
|
96
|
+
this._width = options.width ?? 0;
|
|
97
|
+
if (options.url && !options.path) {
|
|
98
|
+
const url = options.url;
|
|
99
|
+
(async () => {
|
|
100
|
+
const response = await fetch(url), data = await response.text();
|
|
101
|
+
const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), svg = doc.getElementsByTagName("svg")[0];
|
|
102
|
+
let svgPaths = svg.getElementsByTagName("path");
|
|
103
|
+
if (!svgPaths.length) {
|
|
104
|
+
svgPaths = doc.getElementsByTagName("path");
|
|
105
|
+
}
|
|
106
|
+
this._paths = [];
|
|
107
|
+
for (let i = 0; i < svgPaths.length; i++) {
|
|
108
|
+
const path = svgPaths.item(i);
|
|
109
|
+
if (path) {
|
|
110
|
+
this._paths.push({
|
|
111
|
+
element: path,
|
|
112
|
+
length: path.getTotalLength(),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
this._size.height = parseFloat(svg.getAttribute("height") ?? "0");
|
|
117
|
+
this._size.width = parseFloat(svg.getAttribute("width") ?? "0");
|
|
118
|
+
})();
|
|
119
|
+
}
|
|
120
|
+
else if (options.path) {
|
|
121
|
+
const path = options.path;
|
|
122
|
+
this._paths = [];
|
|
123
|
+
for (const item of path.data) {
|
|
124
|
+
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
125
|
+
element.setAttribute("d", item);
|
|
126
|
+
this._paths.push({
|
|
127
|
+
element,
|
|
128
|
+
length: element.getTotalLength(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
this._size.height = path.size.height;
|
|
132
|
+
this._size.width = path.size.width;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
reset() {
|
|
136
|
+
}
|
|
137
|
+
update() {
|
|
138
|
+
}
|
|
139
|
+
}
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SVGPathGenerator = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
class SVGPathGenerator {
|
|
6
|
+
constructor() {
|
|
7
|
+
this._paths = [];
|
|
8
|
+
this._reverse = false;
|
|
9
|
+
this._size = { width: 0, height: 0 };
|
|
10
|
+
this._scale = 1;
|
|
11
|
+
this._offset = { x: 0, y: 0, mode: "percent" };
|
|
12
|
+
this._width = 0;
|
|
13
|
+
}
|
|
14
|
+
generate(particle, delta) {
|
|
15
|
+
const container = particle.container, pxRatio = container.retina.pixelRatio;
|
|
16
|
+
if (particle.svgDirection === undefined) {
|
|
17
|
+
particle.svgDirection = (0, engine_1.getRandom)() > 0.5 ? 0 : 1;
|
|
18
|
+
}
|
|
19
|
+
if (particle.svgPathIndex === undefined) {
|
|
20
|
+
particle.svgPathIndex = Math.floor(Math.random() * this._paths.length);
|
|
21
|
+
}
|
|
22
|
+
if (particle.svgSpeed === undefined) {
|
|
23
|
+
particle.svgSpeed = particle.velocity.mult((particle.retina.moveSpeed ?? 1) / 2).length;
|
|
24
|
+
}
|
|
25
|
+
if (particle.svgStep === undefined) {
|
|
26
|
+
particle.svgStep = (0, engine_1.randomInRange)({ min: 0, max: this._paths[particle.svgPathIndex].length }) * pxRatio;
|
|
27
|
+
}
|
|
28
|
+
if (particle.svgOffset === undefined) {
|
|
29
|
+
particle.svgOffset = {
|
|
30
|
+
width: (0, engine_1.randomInRange)({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
31
|
+
height: (0, engine_1.randomInRange)({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (particle.svgInitialPosition === undefined) {
|
|
35
|
+
particle.svgInitialPosition = { ...particle.position };
|
|
36
|
+
}
|
|
37
|
+
particle.velocity.x = 0;
|
|
38
|
+
particle.velocity.y = 0;
|
|
39
|
+
if (particle.svgDirection === 0) {
|
|
40
|
+
particle.svgStep += particle.svgSpeed * delta.factor;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
particle.svgStep -= particle.svgSpeed * delta.factor;
|
|
44
|
+
}
|
|
45
|
+
let path = this._paths[particle.svgPathIndex];
|
|
46
|
+
if (path) {
|
|
47
|
+
const pathLength = path.length;
|
|
48
|
+
if (particle.svgStep >= pathLength) {
|
|
49
|
+
particle.svgPathIndex = particle.svgPathIndex + 1;
|
|
50
|
+
if (particle.svgPathIndex >= this._paths.length) {
|
|
51
|
+
if (this._reverse) {
|
|
52
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
53
|
+
particle.svgDirection = 1;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
particle.svgPathIndex = 0;
|
|
57
|
+
particle.svgStep = 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else if (particle.svgStep <= 0) {
|
|
62
|
+
particle.svgPathIndex = particle.svgPathIndex - 1;
|
|
63
|
+
if (particle.svgPathIndex < 0) {
|
|
64
|
+
if (this._reverse) {
|
|
65
|
+
particle.svgPathIndex = 0;
|
|
66
|
+
particle.svgDirection = 0;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
70
|
+
path = this._paths[particle.svgPathIndex];
|
|
71
|
+
particle.svgStep = path.length;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
path = this._paths[particle.svgPathIndex];
|
|
76
|
+
}
|
|
77
|
+
if (path) {
|
|
78
|
+
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = particle.container.canvas.size, offset = (0, engine_1.getPosition)(this._offset, canvasSize), scale = this._scale * pxRatio;
|
|
79
|
+
particle.position.x =
|
|
80
|
+
(pos.x - this._size.width / 2) * scale +
|
|
81
|
+
particle.svgInitialPosition.x +
|
|
82
|
+
offset.x +
|
|
83
|
+
particle.svgOffset.width;
|
|
84
|
+
particle.position.y =
|
|
85
|
+
(pos.y - this._size.height / 2) * scale +
|
|
86
|
+
particle.svgInitialPosition.y +
|
|
87
|
+
offset.y +
|
|
88
|
+
particle.svgOffset.height;
|
|
89
|
+
}
|
|
90
|
+
return engine_1.Vector.origin;
|
|
91
|
+
}
|
|
92
|
+
init(container) {
|
|
93
|
+
const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
94
|
+
this._reverse = options.reverse ?? this._reverse;
|
|
95
|
+
this._scale = options.scale ?? 1;
|
|
96
|
+
this._offset.x = position.x;
|
|
97
|
+
this._offset.y = position.y;
|
|
98
|
+
this._offset.mode = position.mode;
|
|
99
|
+
this._width = options.width ?? 0;
|
|
100
|
+
if (options.url && !options.path) {
|
|
101
|
+
const url = options.url;
|
|
102
|
+
(async () => {
|
|
103
|
+
const response = await fetch(url), data = await response.text();
|
|
104
|
+
const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), svg = doc.getElementsByTagName("svg")[0];
|
|
105
|
+
let svgPaths = svg.getElementsByTagName("path");
|
|
106
|
+
if (!svgPaths.length) {
|
|
107
|
+
svgPaths = doc.getElementsByTagName("path");
|
|
108
|
+
}
|
|
109
|
+
this._paths = [];
|
|
110
|
+
for (let i = 0; i < svgPaths.length; i++) {
|
|
111
|
+
const path = svgPaths.item(i);
|
|
112
|
+
if (path) {
|
|
113
|
+
this._paths.push({
|
|
114
|
+
element: path,
|
|
115
|
+
length: path.getTotalLength(),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
this._size.height = parseFloat(svg.getAttribute("height") ?? "0");
|
|
120
|
+
this._size.width = parseFloat(svg.getAttribute("width") ?? "0");
|
|
121
|
+
})();
|
|
122
|
+
}
|
|
123
|
+
else if (options.path) {
|
|
124
|
+
const path = options.path;
|
|
125
|
+
this._paths = [];
|
|
126
|
+
for (const item of path.data) {
|
|
127
|
+
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
128
|
+
element.setAttribute("d", item);
|
|
129
|
+
this._paths.push({
|
|
130
|
+
element,
|
|
131
|
+
length: element.getTotalLength(),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
this._size.height = path.size.height;
|
|
135
|
+
this._size.width = path.size.width;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
reset() {
|
|
139
|
+
}
|
|
140
|
+
update() {
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.SVGPathGenerator = SVGPathGenerator;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadSVGPath = exports.svgPathName = void 0;
|
|
4
|
+
const SVGPathGenerator_1 = require("./SVGPathGenerator");
|
|
5
|
+
exports.svgPathName = "svgPathGenerator";
|
|
6
|
+
async function loadSVGPath(engine, refresh = true) {
|
|
7
|
+
await engine.addPathGenerator(exports.svgPathName, new SVGPathGenerator_1.SVGPathGenerator(), refresh);
|
|
8
|
+
}
|
|
9
|
+
exports.loadSVGPath = loadSVGPath;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Vector, getPosition, getRandom, randomInRange, } from "@tsparticles/engine";
|
|
2
|
+
export class SVGPathGenerator {
|
|
3
|
+
constructor() {
|
|
4
|
+
this._paths = [];
|
|
5
|
+
this._reverse = false;
|
|
6
|
+
this._size = { width: 0, height: 0 };
|
|
7
|
+
this._scale = 1;
|
|
8
|
+
this._offset = { x: 0, y: 0, mode: "percent" };
|
|
9
|
+
this._width = 0;
|
|
10
|
+
}
|
|
11
|
+
generate(particle, delta) {
|
|
12
|
+
const container = particle.container, pxRatio = container.retina.pixelRatio;
|
|
13
|
+
if (particle.svgDirection === undefined) {
|
|
14
|
+
particle.svgDirection = getRandom() > 0.5 ? 0 : 1;
|
|
15
|
+
}
|
|
16
|
+
if (particle.svgPathIndex === undefined) {
|
|
17
|
+
particle.svgPathIndex = Math.floor(Math.random() * this._paths.length);
|
|
18
|
+
}
|
|
19
|
+
if (particle.svgSpeed === undefined) {
|
|
20
|
+
particle.svgSpeed = particle.velocity.mult((particle.retina.moveSpeed ?? 1) / 2).length;
|
|
21
|
+
}
|
|
22
|
+
if (particle.svgStep === undefined) {
|
|
23
|
+
particle.svgStep = randomInRange({ min: 0, max: this._paths[particle.svgPathIndex].length }) * pxRatio;
|
|
24
|
+
}
|
|
25
|
+
if (particle.svgOffset === undefined) {
|
|
26
|
+
particle.svgOffset = {
|
|
27
|
+
width: randomInRange({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
28
|
+
height: randomInRange({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (particle.svgInitialPosition === undefined) {
|
|
32
|
+
particle.svgInitialPosition = { ...particle.position };
|
|
33
|
+
}
|
|
34
|
+
particle.velocity.x = 0;
|
|
35
|
+
particle.velocity.y = 0;
|
|
36
|
+
if (particle.svgDirection === 0) {
|
|
37
|
+
particle.svgStep += particle.svgSpeed * delta.factor;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
particle.svgStep -= particle.svgSpeed * delta.factor;
|
|
41
|
+
}
|
|
42
|
+
let path = this._paths[particle.svgPathIndex];
|
|
43
|
+
if (path) {
|
|
44
|
+
const pathLength = path.length;
|
|
45
|
+
if (particle.svgStep >= pathLength) {
|
|
46
|
+
particle.svgPathIndex = particle.svgPathIndex + 1;
|
|
47
|
+
if (particle.svgPathIndex >= this._paths.length) {
|
|
48
|
+
if (this._reverse) {
|
|
49
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
50
|
+
particle.svgDirection = 1;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
particle.svgPathIndex = 0;
|
|
54
|
+
particle.svgStep = 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (particle.svgStep <= 0) {
|
|
59
|
+
particle.svgPathIndex = particle.svgPathIndex - 1;
|
|
60
|
+
if (particle.svgPathIndex < 0) {
|
|
61
|
+
if (this._reverse) {
|
|
62
|
+
particle.svgPathIndex = 0;
|
|
63
|
+
particle.svgDirection = 0;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
67
|
+
path = this._paths[particle.svgPathIndex];
|
|
68
|
+
particle.svgStep = path.length;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
path = this._paths[particle.svgPathIndex];
|
|
73
|
+
}
|
|
74
|
+
if (path) {
|
|
75
|
+
const pathElement = path.element, pos = pathElement.getPointAtLength(particle.svgStep), canvasSize = particle.container.canvas.size, offset = getPosition(this._offset, canvasSize), scale = this._scale * pxRatio;
|
|
76
|
+
particle.position.x =
|
|
77
|
+
(pos.x - this._size.width / 2) * scale +
|
|
78
|
+
particle.svgInitialPosition.x +
|
|
79
|
+
offset.x +
|
|
80
|
+
particle.svgOffset.width;
|
|
81
|
+
particle.position.y =
|
|
82
|
+
(pos.y - this._size.height / 2) * scale +
|
|
83
|
+
particle.svgInitialPosition.y +
|
|
84
|
+
offset.y +
|
|
85
|
+
particle.svgOffset.height;
|
|
86
|
+
}
|
|
87
|
+
return Vector.origin;
|
|
88
|
+
}
|
|
89
|
+
init(container) {
|
|
90
|
+
const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
91
|
+
this._reverse = options.reverse ?? this._reverse;
|
|
92
|
+
this._scale = options.scale ?? 1;
|
|
93
|
+
this._offset.x = position.x;
|
|
94
|
+
this._offset.y = position.y;
|
|
95
|
+
this._offset.mode = position.mode;
|
|
96
|
+
this._width = options.width ?? 0;
|
|
97
|
+
if (options.url && !options.path) {
|
|
98
|
+
const url = options.url;
|
|
99
|
+
(async () => {
|
|
100
|
+
const response = await fetch(url), data = await response.text();
|
|
101
|
+
const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), svg = doc.getElementsByTagName("svg")[0];
|
|
102
|
+
let svgPaths = svg.getElementsByTagName("path");
|
|
103
|
+
if (!svgPaths.length) {
|
|
104
|
+
svgPaths = doc.getElementsByTagName("path");
|
|
105
|
+
}
|
|
106
|
+
this._paths = [];
|
|
107
|
+
for (let i = 0; i < svgPaths.length; i++) {
|
|
108
|
+
const path = svgPaths.item(i);
|
|
109
|
+
if (path) {
|
|
110
|
+
this._paths.push({
|
|
111
|
+
element: path,
|
|
112
|
+
length: path.getTotalLength(),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
this._size.height = parseFloat(svg.getAttribute("height") ?? "0");
|
|
117
|
+
this._size.width = parseFloat(svg.getAttribute("width") ?? "0");
|
|
118
|
+
})();
|
|
119
|
+
}
|
|
120
|
+
else if (options.path) {
|
|
121
|
+
const path = options.path;
|
|
122
|
+
this._paths = [];
|
|
123
|
+
for (const item of path.data) {
|
|
124
|
+
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
125
|
+
element.setAttribute("d", item);
|
|
126
|
+
this._paths.push({
|
|
127
|
+
element,
|
|
128
|
+
length: element.getTotalLength(),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
this._size.height = path.size.height;
|
|
132
|
+
this._size.width = path.size.width;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
reset() {
|
|
136
|
+
}
|
|
137
|
+
update() {
|
|
138
|
+
}
|
|
139
|
+
}
|
package/esm/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsparticles/path-svg",
|
|
3
|
+
"version": "3.0.0-beta.0",
|
|
4
|
+
"description": "tsParticles svg path",
|
|
5
|
+
"homepage": "https://particles.js.org",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/matteobruni/tsparticles.git",
|
|
9
|
+
"directory": "paths/svg"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"front-end",
|
|
13
|
+
"frontend",
|
|
14
|
+
"tsparticles",
|
|
15
|
+
"particles.js",
|
|
16
|
+
"particlesjs",
|
|
17
|
+
"particles",
|
|
18
|
+
"particle",
|
|
19
|
+
"canvas",
|
|
20
|
+
"jsparticles",
|
|
21
|
+
"xparticles",
|
|
22
|
+
"particles-js",
|
|
23
|
+
"particles-bg",
|
|
24
|
+
"particles-bg-vue",
|
|
25
|
+
"particles-ts",
|
|
26
|
+
"particles.ts",
|
|
27
|
+
"react-particles-js",
|
|
28
|
+
"react-particles.js",
|
|
29
|
+
"react-particles",
|
|
30
|
+
"react",
|
|
31
|
+
"reactjs",
|
|
32
|
+
"vue-particles",
|
|
33
|
+
"ngx-particles",
|
|
34
|
+
"angular-particles",
|
|
35
|
+
"particleground",
|
|
36
|
+
"vue",
|
|
37
|
+
"vuejs",
|
|
38
|
+
"preact",
|
|
39
|
+
"preactjs",
|
|
40
|
+
"jquery",
|
|
41
|
+
"angularjs",
|
|
42
|
+
"angular",
|
|
43
|
+
"typescript",
|
|
44
|
+
"javascript",
|
|
45
|
+
"animation",
|
|
46
|
+
"web",
|
|
47
|
+
"html5",
|
|
48
|
+
"web-design",
|
|
49
|
+
"webdesign",
|
|
50
|
+
"css",
|
|
51
|
+
"html",
|
|
52
|
+
"css3",
|
|
53
|
+
"animated",
|
|
54
|
+
"background",
|
|
55
|
+
"confetti",
|
|
56
|
+
"canvas",
|
|
57
|
+
"fireworks",
|
|
58
|
+
"fireworks-js",
|
|
59
|
+
"confetti-js",
|
|
60
|
+
"confettijs",
|
|
61
|
+
"fireworksjs",
|
|
62
|
+
"canvas-confetti",
|
|
63
|
+
"@tsparticles/plugin",
|
|
64
|
+
"@tsparticles/path"
|
|
65
|
+
],
|
|
66
|
+
"author": "Matteo Bruni <matteo.bruni@me.com>",
|
|
67
|
+
"license": "MIT",
|
|
68
|
+
"bugs": {
|
|
69
|
+
"url": "https://github.com/matteobruni/tsparticles/issues"
|
|
70
|
+
},
|
|
71
|
+
"funding": [
|
|
72
|
+
{
|
|
73
|
+
"type": "github",
|
|
74
|
+
"url": "https://github.com/sponsors/matteobruni"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"type": "github",
|
|
78
|
+
"url": "https://github.com/sponsors/tsparticles"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"type": "buymeacoffee",
|
|
82
|
+
"url": "https://www.buymeacoffee.com/matteobruni"
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
"main": "cjs/index.js",
|
|
86
|
+
"jsdelivr": "tsparticles.path.svg.min.js",
|
|
87
|
+
"unpkg": "tsparticles.path.svg.min.js",
|
|
88
|
+
"module": "esm/index.js",
|
|
89
|
+
"types": "types/index.d.ts",
|
|
90
|
+
"sideEffects": false,
|
|
91
|
+
"dependencies": {
|
|
92
|
+
"@tsparticles/engine": "^3.0.0-beta.0"
|
|
93
|
+
},
|
|
94
|
+
"publishConfig": {
|
|
95
|
+
"access": "public"
|
|
96
|
+
}
|
|
97
|
+
}
|