@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
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Author : Matteo Bruni
|
|
3
|
+
* MIT license: https://opensource.org/licenses/MIT
|
|
4
|
+
* Demo / Generator : https://particles.js.org/
|
|
5
|
+
* GitHub : https://www.github.com/matteobruni/tsparticles
|
|
6
|
+
* How to use? : Check the GitHub README
|
|
7
|
+
* v3.0.0-beta.0
|
|
8
|
+
*/
|
|
9
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
10
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
11
|
+
module.exports = factory(require("@tsparticles/engine"));
|
|
12
|
+
else if(typeof define === 'function' && define.amd)
|
|
13
|
+
define(["@tsparticles/engine"], factory);
|
|
14
|
+
else {
|
|
15
|
+
var a = typeof exports === 'object' ? factory(require("@tsparticles/engine")) : factory(root["window"]);
|
|
16
|
+
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
|
|
17
|
+
}
|
|
18
|
+
})(this, (__WEBPACK_EXTERNAL_MODULE__533__) => {
|
|
19
|
+
return /******/ (() => { // webpackBootstrap
|
|
20
|
+
/******/ "use strict";
|
|
21
|
+
/******/ var __webpack_modules__ = ({
|
|
22
|
+
|
|
23
|
+
/***/ 533:
|
|
24
|
+
/***/ ((module) => {
|
|
25
|
+
|
|
26
|
+
module.exports = __WEBPACK_EXTERNAL_MODULE__533__;
|
|
27
|
+
|
|
28
|
+
/***/ })
|
|
29
|
+
|
|
30
|
+
/******/ });
|
|
31
|
+
/************************************************************************/
|
|
32
|
+
/******/ // The module cache
|
|
33
|
+
/******/ var __webpack_module_cache__ = {};
|
|
34
|
+
/******/
|
|
35
|
+
/******/ // The require function
|
|
36
|
+
/******/ function __webpack_require__(moduleId) {
|
|
37
|
+
/******/ // Check if module is in cache
|
|
38
|
+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
|
|
39
|
+
/******/ if (cachedModule !== undefined) {
|
|
40
|
+
/******/ return cachedModule.exports;
|
|
41
|
+
/******/ }
|
|
42
|
+
/******/ // Create a new module (and put it into the cache)
|
|
43
|
+
/******/ var module = __webpack_module_cache__[moduleId] = {
|
|
44
|
+
/******/ // no module.id needed
|
|
45
|
+
/******/ // no module.loaded needed
|
|
46
|
+
/******/ exports: {}
|
|
47
|
+
/******/ };
|
|
48
|
+
/******/
|
|
49
|
+
/******/ // Execute the module function
|
|
50
|
+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
51
|
+
/******/
|
|
52
|
+
/******/ // Return the exports of the module
|
|
53
|
+
/******/ return module.exports;
|
|
54
|
+
/******/ }
|
|
55
|
+
/******/
|
|
56
|
+
/************************************************************************/
|
|
57
|
+
/******/ /* webpack/runtime/define property getters */
|
|
58
|
+
/******/ (() => {
|
|
59
|
+
/******/ // define getter functions for harmony exports
|
|
60
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
61
|
+
/******/ for(var key in definition) {
|
|
62
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
63
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
64
|
+
/******/ }
|
|
65
|
+
/******/ }
|
|
66
|
+
/******/ };
|
|
67
|
+
/******/ })();
|
|
68
|
+
/******/
|
|
69
|
+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
70
|
+
/******/ (() => {
|
|
71
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
72
|
+
/******/ })();
|
|
73
|
+
/******/
|
|
74
|
+
/******/ /* webpack/runtime/make namespace object */
|
|
75
|
+
/******/ (() => {
|
|
76
|
+
/******/ // define __esModule on exports
|
|
77
|
+
/******/ __webpack_require__.r = (exports) => {
|
|
78
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
79
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
80
|
+
/******/ }
|
|
81
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
82
|
+
/******/ };
|
|
83
|
+
/******/ })();
|
|
84
|
+
/******/
|
|
85
|
+
/************************************************************************/
|
|
86
|
+
var __webpack_exports__ = {};
|
|
87
|
+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
|
|
88
|
+
(() => {
|
|
89
|
+
// ESM COMPAT FLAG
|
|
90
|
+
__webpack_require__.r(__webpack_exports__);
|
|
91
|
+
|
|
92
|
+
// EXPORTS
|
|
93
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
94
|
+
loadSVGPath: () => (/* binding */ loadSVGPath),
|
|
95
|
+
svgPathName: () => (/* binding */ svgPathName)
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// EXTERNAL MODULE: external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"}
|
|
99
|
+
var engine_root_window_ = __webpack_require__(533);
|
|
100
|
+
;// CONCATENATED MODULE: ./dist/browser/SVGPathGenerator.js
|
|
101
|
+
|
|
102
|
+
class SVGPathGenerator {
|
|
103
|
+
constructor() {
|
|
104
|
+
this._paths = [];
|
|
105
|
+
this._reverse = false;
|
|
106
|
+
this._size = {
|
|
107
|
+
width: 0,
|
|
108
|
+
height: 0
|
|
109
|
+
};
|
|
110
|
+
this._scale = 1;
|
|
111
|
+
this._offset = {
|
|
112
|
+
x: 0,
|
|
113
|
+
y: 0,
|
|
114
|
+
mode: "percent"
|
|
115
|
+
};
|
|
116
|
+
this._width = 0;
|
|
117
|
+
}
|
|
118
|
+
generate(particle, delta) {
|
|
119
|
+
const container = particle.container,
|
|
120
|
+
pxRatio = container.retina.pixelRatio;
|
|
121
|
+
if (particle.svgDirection === undefined) {
|
|
122
|
+
particle.svgDirection = (0,engine_root_window_.getRandom)() > 0.5 ? 0 : 1;
|
|
123
|
+
}
|
|
124
|
+
if (particle.svgPathIndex === undefined) {
|
|
125
|
+
particle.svgPathIndex = Math.floor(Math.random() * this._paths.length);
|
|
126
|
+
}
|
|
127
|
+
if (particle.svgSpeed === undefined) {
|
|
128
|
+
particle.svgSpeed = particle.velocity.mult((particle.retina.moveSpeed ?? 1) / 2).length;
|
|
129
|
+
}
|
|
130
|
+
if (particle.svgStep === undefined) {
|
|
131
|
+
particle.svgStep = (0,engine_root_window_.randomInRange)({
|
|
132
|
+
min: 0,
|
|
133
|
+
max: this._paths[particle.svgPathIndex].length
|
|
134
|
+
}) * pxRatio;
|
|
135
|
+
}
|
|
136
|
+
if (particle.svgOffset === undefined) {
|
|
137
|
+
particle.svgOffset = {
|
|
138
|
+
width: (0,engine_root_window_.randomInRange)({
|
|
139
|
+
min: -this._width / 2,
|
|
140
|
+
max: this._width / 2
|
|
141
|
+
}) * pxRatio,
|
|
142
|
+
height: (0,engine_root_window_.randomInRange)({
|
|
143
|
+
min: -this._width / 2,
|
|
144
|
+
max: this._width / 2
|
|
145
|
+
}) * pxRatio
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (particle.svgInitialPosition === undefined) {
|
|
149
|
+
particle.svgInitialPosition = {
|
|
150
|
+
...particle.position
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
particle.velocity.x = 0;
|
|
154
|
+
particle.velocity.y = 0;
|
|
155
|
+
if (particle.svgDirection === 0) {
|
|
156
|
+
particle.svgStep += particle.svgSpeed * delta.factor;
|
|
157
|
+
} else {
|
|
158
|
+
particle.svgStep -= particle.svgSpeed * delta.factor;
|
|
159
|
+
}
|
|
160
|
+
let path = this._paths[particle.svgPathIndex];
|
|
161
|
+
if (path) {
|
|
162
|
+
const pathLength = path.length;
|
|
163
|
+
if (particle.svgStep >= pathLength) {
|
|
164
|
+
particle.svgPathIndex = particle.svgPathIndex + 1;
|
|
165
|
+
if (particle.svgPathIndex >= this._paths.length) {
|
|
166
|
+
if (this._reverse) {
|
|
167
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
168
|
+
particle.svgDirection = 1;
|
|
169
|
+
} else {
|
|
170
|
+
particle.svgPathIndex = 0;
|
|
171
|
+
particle.svgStep = 0;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} else if (particle.svgStep <= 0) {
|
|
175
|
+
particle.svgPathIndex = particle.svgPathIndex - 1;
|
|
176
|
+
if (particle.svgPathIndex < 0) {
|
|
177
|
+
if (this._reverse) {
|
|
178
|
+
particle.svgPathIndex = 0;
|
|
179
|
+
particle.svgDirection = 0;
|
|
180
|
+
} else {
|
|
181
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
182
|
+
path = this._paths[particle.svgPathIndex];
|
|
183
|
+
particle.svgStep = path.length;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
path = this._paths[particle.svgPathIndex];
|
|
188
|
+
}
|
|
189
|
+
if (path) {
|
|
190
|
+
const pathElement = path.element,
|
|
191
|
+
pos = pathElement.getPointAtLength(particle.svgStep),
|
|
192
|
+
canvasSize = particle.container.canvas.size,
|
|
193
|
+
offset = (0,engine_root_window_.getPosition)(this._offset, canvasSize),
|
|
194
|
+
scale = this._scale * pxRatio;
|
|
195
|
+
particle.position.x = (pos.x - this._size.width / 2) * scale + particle.svgInitialPosition.x + offset.x + particle.svgOffset.width;
|
|
196
|
+
particle.position.y = (pos.y - this._size.height / 2) * scale + particle.svgInitialPosition.y + offset.y + particle.svgOffset.height;
|
|
197
|
+
}
|
|
198
|
+
return engine_root_window_.Vector.origin;
|
|
199
|
+
}
|
|
200
|
+
init(container) {
|
|
201
|
+
const options = container.actualOptions.particles.move.path.options,
|
|
202
|
+
position = options.position ?? this._offset;
|
|
203
|
+
this._reverse = options.reverse ?? this._reverse;
|
|
204
|
+
this._scale = options.scale ?? 1;
|
|
205
|
+
this._offset.x = position.x;
|
|
206
|
+
this._offset.y = position.y;
|
|
207
|
+
this._offset.mode = position.mode;
|
|
208
|
+
this._width = options.width ?? 0;
|
|
209
|
+
if (options.url && !options.path) {
|
|
210
|
+
const url = options.url;
|
|
211
|
+
(async () => {
|
|
212
|
+
const response = await fetch(url),
|
|
213
|
+
data = await response.text();
|
|
214
|
+
const parser = new DOMParser(),
|
|
215
|
+
doc = parser.parseFromString(data, "image/svg+xml"),
|
|
216
|
+
svg = doc.getElementsByTagName("svg")[0];
|
|
217
|
+
let svgPaths = svg.getElementsByTagName("path");
|
|
218
|
+
if (!svgPaths.length) {
|
|
219
|
+
svgPaths = doc.getElementsByTagName("path");
|
|
220
|
+
}
|
|
221
|
+
this._paths = [];
|
|
222
|
+
for (let i = 0; i < svgPaths.length; i++) {
|
|
223
|
+
const path = svgPaths.item(i);
|
|
224
|
+
if (path) {
|
|
225
|
+
this._paths.push({
|
|
226
|
+
element: path,
|
|
227
|
+
length: path.getTotalLength()
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
this._size.height = parseFloat(svg.getAttribute("height") ?? "0");
|
|
232
|
+
this._size.width = parseFloat(svg.getAttribute("width") ?? "0");
|
|
233
|
+
})();
|
|
234
|
+
} else if (options.path) {
|
|
235
|
+
const path = options.path;
|
|
236
|
+
this._paths = [];
|
|
237
|
+
for (const item of path.data) {
|
|
238
|
+
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
239
|
+
element.setAttribute("d", item);
|
|
240
|
+
this._paths.push({
|
|
241
|
+
element,
|
|
242
|
+
length: element.getTotalLength()
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
this._size.height = path.size.height;
|
|
246
|
+
this._size.width = path.size.width;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
reset() {}
|
|
250
|
+
update() {}
|
|
251
|
+
}
|
|
252
|
+
;// CONCATENATED MODULE: ./dist/browser/index.js
|
|
253
|
+
|
|
254
|
+
const svgPathName = "svgPathGenerator";
|
|
255
|
+
async function loadSVGPath(engine, refresh = true) {
|
|
256
|
+
await engine.addPathGenerator(svgPathName, new SVGPathGenerator(), refresh);
|
|
257
|
+
}
|
|
258
|
+
})();
|
|
259
|
+
|
|
260
|
+
/******/ return __webpack_exports__;
|
|
261
|
+
/******/ })()
|
|
262
|
+
;
|
|
263
|
+
});
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! For license information please see tsparticles.path.svg.min.js.LICENSE.txt */
|
|
2
|
+
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],e);else{var s="object"==typeof exports?e(require("@tsparticles/engine")):e(t.window);for(var i in s)("object"==typeof exports?exports:t)[i]=s[i]}}(this,(t=>(()=>{"use strict";var e={533:e=>{e.exports=t}},s={};function i(t){var n=s[t];if(void 0!==n)return n.exports;var h=s[t]={exports:{}};return e[t](h,h.exports,i),h.exports}i.d=(t,e)=>{for(var s in e)i.o(e,s)&&!i.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),i.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var n={};return(()=>{i.r(n),i.d(n,{loadSVGPath:()=>h,svgPathName:()=>s});var t=i(533);class e{constructor(){this._paths=[],this._reverse=!1,this._size={width:0,height:0},this._scale=1,this._offset={x:0,y:0,mode:"percent"},this._width=0}generate(e,s){const i=e.container.retina.pixelRatio;void 0===e.svgDirection&&(e.svgDirection=(0,t.getRandom)()>.5?0:1),void 0===e.svgPathIndex&&(e.svgPathIndex=Math.floor(Math.random()*this._paths.length)),void 0===e.svgSpeed&&(e.svgSpeed=e.velocity.mult((e.retina.moveSpeed??1)/2).length),void 0===e.svgStep&&(e.svgStep=(0,t.randomInRange)({min:0,max:this._paths[e.svgPathIndex].length})*i),void 0===e.svgOffset&&(e.svgOffset={width:(0,t.randomInRange)({min:-this._width/2,max:this._width/2})*i,height:(0,t.randomInRange)({min:-this._width/2,max:this._width/2})*i}),void 0===e.svgInitialPosition&&(e.svgInitialPosition={...e.position}),e.velocity.x=0,e.velocity.y=0,0===e.svgDirection?e.svgStep+=e.svgSpeed*s.factor:e.svgStep-=e.svgSpeed*s.factor;let n=this._paths[e.svgPathIndex];if(n){const t=n.length;e.svgStep>=t?(e.svgPathIndex=e.svgPathIndex+1,e.svgPathIndex>=this._paths.length&&(this._reverse?(e.svgPathIndex=this._paths.length-1,e.svgDirection=1):(e.svgPathIndex=0,e.svgStep=0))):e.svgStep<=0&&(e.svgPathIndex=e.svgPathIndex-1,e.svgPathIndex<0&&(this._reverse?(e.svgPathIndex=0,e.svgDirection=0):(e.svgPathIndex=this._paths.length-1,n=this._paths[e.svgPathIndex],e.svgStep=n.length))),n=this._paths[e.svgPathIndex]}if(n){const s=n.element.getPointAtLength(e.svgStep),h=e.container.canvas.size,o=(0,t.getPosition)(this._offset,h),a=this._scale*i;e.position.x=(s.x-this._size.width/2)*a+e.svgInitialPosition.x+o.x+e.svgOffset.width,e.position.y=(s.y-this._size.height/2)*a+e.svgInitialPosition.y+o.y+e.svgOffset.height}return t.Vector.origin}init(t){const e=t.actualOptions.particles.move.path.options,s=e.position??this._offset;if(this._reverse=e.reverse??this._reverse,this._scale=e.scale??1,this._offset.x=s.x,this._offset.y=s.y,this._offset.mode=s.mode,this._width=e.width??0,e.url&&!e.path){const t=e.url;(async()=>{const e=await fetch(t),s=await e.text(),i=(new DOMParser).parseFromString(s,"image/svg+xml"),n=i.getElementsByTagName("svg")[0];let h=n.getElementsByTagName("path");h.length||(h=i.getElementsByTagName("path")),this._paths=[];for(let t=0;t<h.length;t++){const e=h.item(t);e&&this._paths.push({element:e,length:e.getTotalLength()})}this._size.height=parseFloat(n.getAttribute("height")??"0"),this._size.width=parseFloat(n.getAttribute("width")??"0")})()}else if(e.path){const t=e.path;this._paths=[];for(const e of t.data){const t=document.createElementNS("http://www.w3.org/2000/svg","path");t.setAttribute("d",e),this._paths.push({element:t,length:t.getTotalLength()})}this._size.height=t.size.height,this._size.width=t.size.width}}reset(){}update(){}}const s="svgPathGenerator";async function h(t,i=!0){await t.addPathGenerator(s,new e,i)}})(),n})()));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/*! tsParticles SVG Path v3.0.0-beta.0 by Matteo Bruni */
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Container, type ICoordinates, type IDelta, type IDimension, type IMovePathGenerator, type Particle, Vector } from "@tsparticles/engine";
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
[key: string]: unknown;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
declare const enum SVGPathDirection {
|
|
8
|
+
normal = 0,
|
|
9
|
+
reverse = 1
|
|
10
|
+
}
|
|
11
|
+
type SVGPathParticle = Particle & {
|
|
12
|
+
svgDirection?: SVGPathDirection;
|
|
13
|
+
svgInitialPosition?: ICoordinates;
|
|
14
|
+
svgOffset?: IDimension;
|
|
15
|
+
svgPathIndex?: number;
|
|
16
|
+
svgSpeed?: number;
|
|
17
|
+
svgStep?: number;
|
|
18
|
+
};
|
|
19
|
+
export declare class SVGPathGenerator implements IMovePathGenerator {
|
|
20
|
+
private readonly _offset;
|
|
21
|
+
private _paths;
|
|
22
|
+
private _reverse;
|
|
23
|
+
private _scale;
|
|
24
|
+
private readonly _size;
|
|
25
|
+
private _width;
|
|
26
|
+
constructor();
|
|
27
|
+
generate(particle: SVGPathParticle, delta: IDelta): Vector;
|
|
28
|
+
init(container: Container): void;
|
|
29
|
+
reset(): void;
|
|
30
|
+
update(): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "@tsparticles/engine"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SVGPathGenerator = void 0;
|
|
13
|
+
const engine_1 = require("@tsparticles/engine");
|
|
14
|
+
class SVGPathGenerator {
|
|
15
|
+
constructor() {
|
|
16
|
+
this._paths = [];
|
|
17
|
+
this._reverse = false;
|
|
18
|
+
this._size = { width: 0, height: 0 };
|
|
19
|
+
this._scale = 1;
|
|
20
|
+
this._offset = { x: 0, y: 0, mode: "percent" };
|
|
21
|
+
this._width = 0;
|
|
22
|
+
}
|
|
23
|
+
generate(particle, delta) {
|
|
24
|
+
const container = particle.container, pxRatio = container.retina.pixelRatio;
|
|
25
|
+
if (particle.svgDirection === undefined) {
|
|
26
|
+
particle.svgDirection = (0, engine_1.getRandom)() > 0.5 ? 0 : 1;
|
|
27
|
+
}
|
|
28
|
+
if (particle.svgPathIndex === undefined) {
|
|
29
|
+
particle.svgPathIndex = Math.floor(Math.random() * this._paths.length);
|
|
30
|
+
}
|
|
31
|
+
if (particle.svgSpeed === undefined) {
|
|
32
|
+
particle.svgSpeed = particle.velocity.mult((particle.retina.moveSpeed ?? 1) / 2).length;
|
|
33
|
+
}
|
|
34
|
+
if (particle.svgStep === undefined) {
|
|
35
|
+
particle.svgStep = (0, engine_1.randomInRange)({ min: 0, max: this._paths[particle.svgPathIndex].length }) * pxRatio;
|
|
36
|
+
}
|
|
37
|
+
if (particle.svgOffset === undefined) {
|
|
38
|
+
particle.svgOffset = {
|
|
39
|
+
width: (0, engine_1.randomInRange)({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
40
|
+
height: (0, engine_1.randomInRange)({ min: -this._width / 2, max: this._width / 2 }) * pxRatio,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (particle.svgInitialPosition === undefined) {
|
|
44
|
+
particle.svgInitialPosition = { ...particle.position };
|
|
45
|
+
}
|
|
46
|
+
particle.velocity.x = 0;
|
|
47
|
+
particle.velocity.y = 0;
|
|
48
|
+
if (particle.svgDirection === 0) {
|
|
49
|
+
particle.svgStep += particle.svgSpeed * delta.factor;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
particle.svgStep -= particle.svgSpeed * delta.factor;
|
|
53
|
+
}
|
|
54
|
+
let path = this._paths[particle.svgPathIndex];
|
|
55
|
+
if (path) {
|
|
56
|
+
const pathLength = path.length;
|
|
57
|
+
if (particle.svgStep >= pathLength) {
|
|
58
|
+
particle.svgPathIndex = particle.svgPathIndex + 1;
|
|
59
|
+
if (particle.svgPathIndex >= this._paths.length) {
|
|
60
|
+
if (this._reverse) {
|
|
61
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
62
|
+
particle.svgDirection = 1;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
particle.svgPathIndex = 0;
|
|
66
|
+
particle.svgStep = 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (particle.svgStep <= 0) {
|
|
71
|
+
particle.svgPathIndex = particle.svgPathIndex - 1;
|
|
72
|
+
if (particle.svgPathIndex < 0) {
|
|
73
|
+
if (this._reverse) {
|
|
74
|
+
particle.svgPathIndex = 0;
|
|
75
|
+
particle.svgDirection = 0;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
particle.svgPathIndex = this._paths.length - 1;
|
|
79
|
+
path = this._paths[particle.svgPathIndex];
|
|
80
|
+
particle.svgStep = path.length;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
path = this._paths[particle.svgPathIndex];
|
|
85
|
+
}
|
|
86
|
+
if (path) {
|
|
87
|
+
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;
|
|
88
|
+
particle.position.x =
|
|
89
|
+
(pos.x - this._size.width / 2) * scale +
|
|
90
|
+
particle.svgInitialPosition.x +
|
|
91
|
+
offset.x +
|
|
92
|
+
particle.svgOffset.width;
|
|
93
|
+
particle.position.y =
|
|
94
|
+
(pos.y - this._size.height / 2) * scale +
|
|
95
|
+
particle.svgInitialPosition.y +
|
|
96
|
+
offset.y +
|
|
97
|
+
particle.svgOffset.height;
|
|
98
|
+
}
|
|
99
|
+
return engine_1.Vector.origin;
|
|
100
|
+
}
|
|
101
|
+
init(container) {
|
|
102
|
+
const options = container.actualOptions.particles.move.path.options, position = options.position ?? this._offset;
|
|
103
|
+
this._reverse = options.reverse ?? this._reverse;
|
|
104
|
+
this._scale = options.scale ?? 1;
|
|
105
|
+
this._offset.x = position.x;
|
|
106
|
+
this._offset.y = position.y;
|
|
107
|
+
this._offset.mode = position.mode;
|
|
108
|
+
this._width = options.width ?? 0;
|
|
109
|
+
if (options.url && !options.path) {
|
|
110
|
+
const url = options.url;
|
|
111
|
+
(async () => {
|
|
112
|
+
const response = await fetch(url), data = await response.text();
|
|
113
|
+
const parser = new DOMParser(), doc = parser.parseFromString(data, "image/svg+xml"), svg = doc.getElementsByTagName("svg")[0];
|
|
114
|
+
let svgPaths = svg.getElementsByTagName("path");
|
|
115
|
+
if (!svgPaths.length) {
|
|
116
|
+
svgPaths = doc.getElementsByTagName("path");
|
|
117
|
+
}
|
|
118
|
+
this._paths = [];
|
|
119
|
+
for (let i = 0; i < svgPaths.length; i++) {
|
|
120
|
+
const path = svgPaths.item(i);
|
|
121
|
+
if (path) {
|
|
122
|
+
this._paths.push({
|
|
123
|
+
element: path,
|
|
124
|
+
length: path.getTotalLength(),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this._size.height = parseFloat(svg.getAttribute("height") ?? "0");
|
|
129
|
+
this._size.width = parseFloat(svg.getAttribute("width") ?? "0");
|
|
130
|
+
})();
|
|
131
|
+
}
|
|
132
|
+
else if (options.path) {
|
|
133
|
+
const path = options.path;
|
|
134
|
+
this._paths = [];
|
|
135
|
+
for (const item of path.data) {
|
|
136
|
+
const element = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
137
|
+
element.setAttribute("d", item);
|
|
138
|
+
this._paths.push({
|
|
139
|
+
element,
|
|
140
|
+
length: element.getTotalLength(),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
this._size.height = path.size.height;
|
|
144
|
+
this._size.width = path.size.width;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
reset() {
|
|
148
|
+
}
|
|
149
|
+
update() {
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.SVGPathGenerator = SVGPathGenerator;
|
|
153
|
+
});
|
package/umd/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./SVGPathGenerator"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.loadSVGPath = exports.svgPathName = void 0;
|
|
13
|
+
const SVGPathGenerator_1 = require("./SVGPathGenerator");
|
|
14
|
+
exports.svgPathName = "svgPathGenerator";
|
|
15
|
+
async function loadSVGPath(engine, refresh = true) {
|
|
16
|
+
await engine.addPathGenerator(exports.svgPathName, new SVGPathGenerator_1.SVGPathGenerator(), refresh);
|
|
17
|
+
}
|
|
18
|
+
exports.loadSVGPath = loadSVGPath;
|
|
19
|
+
});
|