@tsparticles/plugin-emitters-shape-path 3.0.0-beta.4
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 +79 -0
- package/browser/EmittersPathShape.js +73 -0
- package/browser/EmittersPathShapeGenerator.js +9 -0
- package/browser/Options/Classes/EmittersPathShapeOptions.js +13 -0
- package/browser/Options/Interfaces/IEmittersPathShapeOptions.js +1 -0
- package/browser/index.js +7 -0
- package/browser/package.json +1 -0
- package/browser/utils.js +29 -0
- package/cjs/EmittersPathShape.js +77 -0
- package/cjs/EmittersPathShapeGenerator.js +13 -0
- package/cjs/Options/Classes/EmittersPathShapeOptions.js +17 -0
- package/cjs/Options/Interfaces/IEmittersPathShapeOptions.js +2 -0
- package/cjs/index.js +11 -0
- package/cjs/package.json +1 -0
- package/cjs/utils.js +34 -0
- package/esm/EmittersPathShape.js +73 -0
- package/esm/EmittersPathShapeGenerator.js +9 -0
- package/esm/Options/Classes/EmittersPathShapeOptions.js +13 -0
- package/esm/Options/Interfaces/IEmittersPathShapeOptions.js +1 -0
- package/esm/index.js +7 -0
- package/esm/package.json +1 -0
- package/esm/utils.js +29 -0
- package/package.json +109 -0
- package/report.html +39 -0
- package/tsparticles.plugin.emitters.shape.path.js +257 -0
- package/tsparticles.plugin.emitters.shape.path.min.js +2 -0
- package/tsparticles.plugin.emitters.shape.path.min.js.LICENSE.txt +1 -0
- package/types/EmittersPathShape.d.ts +12 -0
- package/types/EmittersPathShapeGenerator.d.ts +6 -0
- package/types/Options/Classes/EmittersPathShapeOptions.d.ts +7 -0
- package/types/Options/Interfaces/IEmittersPathShapeOptions.d.ts +4 -0
- package/types/index.d.ts +2 -0
- package/types/utils.d.ts +3 -0
- package/umd/EmittersPathShape.js +87 -0
- package/umd/EmittersPathShapeGenerator.js +23 -0
- package/umd/Options/Classes/EmittersPathShapeOptions.js +27 -0
- package/umd/Options/Interfaces/IEmittersPathShapeOptions.js +12 -0
- package/umd/index.js +21 -0
- package/umd/utils.js +44 -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,79 @@
|
|
|
1
|
+
[](https://particles.js.org)
|
|
2
|
+
|
|
3
|
+
# tsParticles Emitters Shape Path Plugin
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/@tsparticles/plugin-emitters-shape-path)
|
|
6
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-emitters-shape-path)
|
|
7
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-emitters-shape-path) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/tsparticles/tsparticles) plugin for adding the emitters shape path support.
|
|
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.plugin.emitters.shape.path.min.js` file will export the function to load the plugin:
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
loadEmittersShapePathPlugin
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Usage
|
|
24
|
+
|
|
25
|
+
Once the scripts are loaded you can set up `tsParticles` and the plugin like this:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
(async () => {
|
|
29
|
+
await loadEmittersPlugin(tsParticles);
|
|
30
|
+
await loadEmittersShapePathPlugin(tsParticles);
|
|
31
|
+
|
|
32
|
+
await tsParticles.load({
|
|
33
|
+
id: "tsparticles",
|
|
34
|
+
options: {
|
|
35
|
+
/* options */
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
})();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### ESM / CommonJS
|
|
42
|
+
|
|
43
|
+
This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
|
|
44
|
+
|
|
45
|
+
```shell
|
|
46
|
+
$ npm install @tsparticles/plugin-emitters-shape-path
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
or
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
$ yarn add @tsparticles/plugin-emitters-shape-path
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Then you need to import it in the app, like this:
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const { tsParticles } = require("@tsparticles/engine");
|
|
59
|
+
const { loadEmittersPlugin } = require("@tsparticles/plugin-emitters");
|
|
60
|
+
const { loadEmittersShapePathPlugin } = require("@tsparticles/plugin-emitters-shape-path");
|
|
61
|
+
|
|
62
|
+
(async () => {
|
|
63
|
+
await loadEmittersPlugin(tsParticles);
|
|
64
|
+
await loadEmittersShapePathPlugin(tsParticles);
|
|
65
|
+
})();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
or
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
72
|
+
import { loadEmittersPlugin } from "@tsparticles/plugin-emitters";
|
|
73
|
+
import { loadEmittersShapePathPlugin } from "@tsparticles/plugin-emitters-shape-path";
|
|
74
|
+
|
|
75
|
+
(async () => {
|
|
76
|
+
await loadEmittersPlugin(tsParticles);
|
|
77
|
+
await loadEmittersShapePathPlugin(tsParticles);
|
|
78
|
+
})();
|
|
79
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EmitterShapeBase } from "@tsparticles/plugin-emitters";
|
|
2
|
+
import { errorPrefix } from "@tsparticles/engine";
|
|
3
|
+
import { generateRandomPointOnPathPerimeter, generateRandomPointWithinPath } from "./utils.js";
|
|
4
|
+
export class EmittersPathShape extends EmitterShapeBase {
|
|
5
|
+
constructor(position, size, fill, options) {
|
|
6
|
+
super(position, size, fill, options);
|
|
7
|
+
const ctx = document.createElement("canvas").getContext("2d");
|
|
8
|
+
if (!ctx) {
|
|
9
|
+
throw new Error(`${errorPrefix} No 2d context available`);
|
|
10
|
+
}
|
|
11
|
+
this.checkContext = ctx;
|
|
12
|
+
this.points = options.points;
|
|
13
|
+
const pathData = this.points, path = new Path2D(), offset = {
|
|
14
|
+
x: position.x - size.width / 2,
|
|
15
|
+
y: position.y - size.height / 2,
|
|
16
|
+
};
|
|
17
|
+
for (const [index, point] of pathData.entries()) {
|
|
18
|
+
const coords = {
|
|
19
|
+
x: offset.x + (point.x * size.width) / 100,
|
|
20
|
+
y: offset.y + (point.y * size.height) / 100,
|
|
21
|
+
};
|
|
22
|
+
if (index === 0) {
|
|
23
|
+
path.moveTo(coords.x, coords.y);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
path.lineTo(coords.x, coords.y);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (pathData[0]) {
|
|
30
|
+
const coords = {
|
|
31
|
+
x: offset.x + (pathData[0].x * size.width) / 100,
|
|
32
|
+
y: offset.y + (pathData[0].y * size.height) / 100,
|
|
33
|
+
};
|
|
34
|
+
path.lineTo(coords.x, coords.y);
|
|
35
|
+
}
|
|
36
|
+
this.path = path;
|
|
37
|
+
}
|
|
38
|
+
async init() {
|
|
39
|
+
}
|
|
40
|
+
async randomPosition() {
|
|
41
|
+
const ctx = this.checkContext, position = this.position, size = this.size, fill = this.fill, path = this.path, res = fill
|
|
42
|
+
? generateRandomPointWithinPath(ctx, path, position, size)
|
|
43
|
+
: generateRandomPointOnPathPerimeter(ctx, path, position, size);
|
|
44
|
+
return res ? { position: res } : null;
|
|
45
|
+
}
|
|
46
|
+
resize(position, size) {
|
|
47
|
+
super.resize(position, size);
|
|
48
|
+
const pathData = this.points, path = new Path2D(), offset = {
|
|
49
|
+
x: position.x - size.width / 2,
|
|
50
|
+
y: position.y - size.height / 2,
|
|
51
|
+
};
|
|
52
|
+
for (const [index, point] of pathData.entries()) {
|
|
53
|
+
const coords = {
|
|
54
|
+
x: offset.x + (point.x * size.width) / 100,
|
|
55
|
+
y: offset.y + (point.y * size.height) / 100,
|
|
56
|
+
};
|
|
57
|
+
if (index === 0) {
|
|
58
|
+
path.moveTo(coords.x, coords.y);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
path.lineTo(coords.x, coords.y);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (pathData[0]) {
|
|
65
|
+
const coords = {
|
|
66
|
+
x: offset.x + (pathData[0].x * size.width) / 100,
|
|
67
|
+
y: offset.y + (pathData[0].y * size.height) / 100,
|
|
68
|
+
};
|
|
69
|
+
path.lineTo(coords.x, coords.y);
|
|
70
|
+
}
|
|
71
|
+
this.path = path;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EmittersPathShape } from "./EmittersPathShape.js";
|
|
2
|
+
import { EmittersPathShapeOptions } from "./Options/Classes/EmittersPathShapeOptions.js";
|
|
3
|
+
export class EmittersPathShapeGenerator {
|
|
4
|
+
generate(position, size, fill, options) {
|
|
5
|
+
const shapeOptions = new EmittersPathShapeOptions();
|
|
6
|
+
shapeOptions.load(options);
|
|
7
|
+
return new EmittersPathShape(position, size, fill, shapeOptions);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EmittersPathShapeGenerator } from "./EmittersPathShapeGenerator.js";
|
|
2
|
+
export async function loadEmittersShapePath(engine, refresh = true) {
|
|
3
|
+
const emittersEngine = engine;
|
|
4
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
5
|
+
emittersEngine.addEmitterShapeGenerator("path", new EmittersPathShapeGenerator());
|
|
6
|
+
await emittersEngine.refresh(refresh);
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
package/browser/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getRandom } from "@tsparticles/engine";
|
|
2
|
+
export function generateRandomPointWithinPath(ctx, path, center, size) {
|
|
3
|
+
let randomPoint = null;
|
|
4
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
5
|
+
const tmpPoint = {
|
|
6
|
+
x: center.x + getRandom() * size.width - size.width / 2,
|
|
7
|
+
y: center.y + getRandom() * size.height - size.height / 2,
|
|
8
|
+
};
|
|
9
|
+
if (ctx.isPointInPath(path, tmpPoint.x, tmpPoint.y)) {
|
|
10
|
+
randomPoint = tmpPoint;
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return randomPoint;
|
|
15
|
+
}
|
|
16
|
+
export function generateRandomPointOnPathPerimeter(ctx, path, center, size) {
|
|
17
|
+
let randomPoint = null;
|
|
18
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
19
|
+
const tmpPoint = {
|
|
20
|
+
x: center.x + getRandom() * size.width - size.width / 2,
|
|
21
|
+
y: center.y + getRandom() * size.height - size.height / 2,
|
|
22
|
+
};
|
|
23
|
+
if (ctx.isPointInStroke(path, tmpPoint.x, tmpPoint.y)) {
|
|
24
|
+
randomPoint = tmpPoint;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return randomPoint;
|
|
29
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmittersPathShape = void 0;
|
|
4
|
+
const plugin_emitters_1 = require("@tsparticles/plugin-emitters");
|
|
5
|
+
const engine_1 = require("@tsparticles/engine");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
class EmittersPathShape extends plugin_emitters_1.EmitterShapeBase {
|
|
8
|
+
constructor(position, size, fill, options) {
|
|
9
|
+
super(position, size, fill, options);
|
|
10
|
+
const ctx = document.createElement("canvas").getContext("2d");
|
|
11
|
+
if (!ctx) {
|
|
12
|
+
throw new Error(`${engine_1.errorPrefix} No 2d context available`);
|
|
13
|
+
}
|
|
14
|
+
this.checkContext = ctx;
|
|
15
|
+
this.points = options.points;
|
|
16
|
+
const pathData = this.points, path = new Path2D(), offset = {
|
|
17
|
+
x: position.x - size.width / 2,
|
|
18
|
+
y: position.y - size.height / 2,
|
|
19
|
+
};
|
|
20
|
+
for (const [index, point] of pathData.entries()) {
|
|
21
|
+
const coords = {
|
|
22
|
+
x: offset.x + (point.x * size.width) / 100,
|
|
23
|
+
y: offset.y + (point.y * size.height) / 100,
|
|
24
|
+
};
|
|
25
|
+
if (index === 0) {
|
|
26
|
+
path.moveTo(coords.x, coords.y);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
path.lineTo(coords.x, coords.y);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (pathData[0]) {
|
|
33
|
+
const coords = {
|
|
34
|
+
x: offset.x + (pathData[0].x * size.width) / 100,
|
|
35
|
+
y: offset.y + (pathData[0].y * size.height) / 100,
|
|
36
|
+
};
|
|
37
|
+
path.lineTo(coords.x, coords.y);
|
|
38
|
+
}
|
|
39
|
+
this.path = path;
|
|
40
|
+
}
|
|
41
|
+
async init() {
|
|
42
|
+
}
|
|
43
|
+
async randomPosition() {
|
|
44
|
+
const ctx = this.checkContext, position = this.position, size = this.size, fill = this.fill, path = this.path, res = fill
|
|
45
|
+
? (0, utils_js_1.generateRandomPointWithinPath)(ctx, path, position, size)
|
|
46
|
+
: (0, utils_js_1.generateRandomPointOnPathPerimeter)(ctx, path, position, size);
|
|
47
|
+
return res ? { position: res } : null;
|
|
48
|
+
}
|
|
49
|
+
resize(position, size) {
|
|
50
|
+
super.resize(position, size);
|
|
51
|
+
const pathData = this.points, path = new Path2D(), offset = {
|
|
52
|
+
x: position.x - size.width / 2,
|
|
53
|
+
y: position.y - size.height / 2,
|
|
54
|
+
};
|
|
55
|
+
for (const [index, point] of pathData.entries()) {
|
|
56
|
+
const coords = {
|
|
57
|
+
x: offset.x + (point.x * size.width) / 100,
|
|
58
|
+
y: offset.y + (point.y * size.height) / 100,
|
|
59
|
+
};
|
|
60
|
+
if (index === 0) {
|
|
61
|
+
path.moveTo(coords.x, coords.y);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
path.lineTo(coords.x, coords.y);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (pathData[0]) {
|
|
68
|
+
const coords = {
|
|
69
|
+
x: offset.x + (pathData[0].x * size.width) / 100,
|
|
70
|
+
y: offset.y + (pathData[0].y * size.height) / 100,
|
|
71
|
+
};
|
|
72
|
+
path.lineTo(coords.x, coords.y);
|
|
73
|
+
}
|
|
74
|
+
this.path = path;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.EmittersPathShape = EmittersPathShape;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmittersPathShapeGenerator = void 0;
|
|
4
|
+
const EmittersPathShape_js_1 = require("./EmittersPathShape.js");
|
|
5
|
+
const EmittersPathShapeOptions_js_1 = require("./Options/Classes/EmittersPathShapeOptions.js");
|
|
6
|
+
class EmittersPathShapeGenerator {
|
|
7
|
+
generate(position, size, fill, options) {
|
|
8
|
+
const shapeOptions = new EmittersPathShapeOptions_js_1.EmittersPathShapeOptions();
|
|
9
|
+
shapeOptions.load(options);
|
|
10
|
+
return new EmittersPathShape_js_1.EmittersPathShape(position, size, fill, shapeOptions);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.EmittersPathShapeGenerator = EmittersPathShapeGenerator;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmittersPathShapeOptions = void 0;
|
|
4
|
+
class EmittersPathShapeOptions {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.points = [];
|
|
7
|
+
}
|
|
8
|
+
load(data) {
|
|
9
|
+
if (!data) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (data.points !== undefined) {
|
|
13
|
+
this.points = data.points.map((t) => ({ x: t.x ?? 50, y: t.y ?? 50 }));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.EmittersPathShapeOptions = EmittersPathShapeOptions;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadEmittersShapePath = void 0;
|
|
4
|
+
const EmittersPathShapeGenerator_js_1 = require("./EmittersPathShapeGenerator.js");
|
|
5
|
+
async function loadEmittersShapePath(engine, refresh = true) {
|
|
6
|
+
const emittersEngine = engine;
|
|
7
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
8
|
+
emittersEngine.addEmitterShapeGenerator("path", new EmittersPathShapeGenerator_js_1.EmittersPathShapeGenerator());
|
|
9
|
+
await emittersEngine.refresh(refresh);
|
|
10
|
+
}
|
|
11
|
+
exports.loadEmittersShapePath = loadEmittersShapePath;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRandomPointOnPathPerimeter = exports.generateRandomPointWithinPath = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
function generateRandomPointWithinPath(ctx, path, center, size) {
|
|
6
|
+
let randomPoint = null;
|
|
7
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
8
|
+
const tmpPoint = {
|
|
9
|
+
x: center.x + (0, engine_1.getRandom)() * size.width - size.width / 2,
|
|
10
|
+
y: center.y + (0, engine_1.getRandom)() * size.height - size.height / 2,
|
|
11
|
+
};
|
|
12
|
+
if (ctx.isPointInPath(path, tmpPoint.x, tmpPoint.y)) {
|
|
13
|
+
randomPoint = tmpPoint;
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return randomPoint;
|
|
18
|
+
}
|
|
19
|
+
exports.generateRandomPointWithinPath = generateRandomPointWithinPath;
|
|
20
|
+
function generateRandomPointOnPathPerimeter(ctx, path, center, size) {
|
|
21
|
+
let randomPoint = null;
|
|
22
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
23
|
+
const tmpPoint = {
|
|
24
|
+
x: center.x + (0, engine_1.getRandom)() * size.width - size.width / 2,
|
|
25
|
+
y: center.y + (0, engine_1.getRandom)() * size.height - size.height / 2,
|
|
26
|
+
};
|
|
27
|
+
if (ctx.isPointInStroke(path, tmpPoint.x, tmpPoint.y)) {
|
|
28
|
+
randomPoint = tmpPoint;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return randomPoint;
|
|
33
|
+
}
|
|
34
|
+
exports.generateRandomPointOnPathPerimeter = generateRandomPointOnPathPerimeter;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EmitterShapeBase } from "@tsparticles/plugin-emitters";
|
|
2
|
+
import { errorPrefix } from "@tsparticles/engine";
|
|
3
|
+
import { generateRandomPointOnPathPerimeter, generateRandomPointWithinPath } from "./utils.js";
|
|
4
|
+
export class EmittersPathShape extends EmitterShapeBase {
|
|
5
|
+
constructor(position, size, fill, options) {
|
|
6
|
+
super(position, size, fill, options);
|
|
7
|
+
const ctx = document.createElement("canvas").getContext("2d");
|
|
8
|
+
if (!ctx) {
|
|
9
|
+
throw new Error(`${errorPrefix} No 2d context available`);
|
|
10
|
+
}
|
|
11
|
+
this.checkContext = ctx;
|
|
12
|
+
this.points = options.points;
|
|
13
|
+
const pathData = this.points, path = new Path2D(), offset = {
|
|
14
|
+
x: position.x - size.width / 2,
|
|
15
|
+
y: position.y - size.height / 2,
|
|
16
|
+
};
|
|
17
|
+
for (const [index, point] of pathData.entries()) {
|
|
18
|
+
const coords = {
|
|
19
|
+
x: offset.x + (point.x * size.width) / 100,
|
|
20
|
+
y: offset.y + (point.y * size.height) / 100,
|
|
21
|
+
};
|
|
22
|
+
if (index === 0) {
|
|
23
|
+
path.moveTo(coords.x, coords.y);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
path.lineTo(coords.x, coords.y);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (pathData[0]) {
|
|
30
|
+
const coords = {
|
|
31
|
+
x: offset.x + (pathData[0].x * size.width) / 100,
|
|
32
|
+
y: offset.y + (pathData[0].y * size.height) / 100,
|
|
33
|
+
};
|
|
34
|
+
path.lineTo(coords.x, coords.y);
|
|
35
|
+
}
|
|
36
|
+
this.path = path;
|
|
37
|
+
}
|
|
38
|
+
async init() {
|
|
39
|
+
}
|
|
40
|
+
async randomPosition() {
|
|
41
|
+
const ctx = this.checkContext, position = this.position, size = this.size, fill = this.fill, path = this.path, res = fill
|
|
42
|
+
? generateRandomPointWithinPath(ctx, path, position, size)
|
|
43
|
+
: generateRandomPointOnPathPerimeter(ctx, path, position, size);
|
|
44
|
+
return res ? { position: res } : null;
|
|
45
|
+
}
|
|
46
|
+
resize(position, size) {
|
|
47
|
+
super.resize(position, size);
|
|
48
|
+
const pathData = this.points, path = new Path2D(), offset = {
|
|
49
|
+
x: position.x - size.width / 2,
|
|
50
|
+
y: position.y - size.height / 2,
|
|
51
|
+
};
|
|
52
|
+
for (const [index, point] of pathData.entries()) {
|
|
53
|
+
const coords = {
|
|
54
|
+
x: offset.x + (point.x * size.width) / 100,
|
|
55
|
+
y: offset.y + (point.y * size.height) / 100,
|
|
56
|
+
};
|
|
57
|
+
if (index === 0) {
|
|
58
|
+
path.moveTo(coords.x, coords.y);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
path.lineTo(coords.x, coords.y);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (pathData[0]) {
|
|
65
|
+
const coords = {
|
|
66
|
+
x: offset.x + (pathData[0].x * size.width) / 100,
|
|
67
|
+
y: offset.y + (pathData[0].y * size.height) / 100,
|
|
68
|
+
};
|
|
69
|
+
path.lineTo(coords.x, coords.y);
|
|
70
|
+
}
|
|
71
|
+
this.path = path;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EmittersPathShape } from "./EmittersPathShape.js";
|
|
2
|
+
import { EmittersPathShapeOptions } from "./Options/Classes/EmittersPathShapeOptions.js";
|
|
3
|
+
export class EmittersPathShapeGenerator {
|
|
4
|
+
generate(position, size, fill, options) {
|
|
5
|
+
const shapeOptions = new EmittersPathShapeOptions();
|
|
6
|
+
shapeOptions.load(options);
|
|
7
|
+
return new EmittersPathShape(position, size, fill, shapeOptions);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EmittersPathShapeGenerator } from "./EmittersPathShapeGenerator.js";
|
|
2
|
+
export async function loadEmittersShapePath(engine, refresh = true) {
|
|
3
|
+
const emittersEngine = engine;
|
|
4
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
5
|
+
emittersEngine.addEmitterShapeGenerator("path", new EmittersPathShapeGenerator());
|
|
6
|
+
await emittersEngine.refresh(refresh);
|
|
7
|
+
}
|
package/esm/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getRandom } from "@tsparticles/engine";
|
|
2
|
+
export function generateRandomPointWithinPath(ctx, path, center, size) {
|
|
3
|
+
let randomPoint = null;
|
|
4
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
5
|
+
const tmpPoint = {
|
|
6
|
+
x: center.x + getRandom() * size.width - size.width / 2,
|
|
7
|
+
y: center.y + getRandom() * size.height - size.height / 2,
|
|
8
|
+
};
|
|
9
|
+
if (ctx.isPointInPath(path, tmpPoint.x, tmpPoint.y)) {
|
|
10
|
+
randomPoint = tmpPoint;
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return randomPoint;
|
|
15
|
+
}
|
|
16
|
+
export function generateRandomPointOnPathPerimeter(ctx, path, center, size) {
|
|
17
|
+
let randomPoint = null;
|
|
18
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
19
|
+
const tmpPoint = {
|
|
20
|
+
x: center.x + getRandom() * size.width - size.width / 2,
|
|
21
|
+
y: center.y + getRandom() * size.height - size.height / 2,
|
|
22
|
+
};
|
|
23
|
+
if (ctx.isPointInStroke(path, tmpPoint.x, tmpPoint.y)) {
|
|
24
|
+
randomPoint = tmpPoint;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return randomPoint;
|
|
29
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsparticles/plugin-emitters-shape-path",
|
|
3
|
+
"version": "3.0.0-beta.4",
|
|
4
|
+
"description": "tsParticles emitters shape path plugin",
|
|
5
|
+
"homepage": "https://particles.js.org",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/tsparticles/tsparticles.git",
|
|
9
|
+
"directory": "plugins/emitterShapes/path"
|
|
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
|
+
],
|
|
65
|
+
"author": "Matteo Bruni <matteo.bruni@me.com>",
|
|
66
|
+
"license": "MIT",
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/tsparticles/tsparticles/issues"
|
|
69
|
+
},
|
|
70
|
+
"funding": [
|
|
71
|
+
{
|
|
72
|
+
"type": "github",
|
|
73
|
+
"url": "https://github.com/sponsors/matteobruni"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"type": "github",
|
|
77
|
+
"url": "https://github.com/sponsors/tsparticles"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"type": "buymeacoffee",
|
|
81
|
+
"url": "https://www.buymeacoffee.com/matteobruni"
|
|
82
|
+
}
|
|
83
|
+
],
|
|
84
|
+
"sideEffects": false,
|
|
85
|
+
"jsdelivr": "tsparticles.plugin.emitters.shape.path.min.js",
|
|
86
|
+
"unpkg": "tsparticles.plugin.emitters.shape.path.min.js",
|
|
87
|
+
"browser": "browser/index.js",
|
|
88
|
+
"main": "cjs/index.js",
|
|
89
|
+
"module": "esm/index.js",
|
|
90
|
+
"types": "types/index.d.ts",
|
|
91
|
+
"exports": {
|
|
92
|
+
".": {
|
|
93
|
+
"types": "./types/index.d.ts",
|
|
94
|
+
"browser": "./browser/index.js",
|
|
95
|
+
"import": "./esm/index.js",
|
|
96
|
+
"require": "./cjs/index.js",
|
|
97
|
+
"umd": "./umd/index.js",
|
|
98
|
+
"default": "./cjs/index.js"
|
|
99
|
+
},
|
|
100
|
+
"./package.json": "./package.json"
|
|
101
|
+
},
|
|
102
|
+
"dependencies": {
|
|
103
|
+
"@tsparticles/engine": "^3.0.0-beta.4",
|
|
104
|
+
"@tsparticles/plugin-emitters": "^3.0.0-beta.4"
|
|
105
|
+
},
|
|
106
|
+
"publishConfig": {
|
|
107
|
+
"access": "public"
|
|
108
|
+
}
|
|
109
|
+
}
|