@tsparticles/plugin-emitters-shape-polygon 3.9.1 → 4.0.0-alpha.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/595.min.js ADDED
@@ -0,0 +1,2 @@
1
+ /*! For license information please see 595.min.js.LICENSE.txt */
2
+ (this.webpackChunk_tsparticles_plugin_emitters_shape_polygon=this.webpackChunk_tsparticles_plugin_emitters_shape_polygon||[]).push([[595],{595(t,s,n){n.d(s,{EmittersPolygonShapeGenerator:()=>y});var e=n(526),i=n(303);const o=2*Math.PI;function l(t,s,n,e=0){const i=[],l=o/s;for(let o=0;o<s;o++){const s=l*o+e;i.push({x:t.x+n*Math.cos(s),y:t.y+n*Math.sin(s)})}return i}function r(t,s){let n=!1;for(let e=0,i=s.length-1;e<s.length;i=e++){const o=s[e],l=s[i];if(!o||!l)continue;o.y>t.y!=l.y>t.y&&t.x<(l.x-o.x)*(t.y-o.y)/(l.y-o.y)+o.x&&(n=!n)}return n}class a extends e.EmitterShapeBase{constructor(t,s,n,e){super(t,s,n,e),this.sides=e.sides,this.angle=(0,i.degToRad)(e.angle),this.polygon=l(t,this.sides,.5*s.width,this.angle)}async init(){}randomPosition(){const t=this.fill,s=this.polygon,n=t?function(t){const s=t[0];if(!s)return null;const n={...s},e={...s};for(const s of t)s.x<n.x&&(n.x=s.x),s.x>e.x&&(e.x=s.x),s.y<n.y&&(n.y=s.y),s.y>e.y&&(e.y=s.y);let o=null;for(let s=0;s<100;s++){const s={x:n.x+(0,i.getRandom)()*(e.x-n.x),y:n.y+(0,i.getRandom)()*(e.y-n.y)};if(r(s,t)){o=s;break}}return o}(s):function(t){const s=Math.floor((0,i.getRandom)()*t.length),n=t[s];if(!n)return;const e=t[(s+1)%t.length];if(!e)return;const o=(0,i.getRandom)();return{x:n.x+(e.x-n.x)*o,y:n.y+(e.y-n.y)*o}}(s);return n?{position:n}:null}resize(t,s){super.resize(t,s),this.polygon=l(t,this.sides,.5*s.width,this.angle)}}class h{constructor(){this.angle=0,this.sides=5}load(t){(0,i.isNull)(t)||(void 0!==t.angle&&(this.angle=t.angle),void 0!==t.sides&&(this.sides=t.sides))}}class y{generate(t,s,n,e){const i=new h;return i.load(e),new a(t,s,n,i)}}}}]);
@@ -0,0 +1 @@
1
+ /*! tsParticles Emitters Shape Polygon Plugin v4.0.0-alpha.0 by Matteo Bruni */
package/browser/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { EmittersPolygonShapeGenerator } from "./EmittersPolygonShapeGenerator.js";
2
- export async function loadEmittersShapePolygon(engine, refresh = true) {
3
- const emittersEngine = engine;
4
- emittersEngine.checkVersion("3.9.1");
5
- emittersEngine.addEmitterShapeGenerator?.("polygon", new EmittersPolygonShapeGenerator());
6
- await emittersEngine.refresh(refresh);
1
+ export function loadEmittersShapePolygon(engine) {
2
+ engine.checkVersion("4.0.0-alpha.0");
3
+ engine.register(async (emittersEngine) => {
4
+ const { EmittersPolygonShapeGenerator } = await import("./EmittersPolygonShapeGenerator.js");
5
+ emittersEngine.addEmitterShapeGenerator?.("polygon", new EmittersPolygonShapeGenerator());
6
+ });
7
7
  }
package/browser/utils.js CHANGED
@@ -12,7 +12,11 @@ export function generateRandomPolygon(position, sides, radius, rotationAngle = d
12
12
  return polygon;
13
13
  }
14
14
  export function generateRandomPointWithinPolygon(polygon) {
15
- const firstIndex = 0, firstPoint = polygon[firstIndex], min = { ...firstPoint }, max = { ...firstPoint };
15
+ const firstIndex = 0, firstPoint = polygon[firstIndex];
16
+ if (!firstPoint) {
17
+ return null;
18
+ }
19
+ const min = { ...firstPoint }, max = { ...firstPoint };
16
20
  for (const point of polygon) {
17
21
  if (point.x < min.x) {
18
22
  min.x = point.x;
@@ -41,7 +45,15 @@ export function generateRandomPointWithinPolygon(polygon) {
41
45
  return randomPoint;
42
46
  }
43
47
  export function generateRandomPointOnPolygonPerimeter(polygon) {
44
- const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex], offset = 1, endPoint = polygon[(sideIndex + offset) % polygon.length], t = getRandom();
48
+ const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex];
49
+ if (!startPoint) {
50
+ return;
51
+ }
52
+ const offset = 1, endPoint = polygon[(sideIndex + offset) % polygon.length];
53
+ if (!endPoint) {
54
+ return;
55
+ }
56
+ const t = getRandom();
45
57
  return { x: startPoint.x + (endPoint.x - startPoint.x) * t, y: startPoint.y + (endPoint.y - startPoint.y) * t };
46
58
  }
47
59
  export function isPointInPolygon(point, polygon) {
@@ -49,6 +61,9 @@ export function isPointInPolygon(point, polygon) {
49
61
  const offset = 1;
50
62
  for (let i = 0, j = polygon.length - offset; i < polygon.length; j = i++) {
51
63
  const pi = polygon[i], pj = polygon[j];
64
+ if (!pi || !pj) {
65
+ continue;
66
+ }
52
67
  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;
53
68
  if (intersect) {
54
69
  inside = !inside;
@@ -1,26 +1,22 @@
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 engine_1 = require("@tsparticles/engine");
6
- const utils_js_1 = require("./utils.js");
1
+ import { EmitterShapeBase } from "@tsparticles/plugin-emitters";
2
+ import { degToRad } from "@tsparticles/engine";
3
+ import { generateRandomPointOnPolygonPerimeter, generateRandomPointWithinPolygon, generateRandomPolygon, } from "./utils.js";
7
4
  const half = 0.5;
8
- class EmittersPolygonShape extends plugin_emitters_1.EmitterShapeBase {
5
+ export class EmittersPolygonShape extends EmitterShapeBase {
9
6
  constructor(position, size, fill, options) {
10
7
  super(position, size, fill, options);
11
8
  this.sides = options.sides;
12
- this.angle = (0, engine_1.degToRad)(options.angle);
13
- this.polygon = (0, utils_js_1.generateRandomPolygon)(position, this.sides, size.width * half, this.angle);
9
+ this.angle = degToRad(options.angle);
10
+ this.polygon = generateRandomPolygon(position, this.sides, size.width * half, this.angle);
14
11
  }
15
12
  async init() {
16
13
  }
17
14
  randomPosition() {
18
- const fill = this.fill, polygon = this.polygon, res = fill ? (0, utils_js_1.generateRandomPointWithinPolygon)(polygon) : (0, utils_js_1.generateRandomPointOnPolygonPerimeter)(polygon);
15
+ const fill = this.fill, polygon = this.polygon, res = fill ? generateRandomPointWithinPolygon(polygon) : generateRandomPointOnPolygonPerimeter(polygon);
19
16
  return res ? { position: res } : null;
20
17
  }
21
18
  resize(position, size) {
22
19
  super.resize(position, size);
23
- this.polygon = (0, utils_js_1.generateRandomPolygon)(position, this.sides, size.width * half, this.angle);
20
+ this.polygon = generateRandomPolygon(position, this.sides, size.width * half, this.angle);
24
21
  }
25
22
  }
26
- exports.EmittersPolygonShape = EmittersPolygonShape;
@@ -1,13 +1,9 @@
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 {
1
+ import { EmittersPolygonShape } from "./EmittersPolygonShape.js";
2
+ import { EmittersPolygonShapeOptions } from "./Options/Classes/EmittersPolygonShapeOptions.js";
3
+ export class EmittersPolygonShapeGenerator {
7
4
  generate(position, size, fill, options) {
8
- const shapeOptions = new EmittersPolygonShapeOptions_js_1.EmittersPolygonShapeOptions();
5
+ const shapeOptions = new EmittersPolygonShapeOptions();
9
6
  shapeOptions.load(options);
10
- return new EmittersPolygonShape_js_1.EmittersPolygonShape(position, size, fill, shapeOptions);
7
+ return new EmittersPolygonShape(position, size, fill, shapeOptions);
11
8
  }
12
9
  }
13
- exports.EmittersPolygonShapeGenerator = EmittersPolygonShapeGenerator;
@@ -1,14 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EmittersPolygonShapeOptions = void 0;
4
- const engine_1 = require("@tsparticles/engine");
5
- class EmittersPolygonShapeOptions {
1
+ import { isNull } from "@tsparticles/engine";
2
+ export class EmittersPolygonShapeOptions {
6
3
  constructor() {
7
4
  this.angle = 0;
8
5
  this.sides = 5;
9
6
  }
10
7
  load(data) {
11
- if ((0, engine_1.isNull)(data)) {
8
+ if (isNull(data)) {
12
9
  return;
13
10
  }
14
11
  if (data.angle !== undefined) {
@@ -19,4 +16,3 @@ class EmittersPolygonShapeOptions {
19
16
  }
20
17
  }
21
18
  }
22
- exports.EmittersPolygonShapeOptions = EmittersPolygonShapeOptions;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/cjs/index.js CHANGED
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadEmittersShapePolygon = loadEmittersShapePolygon;
4
- const EmittersPolygonShapeGenerator_js_1 = require("./EmittersPolygonShapeGenerator.js");
5
- async function loadEmittersShapePolygon(engine, refresh = true) {
6
- const emittersEngine = engine;
7
- emittersEngine.checkVersion("3.9.1");
8
- emittersEngine.addEmitterShapeGenerator?.("polygon", new EmittersPolygonShapeGenerator_js_1.EmittersPolygonShapeGenerator());
9
- await emittersEngine.refresh(refresh);
1
+ export function loadEmittersShapePolygon(engine) {
2
+ engine.checkVersion("4.0.0-alpha.0");
3
+ engine.register(async (emittersEngine) => {
4
+ const { EmittersPolygonShapeGenerator } = await import("./EmittersPolygonShapeGenerator.js");
5
+ emittersEngine.addEmitterShapeGenerator?.("polygon", new EmittersPolygonShapeGenerator());
6
+ });
10
7
  }
package/cjs/utils.js CHANGED
@@ -1,12 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateRandomPolygon = generateRandomPolygon;
4
- exports.generateRandomPointWithinPolygon = generateRandomPointWithinPolygon;
5
- exports.generateRandomPointOnPolygonPerimeter = generateRandomPointOnPolygonPerimeter;
6
- exports.isPointInPolygon = isPointInPolygon;
7
- const engine_1 = require("@tsparticles/engine");
1
+ import { getRandom } from "@tsparticles/engine";
8
2
  const double = 2, doublePI = Math.PI * double, defaultRotation = 0, maxAttempts = 100;
9
- function generateRandomPolygon(position, sides, radius, rotationAngle = defaultRotation) {
3
+ export function generateRandomPolygon(position, sides, radius, rotationAngle = defaultRotation) {
10
4
  const polygon = [], angle = doublePI / sides;
11
5
  for (let i = 0; i < sides; i++) {
12
6
  const currentAngle = angle * i + rotationAngle;
@@ -17,8 +11,12 @@ function generateRandomPolygon(position, sides, radius, rotationAngle = defaultR
17
11
  }
18
12
  return polygon;
19
13
  }
20
- function generateRandomPointWithinPolygon(polygon) {
21
- const firstIndex = 0, firstPoint = polygon[firstIndex], min = { ...firstPoint }, max = { ...firstPoint };
14
+ export function generateRandomPointWithinPolygon(polygon) {
15
+ const firstIndex = 0, firstPoint = polygon[firstIndex];
16
+ if (!firstPoint) {
17
+ return null;
18
+ }
19
+ const min = { ...firstPoint }, max = { ...firstPoint };
22
20
  for (const point of polygon) {
23
21
  if (point.x < min.x) {
24
22
  min.x = point.x;
@@ -36,8 +34,8 @@ function generateRandomPointWithinPolygon(polygon) {
36
34
  let randomPoint = null;
37
35
  for (let attempts = 0; attempts < maxAttempts; attempts++) {
38
36
  const tmpPoint = {
39
- x: min.x + (0, engine_1.getRandom)() * (max.x - min.x),
40
- y: min.y + (0, engine_1.getRandom)() * (max.y - min.y),
37
+ x: min.x + getRandom() * (max.x - min.x),
38
+ y: min.y + getRandom() * (max.y - min.y),
41
39
  };
42
40
  if (isPointInPolygon(tmpPoint, polygon)) {
43
41
  randomPoint = tmpPoint;
@@ -46,15 +44,26 @@ function generateRandomPointWithinPolygon(polygon) {
46
44
  }
47
45
  return randomPoint;
48
46
  }
49
- function generateRandomPointOnPolygonPerimeter(polygon) {
50
- const sideIndex = Math.floor((0, engine_1.getRandom)() * polygon.length), startPoint = polygon[sideIndex], offset = 1, endPoint = polygon[(sideIndex + offset) % polygon.length], t = (0, engine_1.getRandom)();
47
+ export function generateRandomPointOnPolygonPerimeter(polygon) {
48
+ const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex];
49
+ if (!startPoint) {
50
+ return;
51
+ }
52
+ const offset = 1, endPoint = polygon[(sideIndex + offset) % polygon.length];
53
+ if (!endPoint) {
54
+ return;
55
+ }
56
+ const t = getRandom();
51
57
  return { x: startPoint.x + (endPoint.x - startPoint.x) * t, y: startPoint.y + (endPoint.y - startPoint.y) * t };
52
58
  }
53
- function isPointInPolygon(point, polygon) {
59
+ export function isPointInPolygon(point, polygon) {
54
60
  let inside = false;
55
61
  const offset = 1;
56
62
  for (let i = 0, j = polygon.length - offset; i < polygon.length; j = i++) {
57
63
  const pi = polygon[i], pj = polygon[j];
64
+ if (!pi || !pj) {
65
+ continue;
66
+ }
58
67
  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;
59
68
  if (intersect) {
60
69
  inside = !inside;
@@ -0,0 +1,60 @@
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
+ * v4.0.0-alpha.0
8
+ */
9
+ "use strict";
10
+ /*
11
+ * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
12
+ * This devtool is neither made for production nor for readable output files.
13
+ * It uses "eval()" calls to create a separate source file in the browser devtools.
14
+ * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
15
+ * or disable the default devtool with "devtool: false".
16
+ * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
17
+ */
18
+ (this["webpackChunk_tsparticles_plugin_emitters_shape_polygon"] = this["webpackChunk_tsparticles_plugin_emitters_shape_polygon"] || []).push([["dist_browser_EmittersPolygonShapeGenerator_js"],{
19
+
20
+ /***/ "./dist/browser/EmittersPolygonShape.js"
21
+ /*!**********************************************!*\
22
+ !*** ./dist/browser/EmittersPolygonShape.js ***!
23
+ \**********************************************/
24
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
25
+
26
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ EmittersPolygonShape: () => (/* binding */ EmittersPolygonShape)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_plugin_emitters__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/plugin-emitters */ \"@tsparticles/plugin-emitters\");\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils.js */ \"./dist/browser/utils.js\");\n\n\n\nconst half = 0.5;\nclass EmittersPolygonShape extends _tsparticles_plugin_emitters__WEBPACK_IMPORTED_MODULE_0__.EmitterShapeBase {\n constructor(position, size, fill, options) {\n super(position, size, fill, options);\n this.sides = options.sides;\n this.angle = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.degToRad)(options.angle);\n this.polygon = (0,_utils_js__WEBPACK_IMPORTED_MODULE_2__.generateRandomPolygon)(position, this.sides, size.width * half, this.angle);\n }\n async init() {}\n randomPosition() {\n const fill = this.fill,\n polygon = this.polygon,\n res = fill ? (0,_utils_js__WEBPACK_IMPORTED_MODULE_2__.generateRandomPointWithinPolygon)(polygon) : (0,_utils_js__WEBPACK_IMPORTED_MODULE_2__.generateRandomPointOnPolygonPerimeter)(polygon);\n return res ? {\n position: res\n } : null;\n }\n resize(position, size) {\n super.resize(position, size);\n this.polygon = (0,_utils_js__WEBPACK_IMPORTED_MODULE_2__.generateRandomPolygon)(position, this.sides, size.width * half, this.angle);\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-emitters-shape-polygon/./dist/browser/EmittersPolygonShape.js?\n}");
27
+
28
+ /***/ },
29
+
30
+ /***/ "./dist/browser/EmittersPolygonShapeGenerator.js"
31
+ /*!*******************************************************!*\
32
+ !*** ./dist/browser/EmittersPolygonShapeGenerator.js ***!
33
+ \*******************************************************/
34
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
35
+
36
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ EmittersPolygonShapeGenerator: () => (/* binding */ EmittersPolygonShapeGenerator)\n/* harmony export */ });\n/* harmony import */ var _EmittersPolygonShape_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./EmittersPolygonShape.js */ \"./dist/browser/EmittersPolygonShape.js\");\n/* harmony import */ var _Options_Classes_EmittersPolygonShapeOptions_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./Options/Classes/EmittersPolygonShapeOptions.js */ \"./dist/browser/Options/Classes/EmittersPolygonShapeOptions.js\");\n\n\nclass EmittersPolygonShapeGenerator {\n generate(position, size, fill, options) {\n const shapeOptions = new _Options_Classes_EmittersPolygonShapeOptions_js__WEBPACK_IMPORTED_MODULE_1__.EmittersPolygonShapeOptions();\n shapeOptions.load(options);\n return new _EmittersPolygonShape_js__WEBPACK_IMPORTED_MODULE_0__.EmittersPolygonShape(position, size, fill, shapeOptions);\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-emitters-shape-polygon/./dist/browser/EmittersPolygonShapeGenerator.js?\n}");
37
+
38
+ /***/ },
39
+
40
+ /***/ "./dist/browser/Options/Classes/EmittersPolygonShapeOptions.js"
41
+ /*!*********************************************************************!*\
42
+ !*** ./dist/browser/Options/Classes/EmittersPolygonShapeOptions.js ***!
43
+ \*********************************************************************/
44
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
45
+
46
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ EmittersPolygonShapeOptions: () => (/* binding */ EmittersPolygonShapeOptions)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nclass EmittersPolygonShapeOptions {\n constructor() {\n this.angle = 0;\n this.sides = 5;\n }\n load(data) {\n if ((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.isNull)(data)) {\n return;\n }\n if (data.angle !== undefined) {\n this.angle = data.angle;\n }\n if (data.sides !== undefined) {\n this.sides = data.sides;\n }\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-emitters-shape-polygon/./dist/browser/Options/Classes/EmittersPolygonShapeOptions.js?\n}");
47
+
48
+ /***/ },
49
+
50
+ /***/ "./dist/browser/utils.js"
51
+ /*!*******************************!*\
52
+ !*** ./dist/browser/utils.js ***!
53
+ \*******************************/
54
+ (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
55
+
56
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ generateRandomPointOnPolygonPerimeter: () => (/* binding */ generateRandomPointOnPolygonPerimeter),\n/* harmony export */ generateRandomPointWithinPolygon: () => (/* binding */ generateRandomPointWithinPolygon),\n/* harmony export */ generateRandomPolygon: () => (/* binding */ generateRandomPolygon),\n/* harmony export */ isPointInPolygon: () => (/* binding */ isPointInPolygon)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nconst double = 2,\n doublePI = Math.PI * double,\n defaultRotation = 0,\n maxAttempts = 100;\nfunction generateRandomPolygon(position, sides, radius, rotationAngle = defaultRotation) {\n const polygon = [],\n angle = doublePI / sides;\n for (let i = 0; i < sides; i++) {\n const currentAngle = angle * i + rotationAngle;\n polygon.push({\n x: position.x + radius * Math.cos(currentAngle),\n y: position.y + radius * Math.sin(currentAngle)\n });\n }\n return polygon;\n}\nfunction generateRandomPointWithinPolygon(polygon) {\n const firstIndex = 0,\n firstPoint = polygon[firstIndex];\n if (!firstPoint) {\n return null;\n }\n const min = {\n ...firstPoint\n },\n max = {\n ...firstPoint\n };\n for (const point of polygon) {\n if (point.x < min.x) {\n min.x = point.x;\n }\n if (point.x > max.x) {\n max.x = point.x;\n }\n if (point.y < min.y) {\n min.y = point.y;\n }\n if (point.y > max.y) {\n max.y = point.y;\n }\n }\n let randomPoint = null;\n for (let attempts = 0; attempts < maxAttempts; attempts++) {\n const tmpPoint = {\n x: min.x + (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() * (max.x - min.x),\n y: min.y + (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() * (max.y - min.y)\n };\n if (isPointInPolygon(tmpPoint, polygon)) {\n randomPoint = tmpPoint;\n break;\n }\n }\n return randomPoint;\n}\nfunction generateRandomPointOnPolygonPerimeter(polygon) {\n const sideIndex = Math.floor((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() * polygon.length),\n startPoint = polygon[sideIndex];\n if (!startPoint) {\n return;\n }\n const offset = 1,\n endPoint = polygon[(sideIndex + offset) % polygon.length];\n if (!endPoint) {\n return;\n }\n const t = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)();\n return {\n x: startPoint.x + (endPoint.x - startPoint.x) * t,\n y: startPoint.y + (endPoint.y - startPoint.y) * t\n };\n}\nfunction isPointInPolygon(point, polygon) {\n let inside = false;\n const offset = 1;\n for (let i = 0, j = polygon.length - offset; i < polygon.length; j = i++) {\n const pi = polygon[i],\n pj = polygon[j];\n if (!pi || !pj) {\n continue;\n }\n 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;\n if (intersect) {\n inside = !inside;\n }\n }\n return inside;\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-emitters-shape-polygon/./dist/browser/utils.js?\n}");
57
+
58
+ /***/ }
59
+
60
+ }]);
package/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { EmittersPolygonShapeGenerator } from "./EmittersPolygonShapeGenerator.js";
2
- export async function loadEmittersShapePolygon(engine, refresh = true) {
3
- const emittersEngine = engine;
4
- emittersEngine.checkVersion("3.9.1");
5
- emittersEngine.addEmitterShapeGenerator?.("polygon", new EmittersPolygonShapeGenerator());
6
- await emittersEngine.refresh(refresh);
1
+ export function loadEmittersShapePolygon(engine) {
2
+ engine.checkVersion("4.0.0-alpha.0");
3
+ engine.register(async (emittersEngine) => {
4
+ const { EmittersPolygonShapeGenerator } = await import("./EmittersPolygonShapeGenerator.js");
5
+ emittersEngine.addEmitterShapeGenerator?.("polygon", new EmittersPolygonShapeGenerator());
6
+ });
7
7
  }
package/esm/utils.js CHANGED
@@ -12,7 +12,11 @@ export function generateRandomPolygon(position, sides, radius, rotationAngle = d
12
12
  return polygon;
13
13
  }
14
14
  export function generateRandomPointWithinPolygon(polygon) {
15
- const firstIndex = 0, firstPoint = polygon[firstIndex], min = { ...firstPoint }, max = { ...firstPoint };
15
+ const firstIndex = 0, firstPoint = polygon[firstIndex];
16
+ if (!firstPoint) {
17
+ return null;
18
+ }
19
+ const min = { ...firstPoint }, max = { ...firstPoint };
16
20
  for (const point of polygon) {
17
21
  if (point.x < min.x) {
18
22
  min.x = point.x;
@@ -41,7 +45,15 @@ export function generateRandomPointWithinPolygon(polygon) {
41
45
  return randomPoint;
42
46
  }
43
47
  export function generateRandomPointOnPolygonPerimeter(polygon) {
44
- const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex], offset = 1, endPoint = polygon[(sideIndex + offset) % polygon.length], t = getRandom();
48
+ const sideIndex = Math.floor(getRandom() * polygon.length), startPoint = polygon[sideIndex];
49
+ if (!startPoint) {
50
+ return;
51
+ }
52
+ const offset = 1, endPoint = polygon[(sideIndex + offset) % polygon.length];
53
+ if (!endPoint) {
54
+ return;
55
+ }
56
+ const t = getRandom();
45
57
  return { x: startPoint.x + (endPoint.x - startPoint.x) * t, y: startPoint.y + (endPoint.y - startPoint.y) * t };
46
58
  }
47
59
  export function isPointInPolygon(point, polygon) {
@@ -49,6 +61,9 @@ export function isPointInPolygon(point, polygon) {
49
61
  const offset = 1;
50
62
  for (let i = 0, j = polygon.length - offset; i < polygon.length; j = i++) {
51
63
  const pi = polygon[i], pj = polygon[j];
64
+ if (!pi || !pj) {
65
+ continue;
66
+ }
52
67
  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;
53
68
  if (intersect) {
54
69
  inside = !inside;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/plugin-emitters-shape-polygon",
3
- "version": "3.9.1",
3
+ "version": "4.0.0-alpha.0",
4
4
  "description": "tsParticles emitters shape polygon plugin",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -100,10 +100,11 @@
100
100
  "./package.json": "./package.json"
101
101
  },
102
102
  "dependencies": {
103
- "@tsparticles/engine": "3.9.1",
104
- "@tsparticles/plugin-emitters": "3.9.1"
103
+ "@tsparticles/engine": "4.0.0-alpha.0",
104
+ "@tsparticles/plugin-emitters": "4.0.0-alpha.0"
105
105
  },
106
106
  "publishConfig": {
107
107
  "access": "public"
108
- }
108
+ },
109
+ "type": "module"
109
110
  }