@tsparticles/plugin-canvas-mask 3.9.1 → 4.0.0-alpha.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.
- package/76.min.js +2 -0
- package/76.min.js.LICENSE.txt +1 -0
- package/browser/CanvasMaskInstance.js +3 -2
- package/browser/Options/Classes/CanvasMask.js +2 -6
- package/browser/Options/Classes/CanvasMaskPixels.js +2 -2
- package/browser/index.js +6 -4
- package/browser/utils.js +37 -20
- package/cjs/CanvasMaskInstance.js +10 -13
- package/cjs/CanvasMaskPlugin.js +5 -9
- package/cjs/Options/Classes/CanvasMask.js +11 -19
- package/cjs/Options/Classes/CanvasMaskOverride.js +3 -7
- package/cjs/Options/Classes/CanvasMaskPixels.js +7 -11
- package/cjs/Options/Classes/FontTextMask.js +3 -7
- package/cjs/Options/Classes/ImageMask.js +3 -7
- package/cjs/Options/Classes/TextMask.js +7 -11
- package/cjs/Options/Classes/TextMaskLine.js +3 -7
- package/cjs/Options/Interfaces/ICanvasMask.js +1 -2
- package/cjs/Options/Interfaces/ICanvasMaskOverride.js +1 -2
- package/cjs/Options/Interfaces/ICanvasMaskPixels.js +1 -2
- package/cjs/Options/Interfaces/IFontTextMask.js +1 -2
- package/cjs/Options/Interfaces/IImageMask.js +1 -2
- package/cjs/Options/Interfaces/ITextMask.js +1 -2
- package/cjs/Options/Interfaces/ITextMaskLine.js +1 -2
- package/cjs/index.js +6 -7
- package/cjs/types.js +1 -2
- package/cjs/utils.js +43 -32
- package/dist_browser_CanvasMaskPlugin_js.js +120 -0
- package/esm/CanvasMaskInstance.js +3 -2
- package/esm/Options/Classes/CanvasMask.js +2 -6
- package/esm/Options/Classes/CanvasMaskPixels.js +2 -2
- package/esm/index.js +6 -4
- package/esm/utils.js +37 -20
- package/package.json +4 -3
- package/report.html +5 -4
- package/tsparticles.plugin.canvas-mask.js +209 -120
- package/tsparticles.plugin.canvas-mask.min.js +1 -1
- package/tsparticles.plugin.canvas-mask.min.js.LICENSE.txt +1 -1
- package/types/Options/Classes/CanvasMaskPixels.d.ts +1 -1
- package/types/Options/Classes/FontTextMask.d.ts +1 -1
- package/types/Options/Classes/TextMaskLine.d.ts +1 -1
- package/types/index.d.ts +1 -1
- package/umd/CanvasMaskInstance.js +2 -1
- package/umd/Options/Classes/CanvasMask.js +2 -6
- package/umd/Options/Classes/CanvasMaskPixels.js +2 -2
- package/umd/index.js +41 -5
- 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
|
-
},
|
|
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
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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 =
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
}
|