@tsparticles/plugin-polygon-mask 3.9.0 → 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.
Files changed (45) hide show
  1. package/409.min.js +2 -0
  2. package/409.min.js.LICENSE.txt +1 -0
  3. package/browser/PolygonMaskInstance.js +40 -24
  4. package/browser/index.js +6 -5
  5. package/browser/pathseg.js +1 -1
  6. package/browser/utils.js +8 -5
  7. package/cjs/Enums/PolygonMaskInlineArrangement.js +2 -5
  8. package/cjs/Enums/PolygonMaskMoveType.js +2 -5
  9. package/cjs/Enums/PolygonMaskType.js +2 -5
  10. package/cjs/Interfaces/ISvgPath.js +1 -2
  11. package/cjs/Options/Classes/PolygonMask.js +16 -20
  12. package/cjs/Options/Classes/PolygonMaskDraw.js +5 -9
  13. package/cjs/Options/Classes/PolygonMaskDrawStroke.js +7 -11
  14. package/cjs/Options/Classes/PolygonMaskInline.js +5 -9
  15. package/cjs/Options/Classes/PolygonMaskLocalSvg.js +3 -7
  16. package/cjs/Options/Classes/PolygonMaskMove.js +5 -9
  17. package/cjs/Options/Interfaces/IPolygonMask.js +1 -2
  18. package/cjs/Options/Interfaces/IPolygonMaskDraw.js +1 -2
  19. package/cjs/Options/Interfaces/IPolygonMaskDrawStroke.js +1 -2
  20. package/cjs/Options/Interfaces/IPolygonMaskInline.js +1 -2
  21. package/cjs/Options/Interfaces/IPolygonMaskLocalSvg.js +1 -2
  22. package/cjs/Options/Interfaces/IPolygonMaskMove.js +1 -2
  23. package/cjs/PolygonMaskInstance.js +73 -61
  24. package/cjs/PolygonMaskPlugin.js +7 -11
  25. package/cjs/index.js +9 -25
  26. package/cjs/pathseg.js +2 -3
  27. package/cjs/types.js +1 -2
  28. package/cjs/utils.js +16 -20
  29. package/dist_browser_PolygonMaskPlugin_js.js +120 -0
  30. package/esm/PolygonMaskInstance.js +40 -24
  31. package/esm/index.js +6 -5
  32. package/esm/pathseg.js +1 -1
  33. package/esm/utils.js +8 -5
  34. package/package.json +4 -3
  35. package/report.html +5 -4
  36. package/tsparticles.plugin.polygon-mask.js +218 -129
  37. package/tsparticles.plugin.polygon-mask.min.js +1 -1
  38. package/tsparticles.plugin.polygon-mask.min.js.LICENSE.txt +1 -1
  39. package/types/PolygonMaskInstance.d.ts +1 -0
  40. package/types/index.d.ts +1 -2
  41. package/types/utils.d.ts +2 -2
  42. package/umd/PolygonMaskInstance.js +40 -24
  43. package/umd/index.js +30 -6
  44. package/umd/pathseg.js +1 -1
  45. package/umd/utils.js +8 -5
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,37 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PolygonMaskInstance = void 0;
4
- const engine_1 = require("@tsparticles/engine");
5
- const utils_js_1 = require("./utils.js");
6
- const PolygonMaskInlineArrangement_js_1 = require("./Enums/PolygonMaskInlineArrangement.js");
7
- const PolygonMaskType_js_1 = require("./Enums/PolygonMaskType.js");
8
- const noPolygonDataLoaded = `${engine_1.errorPrefix} No polygon data loaded.`, noPolygonFound = `${engine_1.errorPrefix} No polygon found, you need to specify SVG url in config.`, origin = {
1
+ import "./pathseg.js";
2
+ import { OutModeDirection, deepExtend, getDistance, getDistances, getRandom, isArray, isString, itemFromArray, percentDenominator, safeDocument, } from "@tsparticles/engine";
3
+ import { calcClosestPointOnSegment, drawPolygonMask, drawPolygonMaskPath, parsePaths, segmentBounce } from "./utils.js";
4
+ import { PolygonMaskInlineArrangement } from "./Enums/PolygonMaskInlineArrangement.js";
5
+ import { PolygonMaskType } from "./Enums/PolygonMaskType.js";
6
+ const noPolygonDataLoaded = `No polygon data loaded.`, noPolygonFound = `No polygon found, you need to specify SVG url in config.`, origin = {
9
7
  x: 0,
10
8
  y: 0,
11
9
  }, half = 0.5, double = 2;
12
- class PolygonMaskInstance {
10
+ export class PolygonMaskInstance {
13
11
  constructor(container, engine) {
14
12
  this._checkInsidePolygon = position => {
15
13
  const container = this._container, options = container.actualOptions.polygon;
16
- if (!options?.enable || options.type === PolygonMaskType_js_1.PolygonMaskType.none || options.type === PolygonMaskType_js_1.PolygonMaskType.inline) {
14
+ if (!options?.enable || options.type === PolygonMaskType.none || options.type === PolygonMaskType.inline) {
17
15
  return true;
18
16
  }
19
17
  if (!this.raw) {
20
18
  throw new Error(noPolygonFound);
21
19
  }
22
- const canvasSize = container.canvas.size, x = position?.x ?? (0, engine_1.getRandom)() * canvasSize.width, y = position?.y ?? (0, engine_1.getRandom)() * canvasSize.height, indexOffset = 1;
20
+ const canvasSize = container.canvas.size, x = position?.x ?? getRandom() * canvasSize.width, y = position?.y ?? getRandom() * canvasSize.height, indexOffset = 1;
23
21
  let inside = false;
24
22
  for (let i = 0, j = this.raw.length - indexOffset; i < this.raw.length; j = i++) {
25
- 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;
23
+ const pi = this.raw[i], pj = this.raw[j];
24
+ if (!pi || !pj) {
25
+ continue;
26
+ }
27
+ const intersect = pi.y > y !== pj.y > y && x < ((pj.x - pi.x) * (y - pi.y)) / (pj.y - pi.y) + pi.x;
26
28
  if (intersect) {
27
29
  inside = !inside;
28
30
  }
29
31
  }
30
- if (options.type === PolygonMaskType_js_1.PolygonMaskType.inside) {
32
+ if (options.type === PolygonMaskType.inside) {
31
33
  return inside;
32
34
  }
33
35
  else {
34
- return options.type === PolygonMaskType_js_1.PolygonMaskType.outside ? !inside : false;
36
+ return !inside;
35
37
  }
36
38
  };
37
39
  this._createPath2D = () => {
@@ -40,16 +42,11 @@ class PolygonMaskInstance {
40
42
  return;
41
43
  }
42
44
  for (const path of this.paths) {
43
- const pathData = path.element?.getAttribute("d");
45
+ const pathData = path.element.getAttribute("d");
44
46
  if (pathData) {
45
- const path2d = new Path2D(pathData), matrix = document.createElementNS("http://www.w3.org/2000/svg", "svg").createSVGMatrix(), finalPath = new Path2D(), transform = matrix.scale(this._scale);
46
- if (finalPath.addPath) {
47
- finalPath.addPath(path2d, transform);
48
- path.path2d = finalPath;
49
- }
50
- else {
51
- delete path.path2d;
52
- }
47
+ const path2d = new Path2D(pathData), matrix = safeDocument().createElementNS("http://www.w3.org/2000/svg", "svg").createSVGMatrix(), finalPath = new Path2D(), transform = matrix.scale(this._scale);
48
+ finalPath.addPath(path2d, transform);
49
+ path.path2d = finalPath;
53
50
  }
54
51
  else {
55
52
  delete path.path2d;
@@ -59,6 +56,9 @@ class PolygonMaskInstance {
59
56
  }
60
57
  path.path2d = new Path2D();
61
58
  const firstIndex = 0, firstPoint = this.raw[firstIndex];
59
+ if (!firstPoint) {
60
+ continue;
61
+ }
62
62
  path.path2d.moveTo(firstPoint.x, firstPoint.y);
63
63
  this.raw.forEach((pos, i) => {
64
64
  if (i > firstIndex) {
@@ -79,7 +79,7 @@ class PolygonMaskInstance {
79
79
  }
80
80
  const req = await fetch(url);
81
81
  if (!req.ok) {
82
- throw new Error(`${engine_1.errorPrefix} occurred during polygon mask download`);
82
+ throw new Error(`Error occurred during polygon mask download`);
83
83
  }
84
84
  return this._parseSvgPath(await req.text(), force);
85
85
  };
@@ -125,6 +125,9 @@ class PolygonMaskInstance {
125
125
  throw new Error(noPolygonDataLoaded);
126
126
  }
127
127
  const coords = this.raw[index % this.raw.length];
128
+ if (!coords) {
129
+ return;
130
+ }
128
131
  return {
129
132
  x: coords.x,
130
133
  y: coords.y,
@@ -134,7 +137,10 @@ class PolygonMaskInstance {
134
137
  if (!this.raw?.length) {
135
138
  throw new Error(noPolygonDataLoaded);
136
139
  }
137
- const coords = (0, engine_1.itemFromArray)(this.raw);
140
+ const coords = itemFromArray(this.raw);
141
+ if (!coords) {
142
+ return;
143
+ }
138
144
  return {
139
145
  x: coords.x,
140
146
  y: coords.y,
@@ -148,7 +154,11 @@ class PolygonMaskInstance {
148
154
  if (!this.raw?.length || !this.paths?.length) {
149
155
  throw new Error(noPolygonDataLoaded);
150
156
  }
151
- const path = (0, engine_1.itemFromArray)(this.paths), offset = 1, distance = Math.floor((0, engine_1.getRandom)() * path.length) + offset, point = path.element.getPointAtLength(distance), scale = this._scale;
157
+ const path = itemFromArray(this.paths);
158
+ if (!path) {
159
+ return;
160
+ }
161
+ const offset = 1, distance = Math.floor(getRandom() * path.length) + offset, point = path.element.getPointAtLength(distance), scale = this._scale;
152
162
  return {
153
163
  x: point.x * scale + (this.offset?.x ?? origin.x),
154
164
  y: point.y * scale + (this.offset?.y ?? origin.y),
@@ -165,13 +175,13 @@ class PolygonMaskInstance {
165
175
  else if (options.data) {
166
176
  const data = options.data;
167
177
  let svg;
168
- if ((0, engine_1.isString)(data)) {
178
+ if (isString(data)) {
169
179
  svg = data;
170
180
  }
171
181
  else {
172
- const getPath = (p) => `<path d="${p}" />`, path = (0, engine_1.isArray)(data.path) ? data.path.map(getPath).join("") : getPath(data.path);
182
+ const getPath = (p) => `<path d="${p}" />`, path = isArray(data.path) ? data.path.map(getPath).join("") : getPath(data.path);
173
183
  const namespaces = 'xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"';
174
- svg = `<svg ${namespaces} width="${data.size.width}" height="${data.size.height}">${path}</svg>`;
184
+ svg = `<svg ${namespaces} width="${data.size.width.toString()}" height="${data.size.height.toString()}">${path}</svg>`;
175
185
  }
176
186
  this.raw = this._parseSvgPath(svg, force);
177
187
  }
@@ -190,6 +200,9 @@ class PolygonMaskInstance {
190
200
  return;
191
201
  }
192
202
  const parser = new DOMParser(), doc = parser.parseFromString(xml, "image/svg+xml"), firstIndex = 0, svg = doc.getElementsByTagName("svg")[firstIndex];
203
+ if (!svg) {
204
+ return;
205
+ }
193
206
  let svgPaths = svg.getElementsByTagName("path");
194
207
  if (!svgPaths.length) {
195
208
  svgPaths = doc.getElementsByTagName("path");
@@ -212,26 +225,29 @@ class PolygonMaskInstance {
212
225
  y: 50,
213
226
  }, canvasSize = container.canvas.size;
214
227
  this.offset = {
215
- x: (canvasSize.width * position.x) / engine_1.percentDenominator - this.dimension.width * half,
216
- y: (canvasSize.height * position.y) / engine_1.percentDenominator - this.dimension.height * half,
228
+ x: (canvasSize.width * position.x) / percentDenominator - this.dimension.width * half,
229
+ y: (canvasSize.height * position.y) / percentDenominator - this.dimension.height * half,
217
230
  };
218
- return (0, utils_js_1.parsePaths)(this.paths, scale, this.offset);
231
+ return parsePaths(this.paths, scale, this.offset);
219
232
  };
220
- this._polygonBounce = (particle, delta, direction) => {
233
+ this._polygonBounce = (particle, _delta, direction) => {
221
234
  const options = this._container.actualOptions.polygon;
222
- if (!this.raw || !options?.enable || direction !== engine_1.OutModeDirection.top) {
235
+ if (!this.raw || !options?.enable || direction !== OutModeDirection.top) {
223
236
  return false;
224
237
  }
225
- if (options.type === PolygonMaskType_js_1.PolygonMaskType.inside || options.type === PolygonMaskType_js_1.PolygonMaskType.outside) {
238
+ if (options.type === PolygonMaskType.inside || options.type === PolygonMaskType.outside) {
226
239
  let closest, dx, dy;
227
240
  const pos = particle.getPosition(), radius = particle.getRadius(), offset = 1;
228
241
  for (let i = 0, j = this.raw.length - offset; i < this.raw.length; j = i++) {
229
242
  const pi = this.raw[i], pj = this.raw[j];
230
- closest = (0, utils_js_1.calcClosestPointOnSegment)(pi, pj, pos);
231
- const dist = (0, engine_1.getDistances)(pos, closest);
243
+ if (!pi || !pj) {
244
+ continue;
245
+ }
246
+ closest = calcClosestPointOnSegment(pi, pj, pos);
247
+ const dist = getDistances(pos, closest);
232
248
  [dx, dy] = [dist.dx, dist.dy];
233
249
  if (dist.distance < radius) {
234
- (0, utils_js_1.segmentBounce)(pi, pj, particle.velocity);
250
+ segmentBounce(pi, pj, particle.velocity);
235
251
  return true;
236
252
  }
237
253
  }
@@ -249,8 +265,8 @@ class PolygonMaskInstance {
249
265
  return true;
250
266
  }
251
267
  }
252
- else if (options.type === PolygonMaskType_js_1.PolygonMaskType.inline && particle.initialPosition) {
253
- const dist = (0, engine_1.getDistance)(particle.initialPosition, particle.getPosition()), { velocity } = particle;
268
+ else if (options.type === PolygonMaskType.inline) {
269
+ const dist = getDistance(particle.initialPosition, particle.getPosition()), { velocity } = particle;
254
270
  if (dist > this._moveRadius) {
255
271
  velocity.x = velocity.y * half - velocity.x;
256
272
  velocity.y = velocity.x * half - velocity.y;
@@ -265,19 +281,19 @@ class PolygonMaskInstance {
265
281
  return;
266
282
  }
267
283
  let position;
268
- if (options.type === PolygonMaskType_js_1.PolygonMaskType.inline) {
284
+ if (options.type === PolygonMaskType.inline) {
269
285
  switch (options.inline.arrangement) {
270
- case PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.randomPoint:
286
+ case PolygonMaskInlineArrangement.randomPoint:
271
287
  position = this._getRandomPoint();
272
288
  break;
273
- case PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.randomLength:
289
+ case PolygonMaskInlineArrangement.randomLength:
274
290
  position = this._getRandomPointByLength();
275
291
  break;
276
- case PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.equidistant:
292
+ case PolygonMaskInlineArrangement.equidistant:
277
293
  position = this._getEquidistantPointByIndex(container.particles.count);
278
294
  break;
279
- case PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.onePerPoint:
280
- case PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.perPoint:
295
+ case PolygonMaskInlineArrangement.onePerPoint:
296
+ case PolygonMaskInlineArrangement.perPoint:
281
297
  default:
282
298
  position = this._getPointByIndex(container.particles.count);
283
299
  }
@@ -285,8 +301,8 @@ class PolygonMaskInstance {
285
301
  else {
286
302
  const canvasSize = container.canvas.size;
287
303
  position = {
288
- x: (0, engine_1.getRandom)() * canvasSize.width,
289
- y: (0, engine_1.getRandom)() * canvasSize.height,
304
+ x: getRandom() * canvasSize.width,
305
+ y: getRandom() * canvasSize.height,
290
306
  };
291
307
  }
292
308
  if (this._checkInsidePolygon(position)) {
@@ -308,8 +324,8 @@ class PolygonMaskInstance {
308
324
  clickPositionValid(position) {
309
325
  const options = this._container.actualOptions.polygon;
310
326
  return (!!options?.enable &&
311
- options.type !== PolygonMaskType_js_1.PolygonMaskType.none &&
312
- options.type !== PolygonMaskType_js_1.PolygonMaskType.inline &&
327
+ options.type !== PolygonMaskType.none &&
328
+ options.type !== PolygonMaskType.inline &&
313
329
  this._checkInsidePolygon(position));
314
330
  }
315
331
  draw(context) {
@@ -327,14 +343,11 @@ class PolygonMaskInstance {
327
343
  const rawData = this.raw;
328
344
  for (const path of this.paths) {
329
345
  const path2d = path.path2d;
330
- if (!context) {
331
- continue;
332
- }
333
346
  if (path2d && this.offset) {
334
- (0, utils_js_1.drawPolygonMaskPath)(this._engine, context, path2d, polygonDraw.stroke, this.offset);
347
+ drawPolygonMaskPath(this._engine, context, path2d, polygonDraw.stroke, this.offset, this._container.hdr);
335
348
  }
336
349
  else if (rawData) {
337
- (0, utils_js_1.drawPolygonMask)(this._engine, context, rawData, polygonDraw.stroke);
350
+ drawPolygonMask(this._engine, context, rawData, polygonDraw.stroke, this._container.hdr);
338
351
  }
339
352
  }
340
353
  }
@@ -357,14 +370,14 @@ class PolygonMaskInstance {
357
370
  if (!(options?.enable && (this.raw?.length ?? defaultLength) > defaultLength)) {
358
371
  return;
359
372
  }
360
- return (0, engine_1.deepExtend)({}, position ? position : this._randomPoint());
373
+ return deepExtend({}, position ?? this._randomPoint());
361
374
  }
362
375
  particlesInitialization() {
363
376
  const options = this._container.actualOptions.polygon;
364
377
  if (options?.enable &&
365
- options.type === PolygonMaskType_js_1.PolygonMaskType.inline &&
366
- (options.inline.arrangement === PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.onePerPoint ||
367
- options.inline.arrangement === PolygonMaskInlineArrangement_js_1.PolygonMaskInlineArrangement.perPoint)) {
378
+ options.type === PolygonMaskType.inline &&
379
+ (options.inline.arrangement === PolygonMaskInlineArrangement.onePerPoint ||
380
+ options.inline.arrangement === PolygonMaskInlineArrangement.perPoint)) {
368
381
  this._drawPoints();
369
382
  return true;
370
383
  }
@@ -372,7 +385,7 @@ class PolygonMaskInstance {
372
385
  }
373
386
  resize() {
374
387
  const container = this._container, options = container.actualOptions.polygon;
375
- if (!(options?.enable && options.type !== PolygonMaskType_js_1.PolygonMaskType.none)) {
388
+ if (!(options?.enable && options.type !== PolygonMaskType.none)) {
376
389
  return;
377
390
  }
378
391
  if (this.redrawTimeout) {
@@ -391,4 +404,3 @@ class PolygonMaskInstance {
391
404
  delete this.paths;
392
405
  }
393
406
  }
394
- exports.PolygonMaskInstance = PolygonMaskInstance;
@@ -1,16 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PolygonMaskPlugin = void 0;
4
- const PolygonMask_js_1 = require("./Options/Classes/PolygonMask.js");
5
- const PolygonMaskInstance_js_1 = require("./PolygonMaskInstance.js");
6
- const PolygonMaskType_js_1 = require("./Enums/PolygonMaskType.js");
7
- class PolygonMaskPlugin {
1
+ import { PolygonMask } from "./Options/Classes/PolygonMask.js";
2
+ import { PolygonMaskInstance } from "./PolygonMaskInstance.js";
3
+ import { PolygonMaskType } from "./Enums/PolygonMaskType.js";
4
+ export class PolygonMaskPlugin {
8
5
  constructor(engine) {
9
6
  this.id = "polygonMask";
10
7
  this._engine = engine;
11
8
  }
12
9
  getPlugin(container) {
13
- return Promise.resolve(new PolygonMaskInstance_js_1.PolygonMaskInstance(container, this._engine));
10
+ return Promise.resolve(new PolygonMaskInstance(container, this._engine));
14
11
  }
15
12
  loadOptions(options, source) {
16
13
  if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
@@ -18,13 +15,12 @@ class PolygonMaskPlugin {
18
15
  }
19
16
  let polygonOptions = options.polygon;
20
17
  if (polygonOptions?.load === undefined) {
21
- options.polygon = polygonOptions = new PolygonMask_js_1.PolygonMask(this._engine);
18
+ options.polygon = polygonOptions = new PolygonMask(this._engine);
22
19
  }
23
20
  polygonOptions.load(source?.polygon);
24
21
  }
25
22
  needsPlugin(options) {
26
23
  return (options?.polygon?.enable ??
27
- (options?.polygon?.type !== undefined && options.polygon.type !== PolygonMaskType_js_1.PolygonMaskType.none));
24
+ (options?.polygon?.type !== undefined && options.polygon.type !== PolygonMaskType.none));
28
25
  }
29
26
  }
30
- exports.PolygonMaskPlugin = PolygonMaskPlugin;
package/cjs/index.js CHANGED
@@ -1,26 +1,10 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.loadPolygonMaskPlugin = loadPolygonMaskPlugin;
18
- require("./pathseg.js");
19
- const PolygonMaskPlugin_js_1 = require("./PolygonMaskPlugin.js");
20
- async function loadPolygonMaskPlugin(engine, refresh = true) {
21
- engine.checkVersion("3.9.0");
22
- await engine.addPlugin(new PolygonMaskPlugin_js_1.PolygonMaskPlugin(engine), refresh);
1
+ export function loadPolygonMaskPlugin(engine) {
2
+ engine.checkVersion("4.0.0-alpha.0");
3
+ engine.register(async (e) => {
4
+ const { PolygonMaskPlugin } = await import("./PolygonMaskPlugin.js");
5
+ e.addPlugin(new PolygonMaskPlugin(engine));
6
+ });
23
7
  }
24
- __exportStar(require("./Enums/PolygonMaskInlineArrangement.js"), exports);
25
- __exportStar(require("./Enums/PolygonMaskMoveType.js"), exports);
26
- __exportStar(require("./Enums/PolygonMaskType.js"), exports);
8
+ export * from "./Enums/PolygonMaskInlineArrangement.js";
9
+ export * from "./Enums/PolygonMaskMoveType.js";
10
+ export * from "./Enums/PolygonMaskType.js";
package/cjs/pathseg.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
1
  (function () {
4
2
  "use strict";
5
3
  try {
@@ -1148,7 +1146,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
1148
1146
  return string;
1149
1147
  };
1150
1148
  window.SVGPathSegList.prototype._parsePath = function (string) {
1151
- if (!string || !string.length)
1149
+ if (!string?.length)
1152
1150
  return [];
1153
1151
  const owningPathSegList = this;
1154
1152
  const Builder = function () {
@@ -1495,3 +1493,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
1495
1493
  console.warn("An error occurred in tsParticles pathseg polyfill. If the Polygon Mask is not working, please open an issue here: https://github.com/tsparticles/tsparticles", e);
1496
1494
  }
1497
1495
  })();
1496
+ export {};
package/cjs/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/cjs/utils.js CHANGED
@@ -1,32 +1,28 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.drawPolygonMask = drawPolygonMask;
4
- exports.drawPolygonMaskPath = drawPolygonMaskPath;
5
- exports.parsePaths = parsePaths;
6
- exports.calcClosestPointOnSegment = calcClosestPointOnSegment;
7
- exports.segmentBounce = segmentBounce;
8
- const engine_1 = require("@tsparticles/engine");
1
+ import { Vector, getDistances, getStyleFromRgb, rangeColorToRgb, } from "@tsparticles/engine";
9
2
  const squareExp = 2, inSegmentRange = {
10
3
  min: 0,
11
4
  max: 1,
12
5
  }, double = 2;
13
- function drawPolygonMask(engine, context, rawData, stroke) {
14
- const color = (0, engine_1.rangeColorToRgb)(engine, stroke.color);
6
+ export function drawPolygonMask(engine, context, rawData, stroke, hdr = false) {
7
+ const color = rangeColorToRgb(engine, stroke.color);
15
8
  if (!color) {
16
9
  return;
17
10
  }
18
11
  const firstIndex = 0, firstItem = rawData[firstIndex];
12
+ if (!firstItem) {
13
+ return;
14
+ }
19
15
  context.beginPath();
20
16
  context.moveTo(firstItem.x, firstItem.y);
21
17
  for (const item of rawData) {
22
18
  context.lineTo(item.x, item.y);
23
19
  }
24
20
  context.closePath();
25
- context.strokeStyle = (0, engine_1.getStyleFromRgb)(color);
21
+ context.strokeStyle = getStyleFromRgb(color, hdr);
26
22
  context.lineWidth = stroke.width;
27
23
  context.stroke();
28
24
  }
29
- function drawPolygonMaskPath(engine, context, path, stroke, position) {
25
+ export function drawPolygonMaskPath(engine, context, path, stroke, position, hdr = false) {
30
26
  const defaultTransform = {
31
27
  a: 1,
32
28
  b: 0,
@@ -34,16 +30,16 @@ function drawPolygonMaskPath(engine, context, path, stroke, position) {
34
30
  d: 1,
35
31
  };
36
32
  context.setTransform(defaultTransform.a, defaultTransform.b, defaultTransform.c, defaultTransform.d, position.x, position.y);
37
- const color = (0, engine_1.rangeColorToRgb)(engine, stroke.color);
33
+ const color = rangeColorToRgb(engine, stroke.color);
38
34
  if (!color) {
39
35
  return;
40
36
  }
41
- context.strokeStyle = (0, engine_1.getStyleFromRgb)(color, stroke.opacity);
37
+ context.strokeStyle = getStyleFromRgb(color, hdr, stroke.opacity);
42
38
  context.lineWidth = stroke.width;
43
39
  context.stroke(path);
44
40
  context.resetTransform();
45
41
  }
46
- function parsePaths(paths, scale, offset) {
42
+ export function parsePaths(paths, scale, offset) {
47
43
  const res = [], defaultCount = 0;
48
44
  for (const path of paths) {
49
45
  const segments = path.element.pathSegList, len = segments?.numberOfItems ?? defaultCount, p = {
@@ -51,7 +47,7 @@ function parsePaths(paths, scale, offset) {
51
47
  y: 0,
52
48
  };
53
49
  for (let i = 0; i < len; i++) {
54
- const segment = segments?.getItem(i), svgPathSeg = window.SVGPathSeg;
50
+ const segment = segments?.getItem(i), svgPathSeg = globalThis.SVGPathSeg;
55
51
  switch (segment?.pathSegType) {
56
52
  case svgPathSeg.PATHSEG_MOVETO_ABS:
57
53
  case svgPathSeg.PATHSEG_LINETO_ABS:
@@ -101,8 +97,8 @@ function parsePaths(paths, scale, offset) {
101
97
  }
102
98
  return res;
103
99
  }
104
- function calcClosestPointOnSegment(s1, s2, pos) {
105
- const { dx: dx1, dy: dy1 } = (0, engine_1.getDistances)(pos, s1), { dx: dx2, dy: dy2 } = (0, engine_1.getDistances)(s2, s1), t = (dx1 * dx2 + dy1 * dy2) / (dx2 ** squareExp + dy2 ** squareExp), res = {
100
+ export function calcClosestPointOnSegment(s1, s2, pos) {
101
+ const { dx: dx1, dy: dy1 } = getDistances(pos, s1), { dx: dx2, dy: dy2 } = getDistances(s2, s1), t = (dx1 * dx2 + dy1 * dy2) / (dx2 ** squareExp + dy2 ** squareExp), res = {
106
102
  x: s1.x + dx2 * t,
107
103
  y: s1.y + dy2 * t,
108
104
  isOnSegment: t >= inSegmentRange.min && t <= inSegmentRange.max,
@@ -117,8 +113,8 @@ function calcClosestPointOnSegment(s1, s2, pos) {
117
113
  }
118
114
  return res;
119
115
  }
120
- function segmentBounce(start, stop, velocity) {
121
- const { dx, dy } = (0, engine_1.getDistances)(start, stop), wallAngle = Math.atan2(dy, dx), wallNormal = engine_1.Vector.create(Math.sin(wallAngle), -Math.cos(wallAngle)), d = double * (velocity.x * wallNormal.x + velocity.y * wallNormal.y);
116
+ export function segmentBounce(start, stop, velocity) {
117
+ const { dx, dy } = getDistances(start, stop), wallAngle = Math.atan2(dy, dx), wallNormal = Vector.create(Math.sin(wallAngle), -Math.cos(wallAngle)), d = double * (velocity.x * wallNormal.x + velocity.y * wallNormal.y);
122
118
  wallNormal.multTo(d);
123
119
  velocity.subFrom(wallNormal);
124
120
  }