@tsparticles/plugin-canvas-mask 3.0.0-alpha.1 → 3.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.
- package/README.md +15 -11
- package/browser/CanvasMaskInstance.js +1 -1
- package/browser/Options/Classes/CanvasMask.js +2 -3
- package/browser/Options/Classes/CanvasMaskPixels.js +3 -2
- package/browser/index.js +6 -8
- package/browser/utils.js +20 -20
- package/cjs/CanvasMaskInstance.js +33 -44
- package/cjs/Options/Classes/CanvasMask.js +2 -3
- package/cjs/Options/Classes/CanvasMaskPixels.js +3 -2
- package/cjs/index.js +6 -19
- package/cjs/utils.js +19 -19
- package/esm/CanvasMaskInstance.js +1 -1
- package/esm/Options/Classes/CanvasMask.js +2 -3
- package/esm/Options/Classes/CanvasMaskPixels.js +3 -2
- package/esm/index.js +6 -8
- package/esm/utils.js +20 -20
- package/package.json +6 -5
- package/report.html +4 -4
- package/tsparticles.plugin.canvas-mask.js +34 -35
- package/tsparticles.plugin.canvas-mask.min.js +1 -1
- package/tsparticles.plugin.canvas-mask.min.js.LICENSE.txt +1 -8
- package/types/index.d.ts +1 -1
- package/types/utils.d.ts +1 -1
- package/umd/CanvasMaskInstance.js +1 -1
- package/umd/Options/Classes/CanvasMask.js +2 -3
- package/umd/Options/Classes/CanvasMaskPixels.js +4 -3
- package/umd/index.js +6 -8
- 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
|
-
[](https://www.jsdelivr.com/package/npm/@tsparticles/plugin-canvas-mask)
|
|
6
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-canvas-mask)
|
|
7
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-canvas-mask) [](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
|
|
45
|
+
$ npm install @tsparticles/plugin-canvas-mask
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
or
|
|
49
49
|
|
|
50
50
|
```shell
|
|
51
|
-
$ yarn add tsparticles
|
|
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
|
|
58
|
-
const { loadCanvasMaskPlugin } = require("tsparticles
|
|
57
|
+
const { tsParticles } = require("@tsparticles/engine");
|
|
58
|
+
const { loadCanvasMaskPlugin } = require("@tsparticles/plugin-canvas-mask");
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
(async () => {
|
|
61
|
+
await loadCanvasMaskPlugin(tsParticles);
|
|
62
|
+
})();
|
|
61
63
|
```
|
|
62
64
|
|
|
63
65
|
or
|
|
64
66
|
|
|
65
67
|
```javascript
|
|
66
|
-
import { tsParticles } from "tsparticles
|
|
67
|
-
import { loadCanvasMaskPlugin } from "tsparticles
|
|
68
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
69
|
+
import { loadCanvasMaskPlugin } from "@tsparticles/plugin-canvas-mask";
|
|
68
70
|
|
|
69
|
-
|
|
71
|
+
(async () => {
|
|
72
|
+
await loadCanvasMaskPlugin(tsParticles);
|
|
73
|
+
})();
|
|
70
74
|
```
|
|
@@ -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 (!
|
|
9
|
+
if (!options?.enable) {
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
12
12
|
let pixelData = {
|
|
@@ -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:
|
|
37
|
-
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 (
|
|
12
|
+
if (isString(data.filter)) {
|
|
12
13
|
if (Object.hasOwn(window, data.filter)) {
|
|
13
14
|
const filter = window[data.filter];
|
|
14
|
-
if (
|
|
15
|
+
if (isFunction(filter)) {
|
|
15
16
|
this.filter = filter;
|
|
16
17
|
}
|
|
17
18
|
}
|
package/browser/index.js
CHANGED
|
@@ -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 (
|
|
16
|
+
if (canvasMaskOptions?.load === undefined) {
|
|
17
17
|
options.canvasMask = canvasMaskOptions = new CanvasMask();
|
|
18
18
|
}
|
|
19
|
-
canvasMaskOptions.load(source
|
|
19
|
+
canvasMaskOptions.load(source?.canvasMask);
|
|
20
20
|
}
|
|
21
21
|
needsPlugin(options) {
|
|
22
|
-
|
|
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
|
-
|
|
28
|
-
await engine.addPlugin(plugin);
|
|
25
|
+
export async function loadCanvasMaskPlugin(engine, refresh = true) {
|
|
26
|
+
await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
|
|
29
27
|
}
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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(
|
|
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 =
|
|
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,13 +1,4 @@
|
|
|
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
4
|
const utils_1 = require("./utils");
|
|
@@ -16,46 +7,44 @@ class CanvasMaskInstance {
|
|
|
16
7
|
this._container = container;
|
|
17
8
|
this._engine = engine;
|
|
18
9
|
}
|
|
19
|
-
init() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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_1.getImageData)(url, offset);
|
|
27
|
+
}
|
|
28
|
+
else if (options.text) {
|
|
29
|
+
const textOptions = options.text;
|
|
30
|
+
const data = (0, utils_1.getTextData)(textOptions, offset);
|
|
31
|
+
if (!data) {
|
|
32
|
+
return;
|
|
37
33
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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.
|
|
58
|
-
}
|
|
45
|
+
pixelData = (0, utils_1.getCanvasImageData)(context, canvas, offset);
|
|
46
|
+
}
|
|
47
|
+
(0, utils_1.addParticlesFromCanvasPixels)(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
|
|
59
48
|
}
|
|
60
49
|
}
|
|
61
50
|
exports.CanvasMaskInstance = CanvasMaskInstance;
|
|
@@ -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
|
}
|
|
@@ -36,8 +35,8 @@ class CanvasMask {
|
|
|
36
35
|
this.pixels.load(data.pixels);
|
|
37
36
|
if (data.position) {
|
|
38
37
|
this.position = {
|
|
39
|
-
x:
|
|
40
|
-
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);
|
|
@@ -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 (
|
|
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 (
|
|
18
|
+
if ((0, engine_1.isFunction)(filter)) {
|
|
18
19
|
this.filter = filter;
|
|
19
20
|
}
|
|
20
21
|
}
|
package/cjs/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
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
4
|
const CanvasMask_1 = require("./Options/Classes/CanvasMask");
|
|
@@ -21,24 +12,20 @@ class CanvasMaskPlugin {
|
|
|
21
12
|
return new CanvasMaskInstance_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 (
|
|
19
|
+
if (canvasMaskOptions?.load === undefined) {
|
|
29
20
|
options.canvasMask = canvasMaskOptions = new CanvasMask_1.CanvasMask();
|
|
30
21
|
}
|
|
31
|
-
canvasMaskOptions.load(source
|
|
22
|
+
canvasMaskOptions.load(source?.canvasMask);
|
|
32
23
|
}
|
|
33
24
|
needsPlugin(options) {
|
|
34
|
-
|
|
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
|
-
|
|
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;
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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(
|
|
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 =
|
|
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}`;
|
|
@@ -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 (!
|
|
9
|
+
if (!options?.enable) {
|
|
10
10
|
return;
|
|
11
11
|
}
|
|
12
12
|
let pixelData = {
|
|
@@ -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:
|
|
37
|
-
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 (
|
|
12
|
+
if (isString(data.filter)) {
|
|
12
13
|
if (Object.hasOwn(window, data.filter)) {
|
|
13
14
|
const filter = window[data.filter];
|
|
14
|
-
if (
|
|
15
|
+
if (isFunction(filter)) {
|
|
15
16
|
this.filter = filter;
|
|
16
17
|
}
|
|
17
18
|
}
|
package/esm/index.js
CHANGED
|
@@ -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 (
|
|
16
|
+
if (canvasMaskOptions?.load === undefined) {
|
|
17
17
|
options.canvasMask = canvasMaskOptions = new CanvasMask();
|
|
18
18
|
}
|
|
19
|
-
canvasMaskOptions.load(source
|
|
19
|
+
canvasMaskOptions.load(source?.canvasMask);
|
|
20
20
|
}
|
|
21
21
|
needsPlugin(options) {
|
|
22
|
-
|
|
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
|
-
|
|
28
|
-
await engine.addPlugin(plugin);
|
|
25
|
+
export async function loadCanvasMaskPlugin(engine, refresh = true) {
|
|
26
|
+
await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
|
|
29
27
|
}
|
package/esm/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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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(
|
|
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 =
|
|
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}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsparticles/plugin-canvas-mask",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-beta.0",
|
|
4
4
|
"description": "tsParticles canvas mask plugin",
|
|
5
5
|
"homepage": "https://particles.js.org",
|
|
6
6
|
"repository": {
|
|
@@ -72,10 +72,11 @@
|
|
|
72
72
|
"unpkg": "tsparticles.plugin.canvas-mask.min.js",
|
|
73
73
|
"module": "esm/index.js",
|
|
74
74
|
"types": "types/index.d.ts",
|
|
75
|
+
"sideEffects": false,
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"@tsparticles/engine": "^3.0.0-beta.0"
|
|
78
|
+
},
|
|
75
79
|
"publishConfig": {
|
|
76
80
|
"access": "public"
|
|
77
|
-
},
|
|
78
|
-
"dependencies": {
|
|
79
|
-
"@tsparticles/engine": "^3.0.0-alpha.1"
|
|
80
81
|
}
|
|
81
|
-
}
|
|
82
|
+
}
|