@tsparticles/plugin-polygon-mask 3.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.
Files changed (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/Enums/PolygonMaskInlineArrangement.js +1 -0
  4. package/browser/Enums/PolygonMaskMoveType.js +1 -0
  5. package/browser/Enums/PolygonMaskType.js +1 -0
  6. package/browser/Interfaces/ISvgPath.js +1 -0
  7. package/browser/Options/Classes/PolygonMask.js +56 -0
  8. package/browser/Options/Classes/PolygonMaskDraw.js +34 -0
  9. package/browser/Options/Classes/PolygonMaskDrawStroke.js +24 -0
  10. package/browser/Options/Classes/PolygonMaskInline.js +13 -0
  11. package/browser/Options/Classes/PolygonMaskLocalSvg.js +25 -0
  12. package/browser/Options/Classes/PolygonMaskMove.js +17 -0
  13. package/browser/Options/Interfaces/IPolygonMask.js +1 -0
  14. package/browser/Options/Interfaces/IPolygonMaskDraw.js +1 -0
  15. package/browser/Options/Interfaces/IPolygonMaskDrawStroke.js +1 -0
  16. package/browser/Options/Interfaces/IPolygonMaskInline.js +1 -0
  17. package/browser/Options/Interfaces/IPolygonMaskLocalSvg.js +1 -0
  18. package/browser/Options/Interfaces/IPolygonMaskMove.js +1 -0
  19. package/browser/PolygonMaskInstance.js +388 -0
  20. package/browser/index.js +33 -0
  21. package/browser/pathseg.js +1496 -0
  22. package/browser/types.js +1 -0
  23. package/browser/utils.js +108 -0
  24. package/cjs/Enums/PolygonMaskInlineArrangement.js +2 -0
  25. package/cjs/Enums/PolygonMaskMoveType.js +2 -0
  26. package/cjs/Enums/PolygonMaskType.js +2 -0
  27. package/cjs/Interfaces/ISvgPath.js +2 -0
  28. package/cjs/Options/Classes/PolygonMask.js +60 -0
  29. package/cjs/Options/Classes/PolygonMaskDraw.js +38 -0
  30. package/cjs/Options/Classes/PolygonMaskDrawStroke.js +28 -0
  31. package/cjs/Options/Classes/PolygonMaskInline.js +17 -0
  32. package/cjs/Options/Classes/PolygonMaskLocalSvg.js +29 -0
  33. package/cjs/Options/Classes/PolygonMaskMove.js +21 -0
  34. package/cjs/Options/Interfaces/IPolygonMask.js +2 -0
  35. package/cjs/Options/Interfaces/IPolygonMaskDraw.js +2 -0
  36. package/cjs/Options/Interfaces/IPolygonMaskDrawStroke.js +2 -0
  37. package/cjs/Options/Interfaces/IPolygonMaskInline.js +2 -0
  38. package/cjs/Options/Interfaces/IPolygonMaskLocalSvg.js +2 -0
  39. package/cjs/Options/Interfaces/IPolygonMaskMove.js +2 -0
  40. package/cjs/PolygonMaskInstance.js +407 -0
  41. package/cjs/index.js +62 -0
  42. package/cjs/pathseg.js +1496 -0
  43. package/cjs/types.js +2 -0
  44. package/cjs/utils.js +116 -0
  45. package/esm/Enums/PolygonMaskInlineArrangement.js +1 -0
  46. package/esm/Enums/PolygonMaskMoveType.js +1 -0
  47. package/esm/Enums/PolygonMaskType.js +1 -0
  48. package/esm/Interfaces/ISvgPath.js +1 -0
  49. package/esm/Options/Classes/PolygonMask.js +56 -0
  50. package/esm/Options/Classes/PolygonMaskDraw.js +34 -0
  51. package/esm/Options/Classes/PolygonMaskDrawStroke.js +24 -0
  52. package/esm/Options/Classes/PolygonMaskInline.js +13 -0
  53. package/esm/Options/Classes/PolygonMaskLocalSvg.js +25 -0
  54. package/esm/Options/Classes/PolygonMaskMove.js +17 -0
  55. package/esm/Options/Interfaces/IPolygonMask.js +1 -0
  56. package/esm/Options/Interfaces/IPolygonMaskDraw.js +1 -0
  57. package/esm/Options/Interfaces/IPolygonMaskDrawStroke.js +1 -0
  58. package/esm/Options/Interfaces/IPolygonMaskInline.js +1 -0
  59. package/esm/Options/Interfaces/IPolygonMaskLocalSvg.js +1 -0
  60. package/esm/Options/Interfaces/IPolygonMaskMove.js +1 -0
  61. package/esm/PolygonMaskInstance.js +388 -0
  62. package/esm/index.js +33 -0
  63. package/esm/pathseg.js +1496 -0
  64. package/esm/types.js +1 -0
  65. package/esm/utils.js +108 -0
  66. package/package.json +81 -0
  67. package/report.html +39 -0
  68. package/tsparticles.plugin.polygon-mask.js +2276 -0
  69. package/tsparticles.plugin.polygon-mask.min.js +2 -0
  70. package/tsparticles.plugin.polygon-mask.min.js.LICENSE.txt +8 -0
  71. package/types/Enums/PolygonMaskInlineArrangement.d.ts +8 -0
  72. package/types/Enums/PolygonMaskMoveType.d.ts +4 -0
  73. package/types/Enums/PolygonMaskType.d.ts +6 -0
  74. package/types/Interfaces/ISvgPath.d.ts +6 -0
  75. package/types/Options/Classes/PolygonMask.d.ts +23 -0
  76. package/types/Options/Classes/PolygonMaskDraw.d.ts +14 -0
  77. package/types/Options/Classes/PolygonMaskDrawStroke.d.ts +10 -0
  78. package/types/Options/Classes/PolygonMaskInline.d.ts +9 -0
  79. package/types/Options/Classes/PolygonMaskLocalSvg.d.ts +8 -0
  80. package/types/Options/Classes/PolygonMaskMove.d.ts +9 -0
  81. package/types/Options/Interfaces/IPolygonMask.d.ts +17 -0
  82. package/types/Options/Interfaces/IPolygonMaskDraw.d.ts +8 -0
  83. package/types/Options/Interfaces/IPolygonMaskDrawStroke.d.ts +6 -0
  84. package/types/Options/Interfaces/IPolygonMaskInline.d.ts +4 -0
  85. package/types/Options/Interfaces/IPolygonMaskLocalSvg.d.ts +5 -0
  86. package/types/Options/Interfaces/IPolygonMaskMove.d.ts +5 -0
  87. package/types/PolygonMaskInstance.d.ts +37 -0
  88. package/types/index.d.ts +6 -0
  89. package/types/pathseg.d.ts +0 -0
  90. package/types/types.d.ts +15 -0
  91. package/types/utils.d.ts +11 -0
  92. package/umd/Enums/PolygonMaskInlineArrangement.js +12 -0
  93. package/umd/Enums/PolygonMaskMoveType.js +12 -0
  94. package/umd/Enums/PolygonMaskType.js +12 -0
  95. package/umd/Interfaces/ISvgPath.js +12 -0
  96. package/umd/Options/Classes/PolygonMask.js +70 -0
  97. package/umd/Options/Classes/PolygonMaskDraw.js +48 -0
  98. package/umd/Options/Classes/PolygonMaskDrawStroke.js +38 -0
  99. package/umd/Options/Classes/PolygonMaskInline.js +27 -0
  100. package/umd/Options/Classes/PolygonMaskLocalSvg.js +39 -0
  101. package/umd/Options/Classes/PolygonMaskMove.js +31 -0
  102. package/umd/Options/Interfaces/IPolygonMask.js +12 -0
  103. package/umd/Options/Interfaces/IPolygonMaskDraw.js +12 -0
  104. package/umd/Options/Interfaces/IPolygonMaskDrawStroke.js +12 -0
  105. package/umd/Options/Interfaces/IPolygonMaskInline.js +12 -0
  106. package/umd/Options/Interfaces/IPolygonMaskLocalSvg.js +12 -0
  107. package/umd/Options/Interfaces/IPolygonMaskMove.js +12 -0
  108. package/umd/PolygonMaskInstance.js +402 -0
  109. package/umd/index.js +61 -0
  110. package/umd/pathseg.js +1496 -0
  111. package/umd/types.js +12 -0
  112. package/umd/utils.js +126 -0
@@ -0,0 +1,388 @@
1
+ import { calcClosestPtOnSegment, drawPolygonMask, drawPolygonMaskPath, parsePaths, segmentBounce } from "./utils";
2
+ import { deepExtend, getDistance, getDistances, getRandom, itemFromArray } from "@tsparticles/engine";
3
+ const noPolygonDataLoaded = "No polygon data loaded.", noPolygonFound = "No polygon found, you need to specify SVG url in config.";
4
+ export class PolygonMaskInstance {
5
+ constructor(container, engine) {
6
+ this._container = container;
7
+ this._engine = engine;
8
+ this.dimension = {
9
+ height: 0,
10
+ width: 0,
11
+ };
12
+ this._moveRadius = 0;
13
+ this._scale = 1;
14
+ }
15
+ clickPositionValid(position) {
16
+ const options = this._container.actualOptions.polygon;
17
+ return (!!(options === null || options === void 0 ? void 0 : options.enable) &&
18
+ options.type !== "none" &&
19
+ options.type !== "inline" &&
20
+ this.checkInsidePolygon(position));
21
+ }
22
+ draw(context) {
23
+ var _a;
24
+ if (!((_a = this.paths) === null || _a === void 0 ? void 0 : _a.length)) {
25
+ return;
26
+ }
27
+ const options = this._container.actualOptions.polygon;
28
+ if (!(options === null || options === void 0 ? void 0 : options.enable)) {
29
+ return;
30
+ }
31
+ const polygonDraw = options.draw;
32
+ if (!polygonDraw.enable) {
33
+ return;
34
+ }
35
+ const rawData = this.raw;
36
+ for (const path of this.paths) {
37
+ const path2d = path.path2d;
38
+ if (!context) {
39
+ continue;
40
+ }
41
+ if (path2d && this.offset) {
42
+ drawPolygonMaskPath(context, path2d, polygonDraw.stroke, this.offset);
43
+ }
44
+ else if (rawData) {
45
+ drawPolygonMask(context, rawData, polygonDraw.stroke);
46
+ }
47
+ }
48
+ }
49
+ async init() {
50
+ const container = this._container, polygonMaskOptions = container.actualOptions.polygon, pxRatio = container.retina.pixelRatio;
51
+ if (!polygonMaskOptions) {
52
+ return;
53
+ }
54
+ this._moveRadius = polygonMaskOptions.move.radius * pxRatio;
55
+ this._scale = polygonMaskOptions.scale * pxRatio;
56
+ if (polygonMaskOptions.enable) {
57
+ await this.initRawData();
58
+ }
59
+ }
60
+ particleBounce(particle, delta, direction) {
61
+ return this.polygonBounce(particle, delta, direction);
62
+ }
63
+ particlePosition(position) {
64
+ var _a, _b;
65
+ const options = this._container.actualOptions.polygon;
66
+ if (!((options === null || options === void 0 ? void 0 : options.enable) && ((_b = (_a = this.raw) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0)) {
67
+ return;
68
+ }
69
+ return deepExtend({}, position ? position : this.randomPoint());
70
+ }
71
+ particlesInitialization() {
72
+ const options = this._container.actualOptions.polygon;
73
+ if ((options === null || options === void 0 ? void 0 : options.enable) &&
74
+ options.type === "inline" &&
75
+ (options.inline.arrangement === "one-per-point" ||
76
+ options.inline.arrangement === "per-point")) {
77
+ this.drawPoints();
78
+ return true;
79
+ }
80
+ return false;
81
+ }
82
+ resize() {
83
+ const container = this._container, options = container.actualOptions.polygon;
84
+ if (!((options === null || options === void 0 ? void 0 : options.enable) && options.type !== "none")) {
85
+ return;
86
+ }
87
+ if (this.redrawTimeout) {
88
+ clearTimeout(this.redrawTimeout);
89
+ }
90
+ this.redrawTimeout = window.setTimeout(async () => {
91
+ await this.initRawData(true);
92
+ await container.particles.redraw();
93
+ }, 250);
94
+ }
95
+ stop() {
96
+ delete this.raw;
97
+ delete this.paths;
98
+ }
99
+ checkInsidePolygon(position) {
100
+ var _a, _b;
101
+ const container = this._container, options = container.actualOptions.polygon;
102
+ if (!(options === null || options === void 0 ? void 0 : options.enable) || options.type === "none" || options.type === "inline") {
103
+ return true;
104
+ }
105
+ if (!this.raw) {
106
+ throw new Error(noPolygonFound);
107
+ }
108
+ const canvasSize = container.canvas.size, x = (_a = position === null || position === void 0 ? void 0 : position.x) !== null && _a !== void 0 ? _a : getRandom() * canvasSize.width, y = (_b = position === null || position === void 0 ? void 0 : position.y) !== null && _b !== void 0 ? _b : getRandom() * canvasSize.height;
109
+ let inside = false;
110
+ for (let i = 0, j = this.raw.length - 1; i < this.raw.length; j = i++) {
111
+ const pi = this.raw[i], pj = this.raw[j], intersect = pi.y > y !== pj.y > y && x < ((pj.x - pi.x) * (y - pi.y)) / (pj.y - pi.y) + pi.x;
112
+ if (intersect) {
113
+ inside = !inside;
114
+ }
115
+ }
116
+ return options.type === "inside"
117
+ ? inside
118
+ : options.type === "outside"
119
+ ? !inside
120
+ : false;
121
+ }
122
+ createPath2D() {
123
+ var _a, _b;
124
+ const container = this._container, options = container.actualOptions.polygon;
125
+ if (!options || !((_a = this.paths) === null || _a === void 0 ? void 0 : _a.length)) {
126
+ return;
127
+ }
128
+ for (const path of this.paths) {
129
+ const pathData = (_b = path.element) === null || _b === void 0 ? void 0 : _b.getAttribute("d");
130
+ if (pathData) {
131
+ const path2d = new Path2D(pathData), matrix = document.createElementNS("http://www.w3.org/2000/svg", "svg").createSVGMatrix(), finalPath = new Path2D(), transform = matrix.scale(this._scale);
132
+ if (finalPath.addPath) {
133
+ finalPath.addPath(path2d, transform);
134
+ path.path2d = finalPath;
135
+ }
136
+ else {
137
+ delete path.path2d;
138
+ }
139
+ }
140
+ else {
141
+ delete path.path2d;
142
+ }
143
+ if (path.path2d || !this.raw) {
144
+ continue;
145
+ }
146
+ path.path2d = new Path2D();
147
+ path.path2d.moveTo(this.raw[0].x, this.raw[0].y);
148
+ this.raw.forEach((pos, i) => {
149
+ var _a;
150
+ if (i > 0) {
151
+ (_a = path.path2d) === null || _a === void 0 ? void 0 : _a.lineTo(pos.x, pos.y);
152
+ }
153
+ });
154
+ path.path2d.closePath();
155
+ }
156
+ }
157
+ async downloadSvgPath(svgUrl, force) {
158
+ const options = this._container.actualOptions.polygon;
159
+ if (!options) {
160
+ return;
161
+ }
162
+ const url = svgUrl || options.url, forceDownload = force !== null && force !== void 0 ? force : false;
163
+ if (!url || (this.paths !== undefined && !forceDownload)) {
164
+ return this.raw;
165
+ }
166
+ const req = await fetch(url);
167
+ if (!req.ok) {
168
+ throw new Error("tsParticles Error - Error occurred during polygon mask download");
169
+ }
170
+ return this.parseSvgPath(await req.text(), force);
171
+ }
172
+ drawPoints() {
173
+ if (!this.raw) {
174
+ return;
175
+ }
176
+ for (const item of this.raw) {
177
+ this._container.particles.addParticle({
178
+ x: item.x,
179
+ y: item.y,
180
+ });
181
+ }
182
+ }
183
+ getEquidistantPointByIndex(index) {
184
+ var _a, _b, _c, _d, _e, _f, _g;
185
+ const container = this._container, options = container.actualOptions, polygonMaskOptions = options.polygon;
186
+ if (!polygonMaskOptions) {
187
+ return;
188
+ }
189
+ if (!this.raw || !this.raw.length || !((_a = this.paths) === null || _a === void 0 ? void 0 : _a.length))
190
+ throw new Error(noPolygonDataLoaded);
191
+ let offset = 0, point;
192
+ const totalLength = this.paths.reduce((tot, path) => tot + path.length, 0), distance = totalLength / options.particles.number.value;
193
+ for (const path of this.paths) {
194
+ const pathDistance = distance * index - offset;
195
+ if (pathDistance <= path.length) {
196
+ point = path.element.getPointAtLength(pathDistance);
197
+ break;
198
+ }
199
+ else {
200
+ offset += path.length;
201
+ }
202
+ }
203
+ const scale = this._scale;
204
+ return {
205
+ x: ((_b = point === null || point === void 0 ? void 0 : point.x) !== null && _b !== void 0 ? _b : 0) * scale + ((_d = (_c = this.offset) === null || _c === void 0 ? void 0 : _c.x) !== null && _d !== void 0 ? _d : 0),
206
+ y: ((_e = point === null || point === void 0 ? void 0 : point.y) !== null && _e !== void 0 ? _e : 0) * scale + ((_g = (_f = this.offset) === null || _f === void 0 ? void 0 : _f.y) !== null && _g !== void 0 ? _g : 0),
207
+ };
208
+ }
209
+ getPointByIndex(index) {
210
+ if (!this.raw || !this.raw.length) {
211
+ throw new Error(noPolygonDataLoaded);
212
+ }
213
+ const coords = this.raw[index % this.raw.length];
214
+ return {
215
+ x: coords.x,
216
+ y: coords.y,
217
+ };
218
+ }
219
+ getRandomPoint() {
220
+ if (!this.raw || !this.raw.length) {
221
+ throw new Error(noPolygonDataLoaded);
222
+ }
223
+ const coords = itemFromArray(this.raw);
224
+ return {
225
+ x: coords.x,
226
+ y: coords.y,
227
+ };
228
+ }
229
+ getRandomPointByLength() {
230
+ var _a, _b, _c;
231
+ const container = this._container, options = container.actualOptions.polygon;
232
+ if (!options) {
233
+ return;
234
+ }
235
+ if (!this.raw || !this.raw.length || !((_a = this.paths) === null || _a === void 0 ? void 0 : _a.length)) {
236
+ throw new Error(noPolygonDataLoaded);
237
+ }
238
+ const path = itemFromArray(this.paths), distance = Math.floor(getRandom() * path.length) + 1, point = path.element.getPointAtLength(distance), scale = this._scale;
239
+ return {
240
+ x: point.x * scale + (((_b = this.offset) === null || _b === void 0 ? void 0 : _b.x) || 0),
241
+ y: point.y * scale + (((_c = this.offset) === null || _c === void 0 ? void 0 : _c.y) || 0),
242
+ };
243
+ }
244
+ async initRawData(force) {
245
+ const options = this._container.actualOptions.polygon;
246
+ if (!options) {
247
+ return;
248
+ }
249
+ if (options.url) {
250
+ this.raw = await this.downloadSvgPath(options.url, force);
251
+ }
252
+ else if (options.data) {
253
+ const data = options.data;
254
+ let svg;
255
+ if (typeof data !== "string") {
256
+ const path = data.path instanceof Array
257
+ ? data.path.map((t) => `<path d="${t}" />`).join("")
258
+ : `<path d="${data.path}" />`;
259
+ const namespaces = 'xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"';
260
+ svg = `<svg ${namespaces} width="${data.size.width}" height="${data.size.height}">${path}</svg>`;
261
+ }
262
+ else {
263
+ svg = data;
264
+ }
265
+ this.raw = this.parseSvgPath(svg, force);
266
+ }
267
+ this.createPath2D();
268
+ this._engine.dispatchEvent("polygonMaskLoaded", {
269
+ container: this._container,
270
+ });
271
+ }
272
+ parseSvgPath(xml, force) {
273
+ var _a, _b, _c;
274
+ const forceDownload = force !== null && force !== void 0 ? force : false;
275
+ if (this.paths !== undefined && !forceDownload) {
276
+ return this.raw;
277
+ }
278
+ const container = this._container, options = container.actualOptions.polygon;
279
+ if (!options) {
280
+ return;
281
+ }
282
+ const parser = new DOMParser(), doc = parser.parseFromString(xml, "image/svg+xml"), svg = doc.getElementsByTagName("svg")[0];
283
+ let svgPaths = svg.getElementsByTagName("path");
284
+ if (!svgPaths.length) {
285
+ svgPaths = doc.getElementsByTagName("path");
286
+ }
287
+ this.paths = [];
288
+ for (let i = 0; i < svgPaths.length; i++) {
289
+ const path = svgPaths.item(i);
290
+ if (path) {
291
+ this.paths.push({
292
+ element: path,
293
+ length: path.getTotalLength(),
294
+ });
295
+ }
296
+ }
297
+ const scale = this._scale;
298
+ this.dimension.width = parseFloat((_a = svg.getAttribute("width")) !== null && _a !== void 0 ? _a : "0") * scale;
299
+ this.dimension.height = parseFloat((_b = svg.getAttribute("height")) !== null && _b !== void 0 ? _b : "0") * scale;
300
+ const position = (_c = options.position) !== null && _c !== void 0 ? _c : {
301
+ x: 50,
302
+ y: 50,
303
+ };
304
+ this.offset = {
305
+ x: (container.canvas.size.width * position.x) / 100 - this.dimension.width / 2,
306
+ y: (container.canvas.size.height * position.y) / 100 - this.dimension.height / 2,
307
+ };
308
+ return parsePaths(this.paths, scale, this.offset);
309
+ }
310
+ polygonBounce(particle, _delta, direction) {
311
+ const options = this._container.actualOptions.polygon;
312
+ if (!this.raw || !(options === null || options === void 0 ? void 0 : options.enable) || direction !== "top") {
313
+ return false;
314
+ }
315
+ if (options.type === "inside" || options.type === "outside") {
316
+ let closest, dx, dy;
317
+ const pos = particle.getPosition(), radius = particle.getRadius();
318
+ for (let i = 0, j = this.raw.length - 1; i < this.raw.length; j = i++) {
319
+ const pi = this.raw[i], pj = this.raw[j];
320
+ closest = calcClosestPtOnSegment(pi, pj, pos);
321
+ const dist = getDistances(pos, closest);
322
+ [dx, dy] = [dist.dx, dist.dy];
323
+ if (dist.distance < radius) {
324
+ segmentBounce(pi, pj, particle.velocity);
325
+ return true;
326
+ }
327
+ }
328
+ if (closest && dx !== undefined && dy !== undefined && !this.checkInsidePolygon(pos)) {
329
+ const factor = { x: 1, y: 1 };
330
+ if (particle.position.x >= closest.x) {
331
+ factor.x = -1;
332
+ }
333
+ if (particle.position.y >= closest.y) {
334
+ factor.y = -1;
335
+ }
336
+ particle.position.x = closest.x + radius * 2 * factor.x;
337
+ particle.position.y = closest.y + radius * 2 * factor.y;
338
+ particle.velocity.mult(-1);
339
+ return true;
340
+ }
341
+ }
342
+ else if (options.type === "inline" && particle.initialPosition) {
343
+ const dist = getDistance(particle.initialPosition, particle.getPosition());
344
+ if (dist > this._moveRadius) {
345
+ particle.velocity.x = particle.velocity.y / 2 - particle.velocity.x;
346
+ particle.velocity.y = particle.velocity.x / 2 - particle.velocity.y;
347
+ return true;
348
+ }
349
+ }
350
+ return false;
351
+ }
352
+ randomPoint() {
353
+ const container = this._container, options = container.actualOptions.polygon;
354
+ if (!options) {
355
+ return;
356
+ }
357
+ let position;
358
+ if (options.type === "inline") {
359
+ switch (options.inline.arrangement) {
360
+ case "random-point":
361
+ position = this.getRandomPoint();
362
+ break;
363
+ case "random-length":
364
+ position = this.getRandomPointByLength();
365
+ break;
366
+ case "equidistant":
367
+ position = this.getEquidistantPointByIndex(container.particles.count);
368
+ break;
369
+ case "one-per-point":
370
+ case "per-point":
371
+ default:
372
+ position = this.getPointByIndex(container.particles.count);
373
+ }
374
+ }
375
+ else {
376
+ position = {
377
+ x: getRandom() * container.canvas.size.width,
378
+ y: getRandom() * container.canvas.size.height,
379
+ };
380
+ }
381
+ if (this.checkInsidePolygon(position)) {
382
+ return position;
383
+ }
384
+ else {
385
+ return this.randomPoint();
386
+ }
387
+ }
388
+ }
@@ -0,0 +1,33 @@
1
+ import "./pathseg";
2
+ import { PolygonMask } from "./Options/Classes/PolygonMask";
3
+ import { PolygonMaskInstance } from "./PolygonMaskInstance";
4
+ class PolygonMaskPlugin {
5
+ constructor(engine) {
6
+ this.id = "polygonMask";
7
+ this._engine = engine;
8
+ }
9
+ getPlugin(container) {
10
+ return new PolygonMaskInstance(container, this._engine);
11
+ }
12
+ loadOptions(options, source) {
13
+ if (!this.needsPlugin(source)) {
14
+ return;
15
+ }
16
+ let polygonOptions = options.polygon;
17
+ if ((polygonOptions === null || polygonOptions === void 0 ? void 0 : polygonOptions.load) === undefined) {
18
+ options.polygon = polygonOptions = new PolygonMask();
19
+ }
20
+ polygonOptions.load(source === null || source === void 0 ? void 0 : source.polygon);
21
+ }
22
+ needsPlugin(options) {
23
+ var _a, _b, _c;
24
+ return ((_b = (_a = options === null || options === void 0 ? void 0 : options.polygon) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : (((_c = options === null || options === void 0 ? void 0 : options.polygon) === null || _c === void 0 ? void 0 : _c.type) !== undefined && options.polygon.type !== "none"));
25
+ }
26
+ }
27
+ export async function loadPolygonMaskPlugin(engine) {
28
+ const plugin = new PolygonMaskPlugin(engine);
29
+ await engine.addPlugin(plugin);
30
+ }
31
+ export * from "./Enums/PolygonMaskInlineArrangement";
32
+ export * from "./Enums/PolygonMaskMoveType";
33
+ export * from "./Enums/PolygonMaskType";