@tsparticles/plugin-emitters-shape-polygon 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/EmittersPolygonShape.js +20 -0
- package/browser/EmittersPolygonShapeGenerator.js +9 -0
- package/browser/Options/Classes/EmittersPolygonShapeOptions.js +17 -0
- package/browser/Options/Interfaces/IEmittersPolygonShapeOptions.js +1 -0
- package/browser/index.js +7 -0
- package/browser/package.json +1 -0
- package/browser/utils.js +56 -0
- package/cjs/EmittersPolygonShape.js +24 -0
- package/cjs/EmittersPolygonShapeGenerator.js +13 -0
- package/cjs/Options/Classes/EmittersPolygonShapeOptions.js +21 -0
- package/cjs/Options/Interfaces/IEmittersPolygonShapeOptions.js +2 -0
- package/cjs/index.js +11 -0
- package/cjs/package.json +1 -0
- package/cjs/utils.js +63 -0
- package/esm/EmittersPolygonShape.js +20 -0
- package/esm/EmittersPolygonShapeGenerator.js +9 -0
- package/esm/Options/Classes/EmittersPolygonShapeOptions.js +17 -0
- package/esm/Options/Interfaces/IEmittersPolygonShapeOptions.js +1 -0
- package/esm/index.js +7 -0
- package/esm/package.json +1 -0
- package/esm/utils.js +56 -0
- package/package.json +109 -0
- package/report.html +39 -0
- package/tsparticles.plugin.emitters.shape.polygon.js +242 -0
- package/tsparticles.plugin.emitters.shape.polygon.min.js +2 -0
- package/tsparticles.plugin.emitters.shape.polygon.min.js.LICENSE.txt +1 -0
- package/types/EmittersPolygonShape.d.ts +12 -0
- package/types/EmittersPolygonShapeGenerator.d.ts +6 -0
- package/types/Options/Classes/EmittersPolygonShapeOptions.d.ts +8 -0
- package/types/Options/Interfaces/IEmittersPolygonShapeOptions.d.ts +4 -0
- package/types/index.d.ts +2 -0
- package/types/utils.d.ts +5 -0
- package/umd/EmittersPolygonShape.js +34 -0
- package/umd/EmittersPolygonShapeGenerator.js +23 -0
- package/umd/Options/Classes/EmittersPolygonShapeOptions.js +31 -0
- package/umd/Options/Interfaces/IEmittersPolygonShapeOptions.js +12 -0
- package/umd/index.js +21 -0
- package/umd/utils.js +73 -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 Polygon Plugin
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/@tsparticles/plugin-emitters-shape-polygon)
|
|
6
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-emitters-shape-polygon)
|
|
7
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-emitters-shape-polygon) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/tsparticles/tsparticles) plugin for adding the emitters shape polygon 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.polygon.min.js` file will export the function to load the plugin:
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
loadEmittersShapePolygonPlugin
|
|
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 loadEmittersShapePolygonPlugin(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-polygon
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
or
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
$ yarn add @tsparticles/plugin-emitters-shape-polygon
|
|
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 { loadEmittersShapePolygonPlugin } = require("@tsparticles/plugin-emitters-shape-polygon");
|
|
61
|
+
|
|
62
|
+
(async () => {
|
|
63
|
+
await loadEmittersPlugin(tsParticles);
|
|
64
|
+
await loadEmittersShapePolygonPlugin(tsParticles);
|
|
65
|
+
})();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
or
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
72
|
+
import { loadEmittersPlugin } from "@tsparticles/plugin-emitters";
|
|
73
|
+
import { loadEmittersShapePolygonPlugin } from "@tsparticles/plugin-emitters-shape-polygon";
|
|
74
|
+
|
|
75
|
+
(async () => {
|
|
76
|
+
await loadEmittersPlugin(tsParticles);
|
|
77
|
+
await loadEmittersShapePolygonPlugin(tsParticles);
|
|
78
|
+
})();
|
|
79
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EmitterShapeBase } from "@tsparticles/plugin-emitters";
|
|
2
|
+
import { generateRandomPointOnPolygonPerimeter, generateRandomPointWithinPolygon, generateRandomPolygon, } from "./utils.js";
|
|
3
|
+
export class EmittersPolygonShape extends EmitterShapeBase {
|
|
4
|
+
constructor(position, size, fill, options) {
|
|
5
|
+
super(position, size, fill, options);
|
|
6
|
+
this.sides = options.sides;
|
|
7
|
+
this.angle = (options.angle * Math.PI) / 180;
|
|
8
|
+
this.polygon = generateRandomPolygon(position, this.sides, size.width / 2, this.angle);
|
|
9
|
+
}
|
|
10
|
+
async init() {
|
|
11
|
+
}
|
|
12
|
+
async randomPosition() {
|
|
13
|
+
const fill = this.fill, polygon = this.polygon, res = fill ? generateRandomPointWithinPolygon(polygon) : generateRandomPointOnPolygonPerimeter(polygon);
|
|
14
|
+
return res ? { position: res } : null;
|
|
15
|
+
}
|
|
16
|
+
resize(position, size) {
|
|
17
|
+
super.resize(position, size);
|
|
18
|
+
this.polygon = generateRandomPolygon(position, this.sides, size.width / 2, this.angle);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EmittersPolygonShape } from "./EmittersPolygonShape.js";
|
|
2
|
+
import { EmittersPolygonShapeOptions } from "./Options/Classes/EmittersPolygonShapeOptions.js";
|
|
3
|
+
export class EmittersPolygonShapeGenerator {
|
|
4
|
+
generate(position, size, fill, options) {
|
|
5
|
+
const shapeOptions = new EmittersPolygonShapeOptions();
|
|
6
|
+
shapeOptions.load(options);
|
|
7
|
+
return new EmittersPolygonShape(position, size, fill, shapeOptions);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class EmittersPolygonShapeOptions {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.angle = 0;
|
|
4
|
+
this.sides = 5;
|
|
5
|
+
}
|
|
6
|
+
load(data) {
|
|
7
|
+
if (!data) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (data.angle !== undefined) {
|
|
11
|
+
this.angle = data.angle;
|
|
12
|
+
}
|
|
13
|
+
if (data.sides !== undefined) {
|
|
14
|
+
this.sides = data.sides;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EmittersPolygonShapeGenerator } from "./EmittersPolygonShapeGenerator.js";
|
|
2
|
+
export async function loadEmittersShapePolygon(engine, refresh = true) {
|
|
3
|
+
const emittersEngine = engine;
|
|
4
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
5
|
+
emittersEngine.addEmitterShapeGenerator("polygon", new EmittersPolygonShapeGenerator());
|
|
6
|
+
await emittersEngine.refresh(refresh);
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
package/browser/utils.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { getRandom } from "@tsparticles/engine";
|
|
2
|
+
export function generateRandomPolygon(position, sides, radius, rotationAngle = 0) {
|
|
3
|
+
const polygon = [], angle = (Math.PI * 2) / sides;
|
|
4
|
+
for (let i = 0; i < sides; i++) {
|
|
5
|
+
const currentAngle = angle * i + rotationAngle;
|
|
6
|
+
polygon.push({
|
|
7
|
+
x: position.x + radius * Math.cos(currentAngle),
|
|
8
|
+
y: position.y + radius * Math.sin(currentAngle),
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return polygon;
|
|
12
|
+
}
|
|
13
|
+
export function generateRandomPointWithinPolygon(polygon) {
|
|
14
|
+
const min = { ...polygon[0] }, max = { ...polygon[0] };
|
|
15
|
+
for (const point of polygon) {
|
|
16
|
+
if (point.x < min.x) {
|
|
17
|
+
min.x = point.x;
|
|
18
|
+
}
|
|
19
|
+
if (point.x > max.x) {
|
|
20
|
+
max.x = point.x;
|
|
21
|
+
}
|
|
22
|
+
if (point.y < min.y) {
|
|
23
|
+
min.y = point.y;
|
|
24
|
+
}
|
|
25
|
+
if (point.y > max.y) {
|
|
26
|
+
max.y = point.y;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let randomPoint = null;
|
|
30
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
31
|
+
const tmpPoint = {
|
|
32
|
+
x: min.x + getRandom() * (max.x - min.x),
|
|
33
|
+
y: min.y + getRandom() * (max.y - min.y),
|
|
34
|
+
};
|
|
35
|
+
if (isPointInPolygon(tmpPoint, polygon)) {
|
|
36
|
+
randomPoint = tmpPoint;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return randomPoint;
|
|
41
|
+
}
|
|
42
|
+
export function generateRandomPointOnPolygonPerimeter(polygon) {
|
|
43
|
+
const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex], endPoint = polygon[(sideIndex + 1) % polygon.length], t = getRandom();
|
|
44
|
+
return { x: startPoint.x + (endPoint.x - startPoint.x) * t, y: startPoint.y + (endPoint.y - startPoint.y) * t };
|
|
45
|
+
}
|
|
46
|
+
export function isPointInPolygon(point, polygon) {
|
|
47
|
+
let inside = false;
|
|
48
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
49
|
+
const pi = polygon[i], pj = polygon[j];
|
|
50
|
+
const intersect = pi.y > point.y !== pj.y > point.y && point.x < ((pj.x - pi.x) * (point.y - pi.y)) / (pj.y - pi.y) + pi.x;
|
|
51
|
+
if (intersect) {
|
|
52
|
+
inside = !inside;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return inside;
|
|
56
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmittersPolygonShape = void 0;
|
|
4
|
+
const plugin_emitters_1 = require("@tsparticles/plugin-emitters");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
class EmittersPolygonShape extends plugin_emitters_1.EmitterShapeBase {
|
|
7
|
+
constructor(position, size, fill, options) {
|
|
8
|
+
super(position, size, fill, options);
|
|
9
|
+
this.sides = options.sides;
|
|
10
|
+
this.angle = (options.angle * Math.PI) / 180;
|
|
11
|
+
this.polygon = (0, utils_js_1.generateRandomPolygon)(position, this.sides, size.width / 2, this.angle);
|
|
12
|
+
}
|
|
13
|
+
async init() {
|
|
14
|
+
}
|
|
15
|
+
async randomPosition() {
|
|
16
|
+
const fill = this.fill, polygon = this.polygon, res = fill ? (0, utils_js_1.generateRandomPointWithinPolygon)(polygon) : (0, utils_js_1.generateRandomPointOnPolygonPerimeter)(polygon);
|
|
17
|
+
return res ? { position: res } : null;
|
|
18
|
+
}
|
|
19
|
+
resize(position, size) {
|
|
20
|
+
super.resize(position, size);
|
|
21
|
+
this.polygon = (0, utils_js_1.generateRandomPolygon)(position, this.sides, size.width / 2, this.angle);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.EmittersPolygonShape = EmittersPolygonShape;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmittersPolygonShapeGenerator = void 0;
|
|
4
|
+
const EmittersPolygonShape_js_1 = require("./EmittersPolygonShape.js");
|
|
5
|
+
const EmittersPolygonShapeOptions_js_1 = require("./Options/Classes/EmittersPolygonShapeOptions.js");
|
|
6
|
+
class EmittersPolygonShapeGenerator {
|
|
7
|
+
generate(position, size, fill, options) {
|
|
8
|
+
const shapeOptions = new EmittersPolygonShapeOptions_js_1.EmittersPolygonShapeOptions();
|
|
9
|
+
shapeOptions.load(options);
|
|
10
|
+
return new EmittersPolygonShape_js_1.EmittersPolygonShape(position, size, fill, shapeOptions);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.EmittersPolygonShapeGenerator = EmittersPolygonShapeGenerator;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmittersPolygonShapeOptions = void 0;
|
|
4
|
+
class EmittersPolygonShapeOptions {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.angle = 0;
|
|
7
|
+
this.sides = 5;
|
|
8
|
+
}
|
|
9
|
+
load(data) {
|
|
10
|
+
if (!data) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (data.angle !== undefined) {
|
|
14
|
+
this.angle = data.angle;
|
|
15
|
+
}
|
|
16
|
+
if (data.sides !== undefined) {
|
|
17
|
+
this.sides = data.sides;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.EmittersPolygonShapeOptions = EmittersPolygonShapeOptions;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadEmittersShapePolygon = void 0;
|
|
4
|
+
const EmittersPolygonShapeGenerator_js_1 = require("./EmittersPolygonShapeGenerator.js");
|
|
5
|
+
async function loadEmittersShapePolygon(engine, refresh = true) {
|
|
6
|
+
const emittersEngine = engine;
|
|
7
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
8
|
+
emittersEngine.addEmitterShapeGenerator("polygon", new EmittersPolygonShapeGenerator_js_1.EmittersPolygonShapeGenerator());
|
|
9
|
+
await emittersEngine.refresh(refresh);
|
|
10
|
+
}
|
|
11
|
+
exports.loadEmittersShapePolygon = loadEmittersShapePolygon;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isPointInPolygon = exports.generateRandomPointOnPolygonPerimeter = exports.generateRandomPointWithinPolygon = exports.generateRandomPolygon = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
function generateRandomPolygon(position, sides, radius, rotationAngle = 0) {
|
|
6
|
+
const polygon = [], angle = (Math.PI * 2) / sides;
|
|
7
|
+
for (let i = 0; i < sides; i++) {
|
|
8
|
+
const currentAngle = angle * i + rotationAngle;
|
|
9
|
+
polygon.push({
|
|
10
|
+
x: position.x + radius * Math.cos(currentAngle),
|
|
11
|
+
y: position.y + radius * Math.sin(currentAngle),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return polygon;
|
|
15
|
+
}
|
|
16
|
+
exports.generateRandomPolygon = generateRandomPolygon;
|
|
17
|
+
function generateRandomPointWithinPolygon(polygon) {
|
|
18
|
+
const min = { ...polygon[0] }, max = { ...polygon[0] };
|
|
19
|
+
for (const point of polygon) {
|
|
20
|
+
if (point.x < min.x) {
|
|
21
|
+
min.x = point.x;
|
|
22
|
+
}
|
|
23
|
+
if (point.x > max.x) {
|
|
24
|
+
max.x = point.x;
|
|
25
|
+
}
|
|
26
|
+
if (point.y < min.y) {
|
|
27
|
+
min.y = point.y;
|
|
28
|
+
}
|
|
29
|
+
if (point.y > max.y) {
|
|
30
|
+
max.y = point.y;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
let randomPoint = null;
|
|
34
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
35
|
+
const tmpPoint = {
|
|
36
|
+
x: min.x + (0, engine_1.getRandom)() * (max.x - min.x),
|
|
37
|
+
y: min.y + (0, engine_1.getRandom)() * (max.y - min.y),
|
|
38
|
+
};
|
|
39
|
+
if (isPointInPolygon(tmpPoint, polygon)) {
|
|
40
|
+
randomPoint = tmpPoint;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return randomPoint;
|
|
45
|
+
}
|
|
46
|
+
exports.generateRandomPointWithinPolygon = generateRandomPointWithinPolygon;
|
|
47
|
+
function generateRandomPointOnPolygonPerimeter(polygon) {
|
|
48
|
+
const sideIndex = Math.floor((0, engine_1.getRandom)() * polygon.length), startPoint = polygon[sideIndex], endPoint = polygon[(sideIndex + 1) % polygon.length], t = (0, engine_1.getRandom)();
|
|
49
|
+
return { x: startPoint.x + (endPoint.x - startPoint.x) * t, y: startPoint.y + (endPoint.y - startPoint.y) * t };
|
|
50
|
+
}
|
|
51
|
+
exports.generateRandomPointOnPolygonPerimeter = generateRandomPointOnPolygonPerimeter;
|
|
52
|
+
function isPointInPolygon(point, polygon) {
|
|
53
|
+
let inside = false;
|
|
54
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
55
|
+
const pi = polygon[i], pj = polygon[j];
|
|
56
|
+
const intersect = pi.y > point.y !== pj.y > point.y && point.x < ((pj.x - pi.x) * (point.y - pi.y)) / (pj.y - pi.y) + pi.x;
|
|
57
|
+
if (intersect) {
|
|
58
|
+
inside = !inside;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return inside;
|
|
62
|
+
}
|
|
63
|
+
exports.isPointInPolygon = isPointInPolygon;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EmitterShapeBase } from "@tsparticles/plugin-emitters";
|
|
2
|
+
import { generateRandomPointOnPolygonPerimeter, generateRandomPointWithinPolygon, generateRandomPolygon, } from "./utils.js";
|
|
3
|
+
export class EmittersPolygonShape extends EmitterShapeBase {
|
|
4
|
+
constructor(position, size, fill, options) {
|
|
5
|
+
super(position, size, fill, options);
|
|
6
|
+
this.sides = options.sides;
|
|
7
|
+
this.angle = (options.angle * Math.PI) / 180;
|
|
8
|
+
this.polygon = generateRandomPolygon(position, this.sides, size.width / 2, this.angle);
|
|
9
|
+
}
|
|
10
|
+
async init() {
|
|
11
|
+
}
|
|
12
|
+
async randomPosition() {
|
|
13
|
+
const fill = this.fill, polygon = this.polygon, res = fill ? generateRandomPointWithinPolygon(polygon) : generateRandomPointOnPolygonPerimeter(polygon);
|
|
14
|
+
return res ? { position: res } : null;
|
|
15
|
+
}
|
|
16
|
+
resize(position, size) {
|
|
17
|
+
super.resize(position, size);
|
|
18
|
+
this.polygon = generateRandomPolygon(position, this.sides, size.width / 2, this.angle);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EmittersPolygonShape } from "./EmittersPolygonShape.js";
|
|
2
|
+
import { EmittersPolygonShapeOptions } from "./Options/Classes/EmittersPolygonShapeOptions.js";
|
|
3
|
+
export class EmittersPolygonShapeGenerator {
|
|
4
|
+
generate(position, size, fill, options) {
|
|
5
|
+
const shapeOptions = new EmittersPolygonShapeOptions();
|
|
6
|
+
shapeOptions.load(options);
|
|
7
|
+
return new EmittersPolygonShape(position, size, fill, shapeOptions);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class EmittersPolygonShapeOptions {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.angle = 0;
|
|
4
|
+
this.sides = 5;
|
|
5
|
+
}
|
|
6
|
+
load(data) {
|
|
7
|
+
if (!data) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (data.angle !== undefined) {
|
|
11
|
+
this.angle = data.angle;
|
|
12
|
+
}
|
|
13
|
+
if (data.sides !== undefined) {
|
|
14
|
+
this.sides = data.sides;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EmittersPolygonShapeGenerator } from "./EmittersPolygonShapeGenerator.js";
|
|
2
|
+
export async function loadEmittersShapePolygon(engine, refresh = true) {
|
|
3
|
+
const emittersEngine = engine;
|
|
4
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
5
|
+
emittersEngine.addEmitterShapeGenerator("polygon", new EmittersPolygonShapeGenerator());
|
|
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,56 @@
|
|
|
1
|
+
import { getRandom } from "@tsparticles/engine";
|
|
2
|
+
export function generateRandomPolygon(position, sides, radius, rotationAngle = 0) {
|
|
3
|
+
const polygon = [], angle = (Math.PI * 2) / sides;
|
|
4
|
+
for (let i = 0; i < sides; i++) {
|
|
5
|
+
const currentAngle = angle * i + rotationAngle;
|
|
6
|
+
polygon.push({
|
|
7
|
+
x: position.x + radius * Math.cos(currentAngle),
|
|
8
|
+
y: position.y + radius * Math.sin(currentAngle),
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return polygon;
|
|
12
|
+
}
|
|
13
|
+
export function generateRandomPointWithinPolygon(polygon) {
|
|
14
|
+
const min = { ...polygon[0] }, max = { ...polygon[0] };
|
|
15
|
+
for (const point of polygon) {
|
|
16
|
+
if (point.x < min.x) {
|
|
17
|
+
min.x = point.x;
|
|
18
|
+
}
|
|
19
|
+
if (point.x > max.x) {
|
|
20
|
+
max.x = point.x;
|
|
21
|
+
}
|
|
22
|
+
if (point.y < min.y) {
|
|
23
|
+
min.y = point.y;
|
|
24
|
+
}
|
|
25
|
+
if (point.y > max.y) {
|
|
26
|
+
max.y = point.y;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let randomPoint = null;
|
|
30
|
+
for (let attempts = 0; attempts < 100; attempts++) {
|
|
31
|
+
const tmpPoint = {
|
|
32
|
+
x: min.x + getRandom() * (max.x - min.x),
|
|
33
|
+
y: min.y + getRandom() * (max.y - min.y),
|
|
34
|
+
};
|
|
35
|
+
if (isPointInPolygon(tmpPoint, polygon)) {
|
|
36
|
+
randomPoint = tmpPoint;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return randomPoint;
|
|
41
|
+
}
|
|
42
|
+
export function generateRandomPointOnPolygonPerimeter(polygon) {
|
|
43
|
+
const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex], endPoint = polygon[(sideIndex + 1) % polygon.length], t = getRandom();
|
|
44
|
+
return { x: startPoint.x + (endPoint.x - startPoint.x) * t, y: startPoint.y + (endPoint.y - startPoint.y) * t };
|
|
45
|
+
}
|
|
46
|
+
export function isPointInPolygon(point, polygon) {
|
|
47
|
+
let inside = false;
|
|
48
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
49
|
+
const pi = polygon[i], pj = polygon[j];
|
|
50
|
+
const intersect = pi.y > point.y !== pj.y > point.y && point.x < ((pj.x - pi.x) * (point.y - pi.y)) / (pj.y - pi.y) + pi.x;
|
|
51
|
+
if (intersect) {
|
|
52
|
+
inside = !inside;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return inside;
|
|
56
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsparticles/plugin-emitters-shape-polygon",
|
|
3
|
+
"version": "3.0.0-beta.4",
|
|
4
|
+
"description": "tsParticles emitters shape polygon 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/polygon"
|
|
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.polygon.min.js",
|
|
86
|
+
"unpkg": "tsparticles.plugin.emitters.shape.polygon.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
|
+
}
|