@tsparticles/plugin-canvas-mask 3.0.0-alpha.1 → 3.0.0-beta.1

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 (46) hide show
  1. package/README.md +15 -11
  2. package/browser/CanvasMaskInstance.js +2 -2
  3. package/browser/Options/Classes/CanvasMask.js +6 -7
  4. package/browser/Options/Classes/CanvasMaskPixels.js +3 -2
  5. package/browser/Options/Classes/TextMask.js +2 -2
  6. package/browser/index.js +8 -10
  7. package/browser/package.json +1 -0
  8. package/browser/utils.js +20 -20
  9. package/cjs/CanvasMaskInstance.js +34 -45
  10. package/cjs/Options/Classes/CanvasMask.js +10 -11
  11. package/cjs/Options/Classes/CanvasMaskPixels.js +3 -2
  12. package/cjs/Options/Classes/TextMask.js +4 -4
  13. package/cjs/index.js +10 -23
  14. package/cjs/package.json +1 -0
  15. package/cjs/utils.js +19 -19
  16. package/esm/CanvasMaskInstance.js +2 -2
  17. package/esm/Options/Classes/CanvasMask.js +6 -7
  18. package/esm/Options/Classes/CanvasMaskPixels.js +3 -2
  19. package/esm/Options/Classes/TextMask.js +2 -2
  20. package/esm/index.js +8 -10
  21. package/esm/package.json +1 -0
  22. package/esm/utils.js +20 -20
  23. package/package.json +19 -6
  24. package/report.html +4 -4
  25. package/tsparticles.plugin.canvas-mask.js +34 -35
  26. package/tsparticles.plugin.canvas-mask.min.js +1 -1
  27. package/tsparticles.plugin.canvas-mask.min.js.LICENSE.txt +1 -8
  28. package/types/CanvasMaskInstance.d.ts +1 -1
  29. package/types/Options/Classes/CanvasMask.d.ts +5 -5
  30. package/types/Options/Classes/CanvasMaskOverride.d.ts +1 -1
  31. package/types/Options/Classes/CanvasMaskPixels.d.ts +1 -1
  32. package/types/Options/Classes/FontTextMask.d.ts +1 -1
  33. package/types/Options/Classes/ImageMask.d.ts +1 -1
  34. package/types/Options/Classes/TextMask.d.ts +3 -3
  35. package/types/Options/Classes/TextMaskLine.d.ts +1 -1
  36. package/types/Options/Interfaces/ICanvasMask.d.ts +4 -4
  37. package/types/Options/Interfaces/ITextMask.d.ts +2 -2
  38. package/types/index.d.ts +1 -1
  39. package/types/types.d.ts +2 -2
  40. package/types/utils.d.ts +3 -3
  41. package/umd/CanvasMaskInstance.js +7 -7
  42. package/umd/Options/Classes/CanvasMask.js +11 -12
  43. package/umd/Options/Classes/CanvasMaskPixels.js +4 -3
  44. package/umd/Options/Classes/TextMask.js +5 -5
  45. package/umd/index.js +11 -13
  46. package/umd/utils.js +19 -19
package/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  # tsParticles Canvas Mask Plugin
4
4
 
5
- [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles-plugin-canvas-mask/badge)](https://www.jsdelivr.com/package/npm/tsparticles-plugin-canvas-mask)
6
- [![npmjs](https://badge.fury.io/js/tsparticles-plugin-canvas-mask.svg)](https://www.npmjs.com/package/tsparticles-plugin-canvas-mask)
7
- [![npmjs](https://img.shields.io/npm/dt/tsparticles-plugin-canvas-mask)](https://www.npmjs.com/package/tsparticles-plugin-canvas-mask) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/@tsparticles/plugin-canvas-mask/badge)](https://www.jsdelivr.com/package/npm/@tsparticles/plugin-canvas-mask)
6
+ [![npmjs](https://badge.fury.io/js/@tsparticles/plugin-canvas-mask.svg)](https://www.npmjs.com/package/@tsparticles/plugin-canvas-mask)
7
+ [![npmjs](https://img.shields.io/npm/dt/@tsparticles/plugin-canvas-mask)](https://www.npmjs.com/package/@tsparticles/plugin-canvas-mask) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
8
8
 
9
9
  [tsParticles](https://github.com/matteobruni/tsparticles) plugin for particles canvas mask effect.
10
10
 
@@ -42,29 +42,33 @@ Once the scripts are loaded you can set up `tsParticles` and the plugin like thi
42
42
  This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
43
43
 
44
44
  ```shell
45
- $ npm install tsparticles-plugin-canvas-mask
45
+ $ npm install @tsparticles/plugin-canvas-mask
46
46
  ```
47
47
 
48
48
  or
49
49
 
50
50
  ```shell
51
- $ yarn add tsparticles-plugin-canvas-mask
51
+ $ yarn add @tsparticles/plugin-canvas-mask
52
52
  ```
53
53
 
54
54
  Then you need to import it in the app, like this:
55
55
 
56
56
  ```javascript
57
- const { tsParticles } = require("tsparticles-engine");
58
- const { loadCanvasMaskPlugin } = require("tsparticles-plugin-canvas-mask");
57
+ const { tsParticles } = require("@tsparticles/engine");
58
+ const { loadCanvasMaskPlugin } = require("@tsparticles/plugin-canvas-mask");
59
59
 
60
- loadCanvasMaskPlugin(tsParticles); // awaitable
60
+ (async () => {
61
+ await loadCanvasMaskPlugin(tsParticles);
62
+ })();
61
63
  ```
62
64
 
63
65
  or
64
66
 
65
67
  ```javascript
66
- import { tsParticles } from "tsparticles-engine";
67
- import { loadCanvasMaskPlugin } from "tsparticles-plugin-canvas-mask";
68
+ import { tsParticles } from "@tsparticles/engine";
69
+ import { loadCanvasMaskPlugin } from "@tsparticles/plugin-canvas-mask";
68
70
 
69
- loadCanvasMaskPlugin(tsParticles); // awaitable
71
+ (async () => {
72
+ await loadCanvasMaskPlugin(tsParticles);
73
+ })();
70
74
  ```
@@ -1,4 +1,4 @@
1
- import { addParticlesFromCanvasPixels, getCanvasImageData, getImageData, getTextData } from "./utils";
1
+ import { addParticlesFromCanvasPixels, getCanvasImageData, getImageData, getTextData } from "./utils.js";
2
2
  export class CanvasMaskInstance {
3
3
  constructor(container, engine) {
4
4
  this._container = container;
@@ -6,7 +6,7 @@ export class CanvasMaskInstance {
6
6
  }
7
7
  async init() {
8
8
  const container = this._container, options = container.actualOptions.canvasMask;
9
- if (!(options === null || options === void 0 ? void 0 : options.enable)) {
9
+ if (!options?.enable) {
10
10
  return;
11
11
  }
12
12
  let pixelData = {
@@ -1,7 +1,7 @@
1
- import { CanvasMaskOverride } from "./CanvasMaskOverride";
2
- import { CanvasMaskPixels } from "./CanvasMaskPixels";
3
- import { ImageMask } from "./ImageMask";
4
- import { TextMask } from "./TextMask";
1
+ import { CanvasMaskOverride } from "./CanvasMaskOverride.js";
2
+ import { CanvasMaskPixels } from "./CanvasMaskPixels.js";
3
+ import { ImageMask } from "./ImageMask.js";
4
+ import { TextMask } from "./TextMask.js";
5
5
  export class CanvasMask {
6
6
  constructor() {
7
7
  this.enable = false;
@@ -14,7 +14,6 @@ export class CanvasMask {
14
14
  this.scale = 1;
15
15
  }
16
16
  load(data) {
17
- var _a, _b;
18
17
  if (!data) {
19
18
  return;
20
19
  }
@@ -33,8 +32,8 @@ export class CanvasMask {
33
32
  this.pixels.load(data.pixels);
34
33
  if (data.position) {
35
34
  this.position = {
36
- x: (_a = data.position.x) !== null && _a !== void 0 ? _a : this.position.x,
37
- y: (_b = data.position.y) !== null && _b !== void 0 ? _b : this.position.y,
35
+ x: data.position.x ?? this.position.x,
36
+ y: data.position.y ?? this.position.y,
38
37
  };
39
38
  }
40
39
  this.override.load(data.override);
@@ -1,3 +1,4 @@
1
+ import { isFunction, isString } from "@tsparticles/engine";
1
2
  export class CanvasMaskPixels {
2
3
  constructor() {
3
4
  this.filter = (pixel) => pixel.a > 0;
@@ -8,10 +9,10 @@ export class CanvasMaskPixels {
8
9
  return;
9
10
  }
10
11
  if (data.filter !== undefined) {
11
- if (typeof data.filter === "string") {
12
+ if (isString(data.filter)) {
12
13
  if (Object.hasOwn(window, data.filter)) {
13
14
  const filter = window[data.filter];
14
- if (typeof filter === "function") {
15
+ if (isFunction(filter)) {
15
16
  this.filter = filter;
16
17
  }
17
18
  }
@@ -1,5 +1,5 @@
1
- import { FontTextMask } from "./FontTextMask";
2
- import { TextMaskLine } from "./TextMaskLine";
1
+ import { FontTextMask } from "./FontTextMask.js";
2
+ import { TextMaskLine } from "./TextMaskLine.js";
3
3
  export class TextMask {
4
4
  constructor() {
5
5
  this.color = "#000000";
package/browser/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { CanvasMask } from "./Options/Classes/CanvasMask";
2
- import { CanvasMaskInstance } from "./CanvasMaskInstance";
1
+ import { CanvasMask } from "./Options/Classes/CanvasMask.js";
2
+ import { CanvasMaskInstance } from "./CanvasMaskInstance.js";
3
3
  class CanvasMaskPlugin {
4
4
  constructor(engine) {
5
5
  this.id = "canvasMask";
@@ -9,21 +9,19 @@ class CanvasMaskPlugin {
9
9
  return new CanvasMaskInstance(container, this._engine);
10
10
  }
11
11
  loadOptions(options, source) {
12
- if (!this.needsPlugin(source)) {
12
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
13
13
  return;
14
14
  }
15
15
  let canvasMaskOptions = options.canvasMask;
16
- if ((canvasMaskOptions === null || canvasMaskOptions === void 0 ? void 0 : canvasMaskOptions.load) === undefined) {
16
+ if (canvasMaskOptions?.load === undefined) {
17
17
  options.canvasMask = canvasMaskOptions = new CanvasMask();
18
18
  }
19
- canvasMaskOptions.load(source === null || source === void 0 ? void 0 : source.canvasMask);
19
+ canvasMaskOptions.load(source?.canvasMask);
20
20
  }
21
21
  needsPlugin(options) {
22
- var _a, _b;
23
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.canvasMask) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
22
+ return options?.canvasMask?.enable ?? false;
24
23
  }
25
24
  }
26
- export async function loadCanvasMaskPlugin(engine) {
27
- const plugin = new CanvasMaskPlugin(engine);
28
- await engine.addPlugin(plugin);
25
+ export async function loadCanvasMaskPlugin(engine, refresh = true) {
26
+ await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
29
27
  }
@@ -0,0 +1 @@
1
+ { "type": "module" }
package/browser/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getRandom } from "@tsparticles/engine";
1
+ import { errorPrefix, getRandom, isNumber, } from "@tsparticles/engine";
2
2
  export function shuffle(array) {
3
3
  for (let currentIndex = array.length - 1; currentIndex >= 0; currentIndex--) {
4
4
  const randomIndex = Math.floor(getRandom() * currentIndex);
@@ -18,25 +18,25 @@ export function addParticlesFromCanvasPixels(container, data, position, scale, o
18
18
  x: nextIndex % width,
19
19
  y: Math.floor(nextIndex / width),
20
20
  }, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = filter(pixel);
21
- if (shouldCreateParticle) {
22
- const pos = {
23
- x: pixelPos.x * scale + positionOffset.x,
24
- y: pixelPos.y * scale + positionOffset.y,
21
+ if (!shouldCreateParticle) {
22
+ continue;
23
+ }
24
+ const pos = {
25
+ x: pixelPos.x * scale + positionOffset.x,
26
+ y: pixelPos.y * scale + positionOffset.y,
27
+ }, pOptions = {};
28
+ if (override.color) {
29
+ pOptions.color = {
30
+ value: pixel,
31
+ };
32
+ }
33
+ if (override.opacity) {
34
+ pOptions.opacity = {
35
+ value: pixel.a,
25
36
  };
26
- const pOptions = {};
27
- if (override.color) {
28
- pOptions.color = {
29
- value: pixel,
30
- };
31
- }
32
- if (override.opacity) {
33
- pOptions.opacity = {
34
- value: pixel.a,
35
- };
36
- }
37
- container.particles.addParticle(pos, pOptions);
38
- selectedPixels++;
39
37
  }
38
+ container.particles.addParticle(pos, pOptions);
39
+ selectedPixels++;
40
40
  }
41
41
  }
42
42
  export function getCanvasImageData(ctx, size, offset, clear = true) {
@@ -77,7 +77,7 @@ export function getImageData(src, offset) {
77
77
  canvas.height = image.height;
78
78
  const context = canvas.getContext("2d");
79
79
  if (!context) {
80
- return reject(new Error("Could not get canvas context"));
80
+ return reject(new Error(`${errorPrefix} Could not get canvas context`));
81
81
  }
82
82
  context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
83
83
  resolve(getCanvasImageData(context, canvas, offset));
@@ -91,7 +91,7 @@ export function getTextData(textOptions, offset) {
91
91
  if (!text || !context) {
92
92
  return;
93
93
  }
94
- const lines = text.split(linesOptions.separator), fontSize = typeof font.size === "number" ? `${font.size}px` : font.size, linesData = [];
94
+ const lines = text.split(linesOptions.separator), fontSize = isNumber(font.size) ? `${font.size}px` : font.size, linesData = [];
95
95
  let maxWidth = 0, totalHeight = 0;
96
96
  for (const line of lines) {
97
97
  context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
@@ -1,61 +1,50 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.CanvasMaskInstance = void 0;
13
- const utils_1 = require("./utils");
4
+ const utils_js_1 = require("./utils.js");
14
5
  class CanvasMaskInstance {
15
6
  constructor(container, engine) {
16
7
  this._container = container;
17
8
  this._engine = engine;
18
9
  }
19
- init() {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- const container = this._container, options = container.actualOptions.canvasMask;
22
- if (!(options === null || options === void 0 ? void 0 : options.enable)) {
10
+ async init() {
11
+ const container = this._container, options = container.actualOptions.canvasMask;
12
+ if (!options?.enable) {
13
+ return;
14
+ }
15
+ let pixelData = {
16
+ pixels: [],
17
+ height: 0,
18
+ width: 0,
19
+ };
20
+ const offset = options.pixels.offset;
21
+ if (options.image) {
22
+ const url = options.image.src;
23
+ if (!url) {
23
24
  return;
24
25
  }
25
- let pixelData = {
26
- pixels: [],
27
- height: 0,
28
- width: 0,
29
- };
30
- const offset = options.pixels.offset;
31
- if (options.image) {
32
- const url = options.image.src;
33
- if (!url) {
34
- return;
35
- }
36
- pixelData = yield (0, utils_1.getImageData)(url, offset);
26
+ pixelData = await (0, utils_js_1.getImageData)(url, offset);
27
+ }
28
+ else if (options.text) {
29
+ const textOptions = options.text;
30
+ const data = (0, utils_js_1.getTextData)(textOptions, offset);
31
+ if (!data) {
32
+ return;
37
33
  }
38
- else if (options.text) {
39
- const textOptions = options.text;
40
- const data = (0, utils_1.getTextData)(textOptions, offset);
41
- if (!data) {
42
- return;
43
- }
44
- pixelData = data;
34
+ pixelData = data;
35
+ }
36
+ else if (options.element || options.selector) {
37
+ const canvas = options.element || (options.selector && document.querySelector(options.selector));
38
+ if (!canvas) {
39
+ return;
45
40
  }
46
- else if (options.element || options.selector) {
47
- const canvas = options.element || (options.selector && document.querySelector(options.selector));
48
- if (!canvas) {
49
- return;
50
- }
51
- const context = canvas.getContext("2d");
52
- if (!context) {
53
- return;
54
- }
55
- pixelData = (0, utils_1.getCanvasImageData)(context, canvas, offset);
41
+ const context = canvas.getContext("2d");
42
+ if (!context) {
43
+ return;
56
44
  }
57
- (0, utils_1.addParticlesFromCanvasPixels)(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
58
- });
45
+ pixelData = (0, utils_js_1.getCanvasImageData)(context, canvas, offset);
46
+ }
47
+ (0, utils_js_1.addParticlesFromCanvasPixels)(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
59
48
  }
60
49
  }
61
50
  exports.CanvasMaskInstance = CanvasMaskInstance;
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CanvasMask = void 0;
4
- const CanvasMaskOverride_1 = require("./CanvasMaskOverride");
5
- const CanvasMaskPixels_1 = require("./CanvasMaskPixels");
6
- const ImageMask_1 = require("./ImageMask");
7
- const TextMask_1 = require("./TextMask");
4
+ const CanvasMaskOverride_js_1 = require("./CanvasMaskOverride.js");
5
+ const CanvasMaskPixels_js_1 = require("./CanvasMaskPixels.js");
6
+ const ImageMask_js_1 = require("./ImageMask.js");
7
+ const TextMask_js_1 = require("./TextMask.js");
8
8
  class CanvasMask {
9
9
  constructor() {
10
10
  this.enable = false;
11
- this.override = new CanvasMaskOverride_1.CanvasMaskOverride();
12
- this.pixels = new CanvasMaskPixels_1.CanvasMaskPixels();
11
+ this.override = new CanvasMaskOverride_js_1.CanvasMaskOverride();
12
+ this.pixels = new CanvasMaskPixels_js_1.CanvasMaskPixels();
13
13
  this.position = {
14
14
  x: 50,
15
15
  y: 50,
@@ -17,7 +17,6 @@ class CanvasMask {
17
17
  this.scale = 1;
18
18
  }
19
19
  load(data) {
20
- var _a, _b;
21
20
  if (!data) {
22
21
  return;
23
22
  }
@@ -29,15 +28,15 @@ class CanvasMask {
29
28
  }
30
29
  if (data.image) {
31
30
  if (!this.image) {
32
- this.image = new ImageMask_1.ImageMask();
31
+ this.image = new ImageMask_js_1.ImageMask();
33
32
  }
34
33
  this.image.load(data.image);
35
34
  }
36
35
  this.pixels.load(data.pixels);
37
36
  if (data.position) {
38
37
  this.position = {
39
- x: (_a = data.position.x) !== null && _a !== void 0 ? _a : this.position.x,
40
- y: (_b = data.position.y) !== null && _b !== void 0 ? _b : this.position.y,
38
+ x: data.position.x ?? this.position.x,
39
+ y: data.position.y ?? this.position.y,
41
40
  };
42
41
  }
43
42
  this.override.load(data.override);
@@ -49,7 +48,7 @@ class CanvasMask {
49
48
  }
50
49
  if (data.text) {
51
50
  if (!this.text) {
52
- this.text = new TextMask_1.TextMask();
51
+ this.text = new TextMask_js_1.TextMask();
53
52
  }
54
53
  this.text.load(data.text);
55
54
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CanvasMaskPixels = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
4
5
  class CanvasMaskPixels {
5
6
  constructor() {
6
7
  this.filter = (pixel) => pixel.a > 0;
@@ -11,10 +12,10 @@ class CanvasMaskPixels {
11
12
  return;
12
13
  }
13
14
  if (data.filter !== undefined) {
14
- if (typeof data.filter === "string") {
15
+ if ((0, engine_1.isString)(data.filter)) {
15
16
  if (Object.hasOwn(window, data.filter)) {
16
17
  const filter = window[data.filter];
17
- if (typeof filter === "function") {
18
+ if ((0, engine_1.isFunction)(filter)) {
18
19
  this.filter = filter;
19
20
  }
20
21
  }
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TextMask = void 0;
4
- const FontTextMask_1 = require("./FontTextMask");
5
- const TextMaskLine_1 = require("./TextMaskLine");
4
+ const FontTextMask_js_1 = require("./FontTextMask.js");
5
+ const TextMaskLine_js_1 = require("./TextMaskLine.js");
6
6
  class TextMask {
7
7
  constructor() {
8
8
  this.color = "#000000";
9
- this.font = new FontTextMask_1.FontTextMask();
10
- this.lines = new TextMaskLine_1.TextMaskLine();
9
+ this.font = new FontTextMask_js_1.FontTextMask();
10
+ this.lines = new TextMaskLine_js_1.TextMaskLine();
11
11
  this.text = "";
12
12
  }
13
13
  load(data) {
package/cjs/index.js CHANGED
@@ -1,44 +1,31 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.loadCanvasMaskPlugin = void 0;
13
- const CanvasMask_1 = require("./Options/Classes/CanvasMask");
14
- const CanvasMaskInstance_1 = require("./CanvasMaskInstance");
4
+ const CanvasMask_js_1 = require("./Options/Classes/CanvasMask.js");
5
+ const CanvasMaskInstance_js_1 = require("./CanvasMaskInstance.js");
15
6
  class CanvasMaskPlugin {
16
7
  constructor(engine) {
17
8
  this.id = "canvasMask";
18
9
  this._engine = engine;
19
10
  }
20
11
  getPlugin(container) {
21
- return new CanvasMaskInstance_1.CanvasMaskInstance(container, this._engine);
12
+ return new CanvasMaskInstance_js_1.CanvasMaskInstance(container, this._engine);
22
13
  }
23
14
  loadOptions(options, source) {
24
- if (!this.needsPlugin(source)) {
15
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
25
16
  return;
26
17
  }
27
18
  let canvasMaskOptions = options.canvasMask;
28
- if ((canvasMaskOptions === null || canvasMaskOptions === void 0 ? void 0 : canvasMaskOptions.load) === undefined) {
29
- options.canvasMask = canvasMaskOptions = new CanvasMask_1.CanvasMask();
19
+ if (canvasMaskOptions?.load === undefined) {
20
+ options.canvasMask = canvasMaskOptions = new CanvasMask_js_1.CanvasMask();
30
21
  }
31
- canvasMaskOptions.load(source === null || source === void 0 ? void 0 : source.canvasMask);
22
+ canvasMaskOptions.load(source?.canvasMask);
32
23
  }
33
24
  needsPlugin(options) {
34
- var _a, _b;
35
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.canvasMask) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
25
+ return options?.canvasMask?.enable ?? false;
36
26
  }
37
27
  }
38
- function loadCanvasMaskPlugin(engine) {
39
- return __awaiter(this, void 0, void 0, function* () {
40
- const plugin = new CanvasMaskPlugin(engine);
41
- yield engine.addPlugin(plugin);
42
- });
28
+ async function loadCanvasMaskPlugin(engine, refresh = true) {
29
+ await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
43
30
  }
44
31
  exports.loadCanvasMaskPlugin = loadCanvasMaskPlugin;
@@ -0,0 +1 @@
1
+ { "type": "commonjs" }
package/cjs/utils.js CHANGED
@@ -22,25 +22,25 @@ function addParticlesFromCanvasPixels(container, data, position, scale, override
22
22
  x: nextIndex % width,
23
23
  y: Math.floor(nextIndex / width),
24
24
  }, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = filter(pixel);
25
- if (shouldCreateParticle) {
26
- const pos = {
27
- x: pixelPos.x * scale + positionOffset.x,
28
- y: pixelPos.y * scale + positionOffset.y,
25
+ if (!shouldCreateParticle) {
26
+ continue;
27
+ }
28
+ const pos = {
29
+ x: pixelPos.x * scale + positionOffset.x,
30
+ y: pixelPos.y * scale + positionOffset.y,
31
+ }, pOptions = {};
32
+ if (override.color) {
33
+ pOptions.color = {
34
+ value: pixel,
35
+ };
36
+ }
37
+ if (override.opacity) {
38
+ pOptions.opacity = {
39
+ value: pixel.a,
29
40
  };
30
- const pOptions = {};
31
- if (override.color) {
32
- pOptions.color = {
33
- value: pixel,
34
- };
35
- }
36
- if (override.opacity) {
37
- pOptions.opacity = {
38
- value: pixel.a,
39
- };
40
- }
41
- container.particles.addParticle(pos, pOptions);
42
- selectedPixels++;
43
41
  }
42
+ container.particles.addParticle(pos, pOptions);
43
+ selectedPixels++;
44
44
  }
45
45
  }
46
46
  exports.addParticlesFromCanvasPixels = addParticlesFromCanvasPixels;
@@ -83,7 +83,7 @@ function getImageData(src, offset) {
83
83
  canvas.height = image.height;
84
84
  const context = canvas.getContext("2d");
85
85
  if (!context) {
86
- return reject(new Error("Could not get canvas context"));
86
+ return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
87
87
  }
88
88
  context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
89
89
  resolve(getCanvasImageData(context, canvas, offset));
@@ -98,7 +98,7 @@ function getTextData(textOptions, offset) {
98
98
  if (!text || !context) {
99
99
  return;
100
100
  }
101
- const lines = text.split(linesOptions.separator), fontSize = typeof font.size === "number" ? `${font.size}px` : font.size, linesData = [];
101
+ const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size}px` : font.size, linesData = [];
102
102
  let maxWidth = 0, totalHeight = 0;
103
103
  for (const line of lines) {
104
104
  context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
@@ -1,4 +1,4 @@
1
- import { addParticlesFromCanvasPixels, getCanvasImageData, getImageData, getTextData } from "./utils";
1
+ import { addParticlesFromCanvasPixels, getCanvasImageData, getImageData, getTextData } from "./utils.js";
2
2
  export class CanvasMaskInstance {
3
3
  constructor(container, engine) {
4
4
  this._container = container;
@@ -6,7 +6,7 @@ export class CanvasMaskInstance {
6
6
  }
7
7
  async init() {
8
8
  const container = this._container, options = container.actualOptions.canvasMask;
9
- if (!(options === null || options === void 0 ? void 0 : options.enable)) {
9
+ if (!options?.enable) {
10
10
  return;
11
11
  }
12
12
  let pixelData = {
@@ -1,7 +1,7 @@
1
- import { CanvasMaskOverride } from "./CanvasMaskOverride";
2
- import { CanvasMaskPixels } from "./CanvasMaskPixels";
3
- import { ImageMask } from "./ImageMask";
4
- import { TextMask } from "./TextMask";
1
+ import { CanvasMaskOverride } from "./CanvasMaskOverride.js";
2
+ import { CanvasMaskPixels } from "./CanvasMaskPixels.js";
3
+ import { ImageMask } from "./ImageMask.js";
4
+ import { TextMask } from "./TextMask.js";
5
5
  export class CanvasMask {
6
6
  constructor() {
7
7
  this.enable = false;
@@ -14,7 +14,6 @@ export class CanvasMask {
14
14
  this.scale = 1;
15
15
  }
16
16
  load(data) {
17
- var _a, _b;
18
17
  if (!data) {
19
18
  return;
20
19
  }
@@ -33,8 +32,8 @@ export class CanvasMask {
33
32
  this.pixels.load(data.pixels);
34
33
  if (data.position) {
35
34
  this.position = {
36
- x: (_a = data.position.x) !== null && _a !== void 0 ? _a : this.position.x,
37
- y: (_b = data.position.y) !== null && _b !== void 0 ? _b : this.position.y,
35
+ x: data.position.x ?? this.position.x,
36
+ y: data.position.y ?? this.position.y,
38
37
  };
39
38
  }
40
39
  this.override.load(data.override);
@@ -1,3 +1,4 @@
1
+ import { isFunction, isString } from "@tsparticles/engine";
1
2
  export class CanvasMaskPixels {
2
3
  constructor() {
3
4
  this.filter = (pixel) => pixel.a > 0;
@@ -8,10 +9,10 @@ export class CanvasMaskPixels {
8
9
  return;
9
10
  }
10
11
  if (data.filter !== undefined) {
11
- if (typeof data.filter === "string") {
12
+ if (isString(data.filter)) {
12
13
  if (Object.hasOwn(window, data.filter)) {
13
14
  const filter = window[data.filter];
14
- if (typeof filter === "function") {
15
+ if (isFunction(filter)) {
15
16
  this.filter = filter;
16
17
  }
17
18
  }
@@ -1,5 +1,5 @@
1
- import { FontTextMask } from "./FontTextMask";
2
- import { TextMaskLine } from "./TextMaskLine";
1
+ import { FontTextMask } from "./FontTextMask.js";
2
+ import { TextMaskLine } from "./TextMaskLine.js";
3
3
  export class TextMask {
4
4
  constructor() {
5
5
  this.color = "#000000";