@tsparticles/plugin-canvas-mask 3.9.1 → 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 (46) hide show
  1. package/76.min.js +2 -0
  2. package/76.min.js.LICENSE.txt +1 -0
  3. package/browser/CanvasMaskInstance.js +3 -2
  4. package/browser/Options/Classes/CanvasMask.js +2 -6
  5. package/browser/Options/Classes/CanvasMaskPixels.js +2 -2
  6. package/browser/index.js +6 -4
  7. package/browser/utils.js +37 -20
  8. package/cjs/CanvasMaskInstance.js +10 -13
  9. package/cjs/CanvasMaskPlugin.js +5 -9
  10. package/cjs/Options/Classes/CanvasMask.js +11 -19
  11. package/cjs/Options/Classes/CanvasMaskOverride.js +3 -7
  12. package/cjs/Options/Classes/CanvasMaskPixels.js +7 -11
  13. package/cjs/Options/Classes/FontTextMask.js +3 -7
  14. package/cjs/Options/Classes/ImageMask.js +3 -7
  15. package/cjs/Options/Classes/TextMask.js +7 -11
  16. package/cjs/Options/Classes/TextMaskLine.js +3 -7
  17. package/cjs/Options/Interfaces/ICanvasMask.js +1 -2
  18. package/cjs/Options/Interfaces/ICanvasMaskOverride.js +1 -2
  19. package/cjs/Options/Interfaces/ICanvasMaskPixels.js +1 -2
  20. package/cjs/Options/Interfaces/IFontTextMask.js +1 -2
  21. package/cjs/Options/Interfaces/IImageMask.js +1 -2
  22. package/cjs/Options/Interfaces/ITextMask.js +1 -2
  23. package/cjs/Options/Interfaces/ITextMaskLine.js +1 -2
  24. package/cjs/index.js +6 -7
  25. package/cjs/types.js +1 -2
  26. package/cjs/utils.js +43 -32
  27. package/dist_browser_CanvasMaskPlugin_js.js +120 -0
  28. package/esm/CanvasMaskInstance.js +3 -2
  29. package/esm/Options/Classes/CanvasMask.js +2 -6
  30. package/esm/Options/Classes/CanvasMaskPixels.js +2 -2
  31. package/esm/index.js +6 -4
  32. package/esm/utils.js +37 -20
  33. package/package.json +4 -3
  34. package/report.html +5 -4
  35. package/tsparticles.plugin.canvas-mask.js +209 -120
  36. package/tsparticles.plugin.canvas-mask.min.js +1 -1
  37. package/tsparticles.plugin.canvas-mask.min.js.LICENSE.txt +1 -1
  38. package/types/Options/Classes/CanvasMaskPixels.d.ts +1 -1
  39. package/types/Options/Classes/FontTextMask.d.ts +1 -1
  40. package/types/Options/Classes/TextMaskLine.d.ts +1 -1
  41. package/types/index.d.ts +1 -1
  42. package/umd/CanvasMaskInstance.js +2 -1
  43. package/umd/Options/Classes/CanvasMask.js +2 -6
  44. package/umd/Options/Classes/CanvasMaskPixels.js +2 -2
  45. package/umd/index.js +41 -5
  46. package/umd/utils.js +36 -19
package/umd/utils.js CHANGED
@@ -17,7 +17,7 @@
17
17
  const half = 0.5, origin = {
18
18
  x: 0,
19
19
  y: 0,
20
- }, defaultWidth = 0;
20
+ }, defaultWidth = 0, defaultRgb = 0, defaultAlpha = 1;
21
21
  function addParticlesFromCanvasPixels(container, data, position, scale, override, filter) {
22
22
  const { height, width } = data, numPixels = height * width, indexArray = shuffle(range(numPixels)), maxParticles = Math.min(numPixels, container.actualOptions.particles.number.value), canvasSize = container.canvas.size;
23
23
  let selectedPixels = 0;
@@ -29,7 +29,15 @@
29
29
  const defaultIndex = 0, nextIndex = indexArray.pop() ?? defaultIndex, pixelPos = {
30
30
  x: nextIndex % width,
31
31
  y: Math.floor(nextIndex / width),
32
- }, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = filter(pixel);
32
+ }, row = data.pixels[pixelPos.y];
33
+ if (!row) {
34
+ continue;
35
+ }
36
+ const pixel = row[pixelPos.x];
37
+ if (!pixel) {
38
+ continue;
39
+ }
40
+ const shouldCreateParticle = filter(pixel);
33
41
  if (!shouldCreateParticle) {
34
42
  continue;
35
43
  }
@@ -62,20 +70,21 @@
62
70
  x: idx % size.width,
63
71
  y: Math.floor(idx / size.width),
64
72
  };
65
- if (!pixels[pos.y]) {
66
- pixels[pos.y] = [];
67
- }
73
+ pixels[pos.y] ??= [];
68
74
  const indexesOffset = {
69
75
  r: 0,
70
76
  g: 1,
71
77
  b: 2,
72
78
  a: 3,
73
- }, alphaFactor = 255;
74
- pixels[pos.y][pos.x] = {
75
- r: imageData[i + indexesOffset.r],
76
- g: imageData[i + indexesOffset.g],
77
- b: imageData[i + indexesOffset.b],
78
- a: imageData[i + indexesOffset.a] / alphaFactor,
79
+ }, alphaFactor = 255, row = pixels[pos.y];
80
+ if (!row) {
81
+ continue;
82
+ }
83
+ row[pos.x] = {
84
+ r: imageData[i + indexesOffset.r] ?? defaultRgb,
85
+ g: imageData[i + indexesOffset.g] ?? defaultRgb,
86
+ b: imageData[i + indexesOffset.b] ?? defaultRgb,
87
+ a: (imageData[i + indexesOffset.a] ?? defaultAlpha) / alphaFactor,
79
88
  };
80
89
  }
81
90
  return {
@@ -90,12 +99,13 @@
90
99
  const p = new Promise((resolve, reject) => {
91
100
  image.onerror = reject;
92
101
  image.onload = () => {
93
- const canvas = document.createElement("canvas");
102
+ const canvas = (0, engine_1.safeDocument)().createElement("canvas");
94
103
  canvas.width = image.width;
95
104
  canvas.height = image.height;
96
105
  const context = canvas.getContext("2d");
97
106
  if (!context) {
98
- return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
107
+ reject(new Error("Could not get canvas context"));
108
+ return;
99
109
  }
100
110
  context.drawImage(image, origin.x, origin.y, image.width, image.height, origin.x, origin.y, canvas.width, canvas.height);
101
111
  resolve(getCanvasImageData(context, canvas, offset));
@@ -105,14 +115,14 @@
105
115
  return p;
106
116
  }
107
117
  function getTextData(textOptions, offset) {
108
- const canvas = document.createElement("canvas"), context = canvas.getContext("2d"), { font, text, lines: linesOptions, color } = textOptions;
118
+ const canvas = (0, engine_1.safeDocument)().createElement("canvas"), context = canvas.getContext("2d"), { font, text, lines: linesOptions, color } = textOptions;
109
119
  if (!text || !context) {
110
120
  return;
111
121
  }
112
- const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size}px` : font.size, linesData = [];
122
+ const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size.toString()}px` : font.size, linesData = [];
113
123
  let maxWidth = 0, totalHeight = 0;
114
124
  for (const line of lines) {
115
- context.font = `${font.style ?? ""} ${font.variant ?? ""} ${font.weight ?? ""} ${fontSize} ${font.family}`;
125
+ context.font = `${font.style ?? ""} ${font.variant ?? ""} ${font.weight?.toString() ?? ""} ${fontSize} ${font.family}`;
116
126
  const measure = context.measureText(line), lineData = {
117
127
  measure,
118
128
  text: line,
@@ -127,7 +137,7 @@
127
137
  canvas.height = totalHeight;
128
138
  let currentHeight = 0;
129
139
  for (const line of linesData) {
130
- context.font = `${font.style ?? ""} ${font.variant ?? ""} ${font.weight ?? ""} ${fontSize} ${font.family}`;
140
+ context.font = `${font.style ?? ""} ${font.variant ?? ""} ${font.weight?.toString() ?? ""} ${fontSize} ${font.family}`;
131
141
  context.fillStyle = color;
132
142
  context.fillText(line.text, origin.x, currentHeight + line.measure.actualBoundingBoxAscent);
133
143
  currentHeight += line.height + linesOptions.spacing;
@@ -137,8 +147,15 @@
137
147
  function shuffle(array) {
138
148
  const lengthOffset = 1, minIndex = 0;
139
149
  for (let currentIndex = array.length - lengthOffset; currentIndex >= minIndex; currentIndex--) {
140
- const randomIndex = Math.floor((0, engine_1.getRandom)() * currentIndex);
141
- [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
150
+ const randomIndex = Math.floor((0, engine_1.getRandom)() * currentIndex), currentItem = array[currentIndex], randomItem = array[randomIndex];
151
+ if (randomItem === currentItem) {
152
+ continue;
153
+ }
154
+ if (randomItem === undefined || currentItem === undefined) {
155
+ continue;
156
+ }
157
+ array[currentIndex] = randomItem;
158
+ array[randomIndex] = currentItem;
142
159
  }
143
160
  return array;
144
161
  }