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