@tsparticles/plugin-canvas-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 (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/CanvasMaskInstance.js +46 -0
  4. package/browser/Options/Classes/CanvasMask.js +54 -0
  5. package/browser/Options/Classes/CanvasMaskOverride.js +17 -0
  6. package/browser/Options/Classes/CanvasMaskPixels.js +27 -0
  7. package/browser/Options/Classes/FontTextMask.js +26 -0
  8. package/browser/Options/Classes/ImageMask.js +13 -0
  9. package/browser/Options/Classes/TextMask.js +23 -0
  10. package/browser/Options/Classes/TextMaskLine.js +17 -0
  11. package/browser/Options/Interfaces/ICanvasMask.js +1 -0
  12. package/browser/Options/Interfaces/ICanvasMaskOverride.js +1 -0
  13. package/browser/Options/Interfaces/ICanvasMaskPixels.js +1 -0
  14. package/browser/Options/Interfaces/IFontTextMask.js +1 -0
  15. package/browser/Options/Interfaces/IImageMask.js +1 -0
  16. package/browser/Options/Interfaces/ITextMask.js +1 -0
  17. package/browser/Options/Interfaces/ITextMaskLine.js +1 -0
  18. package/browser/index.js +29 -0
  19. package/browser/types.js +1 -0
  20. package/browser/utils.js +119 -0
  21. package/cjs/CanvasMaskInstance.js +61 -0
  22. package/cjs/Options/Classes/CanvasMask.js +58 -0
  23. package/cjs/Options/Classes/CanvasMaskOverride.js +21 -0
  24. package/cjs/Options/Classes/CanvasMaskPixels.js +31 -0
  25. package/cjs/Options/Classes/FontTextMask.js +30 -0
  26. package/cjs/Options/Classes/ImageMask.js +17 -0
  27. package/cjs/Options/Classes/TextMask.js +27 -0
  28. package/cjs/Options/Classes/TextMaskLine.js +21 -0
  29. package/cjs/Options/Interfaces/ICanvasMask.js +2 -0
  30. package/cjs/Options/Interfaces/ICanvasMaskOverride.js +2 -0
  31. package/cjs/Options/Interfaces/ICanvasMaskPixels.js +2 -0
  32. package/cjs/Options/Interfaces/IFontTextMask.js +2 -0
  33. package/cjs/Options/Interfaces/IImageMask.js +2 -0
  34. package/cjs/Options/Interfaces/ITextMask.js +2 -0
  35. package/cjs/Options/Interfaces/ITextMaskLine.js +2 -0
  36. package/cjs/index.js +44 -0
  37. package/cjs/types.js +2 -0
  38. package/cjs/utils.js +128 -0
  39. package/esm/CanvasMaskInstance.js +46 -0
  40. package/esm/Options/Classes/CanvasMask.js +54 -0
  41. package/esm/Options/Classes/CanvasMaskOverride.js +17 -0
  42. package/esm/Options/Classes/CanvasMaskPixels.js +27 -0
  43. package/esm/Options/Classes/FontTextMask.js +26 -0
  44. package/esm/Options/Classes/ImageMask.js +13 -0
  45. package/esm/Options/Classes/TextMask.js +23 -0
  46. package/esm/Options/Classes/TextMaskLine.js +17 -0
  47. package/esm/Options/Interfaces/ICanvasMask.js +1 -0
  48. package/esm/Options/Interfaces/ICanvasMaskOverride.js +1 -0
  49. package/esm/Options/Interfaces/ICanvasMaskPixels.js +1 -0
  50. package/esm/Options/Interfaces/IFontTextMask.js +1 -0
  51. package/esm/Options/Interfaces/IImageMask.js +1 -0
  52. package/esm/Options/Interfaces/ITextMask.js +1 -0
  53. package/esm/Options/Interfaces/ITextMaskLine.js +1 -0
  54. package/esm/index.js +29 -0
  55. package/esm/types.js +1 -0
  56. package/esm/utils.js +119 -0
  57. package/package.json +81 -0
  58. package/report.html +39 -0
  59. package/tsparticles.plugin.canvas-mask.js +505 -0
  60. package/tsparticles.plugin.canvas-mask.min.js +2 -0
  61. package/tsparticles.plugin.canvas-mask.min.js.LICENSE.txt +8 -0
  62. package/types/CanvasMaskInstance.d.ts +8 -0
  63. package/types/Options/Classes/CanvasMask.d.ts +19 -0
  64. package/types/Options/Classes/CanvasMaskOverride.d.ts +8 -0
  65. package/types/Options/Classes/CanvasMaskPixels.d.ts +8 -0
  66. package/types/Options/Classes/FontTextMask.d.ts +11 -0
  67. package/types/Options/Classes/ImageMask.d.ts +7 -0
  68. package/types/Options/Classes/TextMask.d.ts +12 -0
  69. package/types/Options/Classes/TextMaskLine.d.ts +8 -0
  70. package/types/Options/Interfaces/ICanvasMask.d.ts +16 -0
  71. package/types/Options/Interfaces/ICanvasMaskOverride.d.ts +4 -0
  72. package/types/Options/Interfaces/ICanvasMaskPixels.d.ts +5 -0
  73. package/types/Options/Interfaces/IFontTextMask.d.ts +7 -0
  74. package/types/Options/Interfaces/IImageMask.d.ts +3 -0
  75. package/types/Options/Interfaces/ITextMask.d.ts +8 -0
  76. package/types/Options/Interfaces/ITextMaskLine.d.ts +4 -0
  77. package/types/index.d.ts +2 -0
  78. package/types/types.d.ts +12 -0
  79. package/types/utils.d.ts +14 -0
  80. package/umd/CanvasMaskInstance.js +60 -0
  81. package/umd/Options/Classes/CanvasMask.js +68 -0
  82. package/umd/Options/Classes/CanvasMaskOverride.js +31 -0
  83. package/umd/Options/Classes/CanvasMaskPixels.js +41 -0
  84. package/umd/Options/Classes/FontTextMask.js +40 -0
  85. package/umd/Options/Classes/ImageMask.js +27 -0
  86. package/umd/Options/Classes/TextMask.js +37 -0
  87. package/umd/Options/Classes/TextMaskLine.js +31 -0
  88. package/umd/Options/Interfaces/ICanvasMask.js +12 -0
  89. package/umd/Options/Interfaces/ICanvasMaskOverride.js +12 -0
  90. package/umd/Options/Interfaces/ICanvasMaskPixels.js +12 -0
  91. package/umd/Options/Interfaces/IFontTextMask.js +12 -0
  92. package/umd/Options/Interfaces/IImageMask.js +12 -0
  93. package/umd/Options/Interfaces/ITextMask.js +12 -0
  94. package/umd/Options/Interfaces/ITextMaskLine.js +12 -0
  95. package/umd/index.js +43 -0
  96. package/umd/types.js +12 -0
  97. package/umd/utils.js +138 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Matteo Bruni
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ [![banner](https://particles.js.org/images/banner2.png)](https://particles.js.org)
2
+
3
+ # tsParticles Canvas Mask Plugin
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)
8
+
9
+ [tsParticles](https://github.com/matteobruni/tsparticles) plugin for particles canvas mask effect.
10
+
11
+ ## How to use it
12
+
13
+ ### CDN / Vanilla JS / jQuery
14
+
15
+ The CDN/Vanilla version JS has one required file in vanilla configuration:
16
+
17
+ Including the `tsparticles.plugin.canvas-mask.min.js` file will export the function to load the plugin:
18
+
19
+ ```javascript
20
+ loadCanvasMaskPlugin;
21
+ ```
22
+
23
+ ### Usage
24
+
25
+ Once the scripts are loaded you can set up `tsParticles` and the plugin like this:
26
+
27
+ ```javascript
28
+ (async () => {
29
+ await loadCanvasMaskPlugin(tsParticles);
30
+
31
+ await tsParticles.load({
32
+ id: "tsparticles",
33
+ options: {
34
+ /* options */
35
+ },
36
+ });
37
+ })();
38
+ ```
39
+
40
+ ### ESM / CommonJS
41
+
42
+ This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
43
+
44
+ ```shell
45
+ $ npm install tsparticles-plugin-canvas-mask
46
+ ```
47
+
48
+ or
49
+
50
+ ```shell
51
+ $ yarn add tsparticles-plugin-canvas-mask
52
+ ```
53
+
54
+ Then you need to import it in the app, like this:
55
+
56
+ ```javascript
57
+ const { tsParticles } = require("tsparticles-engine");
58
+ const { loadCanvasMaskPlugin } = require("tsparticles-plugin-canvas-mask");
59
+
60
+ loadCanvasMaskPlugin(tsParticles); // awaitable
61
+ ```
62
+
63
+ or
64
+
65
+ ```javascript
66
+ import { tsParticles } from "tsparticles-engine";
67
+ import { loadCanvasMaskPlugin } from "tsparticles-plugin-canvas-mask";
68
+
69
+ loadCanvasMaskPlugin(tsParticles); // awaitable
70
+ ```
@@ -0,0 +1,46 @@
1
+ import { addParticlesFromCanvasPixels, getCanvasImageData, getImageData, getTextData } from "./utils";
2
+ export class CanvasMaskInstance {
3
+ constructor(container, engine) {
4
+ this._container = container;
5
+ this._engine = engine;
6
+ }
7
+ async init() {
8
+ const container = this._container, options = container.actualOptions.canvasMask;
9
+ if (!(options === null || options === void 0 ? void 0 : options.enable)) {
10
+ return;
11
+ }
12
+ let pixelData = {
13
+ pixels: [],
14
+ height: 0,
15
+ width: 0,
16
+ };
17
+ const offset = options.pixels.offset;
18
+ if (options.image) {
19
+ const url = options.image.src;
20
+ if (!url) {
21
+ return;
22
+ }
23
+ pixelData = await getImageData(url, offset);
24
+ }
25
+ else if (options.text) {
26
+ const textOptions = options.text;
27
+ const data = getTextData(textOptions, offset);
28
+ if (!data) {
29
+ return;
30
+ }
31
+ pixelData = data;
32
+ }
33
+ else if (options.element || options.selector) {
34
+ const canvas = options.element || (options.selector && document.querySelector(options.selector));
35
+ if (!canvas) {
36
+ return;
37
+ }
38
+ const context = canvas.getContext("2d");
39
+ if (!context) {
40
+ return;
41
+ }
42
+ pixelData = getCanvasImageData(context, canvas, offset);
43
+ }
44
+ addParticlesFromCanvasPixels(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
45
+ }
46
+ }
@@ -0,0 +1,54 @@
1
+ import { CanvasMaskOverride } from "./CanvasMaskOverride";
2
+ import { CanvasMaskPixels } from "./CanvasMaskPixels";
3
+ import { ImageMask } from "./ImageMask";
4
+ import { TextMask } from "./TextMask";
5
+ export class CanvasMask {
6
+ constructor() {
7
+ this.enable = false;
8
+ this.override = new CanvasMaskOverride();
9
+ this.pixels = new CanvasMaskPixels();
10
+ this.position = {
11
+ x: 50,
12
+ y: 50,
13
+ };
14
+ this.scale = 1;
15
+ }
16
+ load(data) {
17
+ var _a, _b;
18
+ if (!data) {
19
+ return;
20
+ }
21
+ if (data.element !== undefined && data.element instanceof HTMLCanvasElement) {
22
+ this.element = data.element;
23
+ }
24
+ if (data.enable !== undefined) {
25
+ this.enable = data.enable;
26
+ }
27
+ if (data.image) {
28
+ if (!this.image) {
29
+ this.image = new ImageMask();
30
+ }
31
+ this.image.load(data.image);
32
+ }
33
+ this.pixels.load(data.pixels);
34
+ if (data.position) {
35
+ 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,
38
+ };
39
+ }
40
+ this.override.load(data.override);
41
+ if (data.scale !== undefined) {
42
+ this.scale = data.scale;
43
+ }
44
+ if (data.selector !== undefined) {
45
+ this.selector = data.selector;
46
+ }
47
+ if (data.text) {
48
+ if (!this.text) {
49
+ this.text = new TextMask();
50
+ }
51
+ this.text.load(data.text);
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,17 @@
1
+ export class CanvasMaskOverride {
2
+ constructor() {
3
+ this.color = true;
4
+ this.opacity = false;
5
+ }
6
+ load(data) {
7
+ if (!data) {
8
+ return;
9
+ }
10
+ if (data.color !== undefined) {
11
+ this.color = data.color;
12
+ }
13
+ if (data.opacity !== undefined) {
14
+ this.opacity = data.opacity;
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,27 @@
1
+ export class CanvasMaskPixels {
2
+ constructor() {
3
+ this.filter = (pixel) => pixel.a > 0;
4
+ this.offset = 4;
5
+ }
6
+ load(data) {
7
+ if (!data) {
8
+ return;
9
+ }
10
+ if (data.filter !== undefined) {
11
+ if (typeof data.filter === "string") {
12
+ if (Object.hasOwn(window, data.filter)) {
13
+ const filter = window[data.filter];
14
+ if (typeof filter === "function") {
15
+ this.filter = filter;
16
+ }
17
+ }
18
+ }
19
+ else {
20
+ this.filter = data.filter;
21
+ }
22
+ }
23
+ if (data.offset !== undefined) {
24
+ this.offset = data.offset;
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,26 @@
1
+ export class FontTextMask {
2
+ constructor() {
3
+ this.family = "sans-serif";
4
+ this.size = 100;
5
+ }
6
+ load(data) {
7
+ if (!data) {
8
+ return;
9
+ }
10
+ if (data.family !== undefined) {
11
+ this.family = data.family;
12
+ }
13
+ if (data.size !== undefined) {
14
+ this.size = data.size;
15
+ }
16
+ if (data.style !== undefined) {
17
+ this.style = data.style;
18
+ }
19
+ if (data.variant !== undefined) {
20
+ this.variant = data.variant;
21
+ }
22
+ if (data.weight !== undefined) {
23
+ this.weight = data.weight;
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,13 @@
1
+ export class ImageMask {
2
+ constructor() {
3
+ this.src = "";
4
+ }
5
+ load(data) {
6
+ if (!data) {
7
+ return;
8
+ }
9
+ if (data.src !== undefined) {
10
+ this.src = data.src;
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,23 @@
1
+ import { FontTextMask } from "./FontTextMask";
2
+ import { TextMaskLine } from "./TextMaskLine";
3
+ export class TextMask {
4
+ constructor() {
5
+ this.color = "#000000";
6
+ this.font = new FontTextMask();
7
+ this.lines = new TextMaskLine();
8
+ this.text = "";
9
+ }
10
+ load(data) {
11
+ if (!data) {
12
+ return;
13
+ }
14
+ if (data.color !== undefined) {
15
+ this.color = data.color;
16
+ }
17
+ this.font.load(data.font);
18
+ this.lines.load(data.lines);
19
+ if (data.text !== undefined) {
20
+ this.text = data.text;
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,17 @@
1
+ export class TextMaskLine {
2
+ constructor() {
3
+ this.separator = "\n";
4
+ this.spacing = 10;
5
+ }
6
+ load(data) {
7
+ if (!data) {
8
+ return;
9
+ }
10
+ if (data.separator !== undefined) {
11
+ this.separator = data.separator;
12
+ }
13
+ if (data.spacing !== undefined) {
14
+ this.spacing = data.spacing;
15
+ }
16
+ }
17
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { CanvasMask } from "./Options/Classes/CanvasMask";
2
+ import { CanvasMaskInstance } from "./CanvasMaskInstance";
3
+ class CanvasMaskPlugin {
4
+ constructor(engine) {
5
+ this.id = "canvasMask";
6
+ this._engine = engine;
7
+ }
8
+ getPlugin(container) {
9
+ return new CanvasMaskInstance(container, this._engine);
10
+ }
11
+ loadOptions(options, source) {
12
+ if (!this.needsPlugin(source)) {
13
+ return;
14
+ }
15
+ let canvasMaskOptions = options.canvasMask;
16
+ if ((canvasMaskOptions === null || canvasMaskOptions === void 0 ? void 0 : canvasMaskOptions.load) === undefined) {
17
+ options.canvasMask = canvasMaskOptions = new CanvasMask();
18
+ }
19
+ canvasMaskOptions.load(source === null || source === void 0 ? void 0 : source.canvasMask);
20
+ }
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;
24
+ }
25
+ }
26
+ export async function loadCanvasMaskPlugin(engine) {
27
+ const plugin = new CanvasMaskPlugin(engine);
28
+ await engine.addPlugin(plugin);
29
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,119 @@
1
+ import { getRandom } from "@tsparticles/engine";
2
+ export function shuffle(array) {
3
+ for (let currentIndex = array.length - 1; currentIndex >= 0; currentIndex--) {
4
+ const randomIndex = Math.floor(getRandom() * currentIndex);
5
+ [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
6
+ }
7
+ return array;
8
+ }
9
+ export function addParticlesFromCanvasPixels(container, data, position, scale, override, filter) {
10
+ const { height, width } = data, numPixels = height * width, indexArray = shuffle(range(numPixels)), maxParticles = Math.min(numPixels, container.actualOptions.particles.number.value), canvasSize = container.canvas.size;
11
+ let selectedPixels = 0;
12
+ const positionOffset = {
13
+ x: (canvasSize.width * position.x) / 100 - (width * scale) / 2,
14
+ y: (canvasSize.height * position.y) / 100 - (height * scale) / 2,
15
+ };
16
+ while (selectedPixels < maxParticles && indexArray.length) {
17
+ const nextIndex = indexArray.pop() || 0, pixelPos = {
18
+ x: nextIndex % width,
19
+ y: Math.floor(nextIndex / width),
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,
25
+ };
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
+ }
40
+ }
41
+ }
42
+ export function getCanvasImageData(ctx, size, offset, clear = true) {
43
+ const imageData = ctx.getImageData(0, 0, size.width, size.height).data;
44
+ if (clear) {
45
+ ctx.clearRect(0, 0, size.width, size.height);
46
+ }
47
+ const pixels = [];
48
+ for (let i = 0; i < imageData.length; i += offset) {
49
+ const idx = i / offset, pos = {
50
+ x: idx % size.width,
51
+ y: Math.floor(idx / size.width),
52
+ };
53
+ if (!pixels[pos.y]) {
54
+ pixels[pos.y] = [];
55
+ }
56
+ pixels[pos.y][pos.x] = {
57
+ r: imageData[i],
58
+ g: imageData[i + 1],
59
+ b: imageData[i + 2],
60
+ a: imageData[i + 3] / 255,
61
+ };
62
+ }
63
+ return {
64
+ pixels,
65
+ width: Math.min(...pixels.map((row) => row.length)),
66
+ height: pixels.length,
67
+ };
68
+ }
69
+ export function getImageData(src, offset) {
70
+ const image = new Image();
71
+ image.crossOrigin = "Anonymous";
72
+ const p = new Promise((resolve, reject) => {
73
+ image.onerror = reject;
74
+ image.onload = () => {
75
+ const canvas = document.createElement("canvas");
76
+ canvas.width = image.width;
77
+ canvas.height = image.height;
78
+ const context = canvas.getContext("2d");
79
+ if (!context) {
80
+ return reject(new Error("Could not get canvas context"));
81
+ }
82
+ context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
83
+ resolve(getCanvasImageData(context, canvas, offset));
84
+ };
85
+ });
86
+ image.src = src;
87
+ return p;
88
+ }
89
+ export function getTextData(textOptions, offset) {
90
+ const canvas = document.createElement("canvas"), context = canvas.getContext("2d"), { font, text, lines: linesOptions, color } = textOptions;
91
+ if (!text || !context) {
92
+ return;
93
+ }
94
+ const lines = text.split(linesOptions.separator), fontSize = typeof font.size === "number" ? `${font.size}px` : font.size, linesData = [];
95
+ let maxWidth = 0, totalHeight = 0;
96
+ for (const line of lines) {
97
+ context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
98
+ const measure = context.measureText(line), lineData = {
99
+ measure,
100
+ text: line,
101
+ height: measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent,
102
+ width: measure.width,
103
+ };
104
+ maxWidth = Math.max(maxWidth || 0, lineData.width);
105
+ totalHeight += lineData.height + linesOptions.spacing;
106
+ linesData.push(lineData);
107
+ }
108
+ canvas.width = maxWidth;
109
+ canvas.height = totalHeight;
110
+ let currentHeight = 0;
111
+ for (const line of linesData) {
112
+ context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
113
+ context.fillStyle = color;
114
+ context.fillText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
115
+ currentHeight += line.height + linesOptions.spacing;
116
+ }
117
+ return getCanvasImageData(context, canvas, offset);
118
+ }
119
+ export const range = (n) => [...Array(n).keys()];
@@ -0,0 +1,61 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CanvasMaskInstance = void 0;
13
+ const utils_1 = require("./utils");
14
+ class CanvasMaskInstance {
15
+ constructor(container, engine) {
16
+ this._container = container;
17
+ this._engine = engine;
18
+ }
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)) {
23
+ return;
24
+ }
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);
37
+ }
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;
45
+ }
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);
56
+ }
57
+ (0, utils_1.addParticlesFromCanvasPixels)(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
58
+ });
59
+ }
60
+ }
61
+ exports.CanvasMaskInstance = CanvasMaskInstance;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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");
8
+ class CanvasMask {
9
+ constructor() {
10
+ this.enable = false;
11
+ this.override = new CanvasMaskOverride_1.CanvasMaskOverride();
12
+ this.pixels = new CanvasMaskPixels_1.CanvasMaskPixels();
13
+ this.position = {
14
+ x: 50,
15
+ y: 50,
16
+ };
17
+ this.scale = 1;
18
+ }
19
+ load(data) {
20
+ var _a, _b;
21
+ if (!data) {
22
+ return;
23
+ }
24
+ if (data.element !== undefined && data.element instanceof HTMLCanvasElement) {
25
+ this.element = data.element;
26
+ }
27
+ if (data.enable !== undefined) {
28
+ this.enable = data.enable;
29
+ }
30
+ if (data.image) {
31
+ if (!this.image) {
32
+ this.image = new ImageMask_1.ImageMask();
33
+ }
34
+ this.image.load(data.image);
35
+ }
36
+ this.pixels.load(data.pixels);
37
+ if (data.position) {
38
+ 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,
41
+ };
42
+ }
43
+ this.override.load(data.override);
44
+ if (data.scale !== undefined) {
45
+ this.scale = data.scale;
46
+ }
47
+ if (data.selector !== undefined) {
48
+ this.selector = data.selector;
49
+ }
50
+ if (data.text) {
51
+ if (!this.text) {
52
+ this.text = new TextMask_1.TextMask();
53
+ }
54
+ this.text.load(data.text);
55
+ }
56
+ }
57
+ }
58
+ exports.CanvasMask = CanvasMask;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CanvasMaskOverride = void 0;
4
+ class CanvasMaskOverride {
5
+ constructor() {
6
+ this.color = true;
7
+ this.opacity = false;
8
+ }
9
+ load(data) {
10
+ if (!data) {
11
+ return;
12
+ }
13
+ if (data.color !== undefined) {
14
+ this.color = data.color;
15
+ }
16
+ if (data.opacity !== undefined) {
17
+ this.opacity = data.opacity;
18
+ }
19
+ }
20
+ }
21
+ exports.CanvasMaskOverride = CanvasMaskOverride;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CanvasMaskPixels = void 0;
4
+ class CanvasMaskPixels {
5
+ constructor() {
6
+ this.filter = (pixel) => pixel.a > 0;
7
+ this.offset = 4;
8
+ }
9
+ load(data) {
10
+ if (!data) {
11
+ return;
12
+ }
13
+ if (data.filter !== undefined) {
14
+ if (typeof data.filter === "string") {
15
+ if (Object.hasOwn(window, data.filter)) {
16
+ const filter = window[data.filter];
17
+ if (typeof filter === "function") {
18
+ this.filter = filter;
19
+ }
20
+ }
21
+ }
22
+ else {
23
+ this.filter = data.filter;
24
+ }
25
+ }
26
+ if (data.offset !== undefined) {
27
+ this.offset = data.offset;
28
+ }
29
+ }
30
+ }
31
+ exports.CanvasMaskPixels = CanvasMaskPixels;