@tsparticles/plugin-emitters-shape-canvas 3.0.0-beta.4
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/LICENSE +21 -0
- package/README.md +79 -0
- package/browser/EmittersCanvasShape.js +91 -0
- package/browser/EmittersCanvasShapeGenerator.js +9 -0
- package/browser/Options/Classes/EmittersCanvasShapeOptions.js +33 -0
- package/browser/Options/Classes/PixelsOptions.js +13 -0
- package/browser/Options/Classes/TextFontOptions.js +29 -0
- package/browser/Options/Classes/TextLinesOptions.js +17 -0
- package/browser/Options/Classes/TextOptions.js +23 -0
- package/browser/Options/Interfaces/IEmittersCanvasShapeOptions.js +1 -0
- package/browser/Options/Interfaces/IPixelsOptions.js +1 -0
- package/browser/Options/Interfaces/ITextFontOptions.js +1 -0
- package/browser/Options/Interfaces/ITextLinesOptions.js +1 -0
- package/browser/Options/Interfaces/ITextOptions.js +1 -0
- package/browser/index.js +7 -0
- package/browser/package.json +1 -0
- package/browser/types.js +1 -0
- package/browser/utils.js +84 -0
- package/cjs/EmittersCanvasShape.js +95 -0
- package/cjs/EmittersCanvasShapeGenerator.js +13 -0
- package/cjs/Options/Classes/EmittersCanvasShapeOptions.js +37 -0
- package/cjs/Options/Classes/PixelsOptions.js +17 -0
- package/cjs/Options/Classes/TextFontOptions.js +33 -0
- package/cjs/Options/Classes/TextLinesOptions.js +21 -0
- package/cjs/Options/Classes/TextOptions.js +27 -0
- package/cjs/Options/Interfaces/IEmittersCanvasShapeOptions.js +2 -0
- package/cjs/Options/Interfaces/IPixelsOptions.js +2 -0
- package/cjs/Options/Interfaces/ITextFontOptions.js +2 -0
- package/cjs/Options/Interfaces/ITextLinesOptions.js +2 -0
- package/cjs/Options/Interfaces/ITextOptions.js +2 -0
- package/cjs/index.js +11 -0
- package/cjs/package.json +1 -0
- package/cjs/types.js +2 -0
- package/cjs/utils.js +90 -0
- package/esm/EmittersCanvasShape.js +91 -0
- package/esm/EmittersCanvasShapeGenerator.js +9 -0
- package/esm/Options/Classes/EmittersCanvasShapeOptions.js +33 -0
- package/esm/Options/Classes/PixelsOptions.js +13 -0
- package/esm/Options/Classes/TextFontOptions.js +29 -0
- package/esm/Options/Classes/TextLinesOptions.js +17 -0
- package/esm/Options/Classes/TextOptions.js +23 -0
- package/esm/Options/Interfaces/IEmittersCanvasShapeOptions.js +1 -0
- package/esm/Options/Interfaces/IPixelsOptions.js +1 -0
- package/esm/Options/Interfaces/ITextFontOptions.js +1 -0
- package/esm/Options/Interfaces/ITextLinesOptions.js +1 -0
- package/esm/Options/Interfaces/ITextOptions.js +1 -0
- package/esm/index.js +7 -0
- package/esm/package.json +1 -0
- package/esm/types.js +1 -0
- package/esm/utils.js +84 -0
- package/package.json +109 -0
- package/report.html +39 -0
- package/tsparticles.plugin.emitters.shape.canvas.js +452 -0
- package/tsparticles.plugin.emitters.shape.canvas.min.js +2 -0
- package/tsparticles.plugin.emitters.shape.canvas.min.js.LICENSE.txt +1 -0
- package/types/EmittersCanvasShape.d.ts +13 -0
- package/types/EmittersCanvasShapeGenerator.d.ts +6 -0
- package/types/Options/Classes/EmittersCanvasShapeOptions.d.ts +15 -0
- package/types/Options/Classes/PixelsOptions.d.ts +7 -0
- package/types/Options/Classes/TextFontOptions.d.ts +11 -0
- package/types/Options/Classes/TextLinesOptions.d.ts +8 -0
- package/types/Options/Classes/TextOptions.d.ts +12 -0
- package/types/Options/Interfaces/IEmittersCanvasShapeOptions.d.ts +12 -0
- package/types/Options/Interfaces/IPixelsOptions.d.ts +3 -0
- package/types/Options/Interfaces/ITextFontOptions.d.ts +7 -0
- package/types/Options/Interfaces/ITextLinesOptions.d.ts +4 -0
- package/types/Options/Interfaces/ITextOptions.d.ts +8 -0
- package/types/index.d.ts +2 -0
- package/types/types.d.ts +12 -0
- package/types/utils.d.ts +6 -0
- package/umd/EmittersCanvasShape.js +105 -0
- package/umd/EmittersCanvasShapeGenerator.js +23 -0
- package/umd/Options/Classes/EmittersCanvasShapeOptions.js +47 -0
- package/umd/Options/Classes/PixelsOptions.js +27 -0
- package/umd/Options/Classes/TextFontOptions.js +43 -0
- package/umd/Options/Classes/TextLinesOptions.js +31 -0
- package/umd/Options/Classes/TextOptions.js +37 -0
- package/umd/Options/Interfaces/IEmittersCanvasShapeOptions.js +12 -0
- package/umd/Options/Interfaces/IPixelsOptions.js +12 -0
- package/umd/Options/Interfaces/ITextFontOptions.js +12 -0
- package/umd/Options/Interfaces/ITextLinesOptions.js +12 -0
- package/umd/Options/Interfaces/ITextOptions.js +12 -0
- package/umd/index.js +21 -0
- package/umd/types.js +12 -0
- package/umd/utils.js +100 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TextOptions = void 0;
|
|
4
|
+
const TextFontOptions_js_1 = require("./TextFontOptions.js");
|
|
5
|
+
const TextLinesOptions_js_1 = require("./TextLinesOptions.js");
|
|
6
|
+
class TextOptions {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.color = "#000000";
|
|
9
|
+
this.font = new TextFontOptions_js_1.TextFontOptions();
|
|
10
|
+
this.lines = new TextLinesOptions_js_1.TextLinesOptions();
|
|
11
|
+
this.text = "";
|
|
12
|
+
}
|
|
13
|
+
load(data) {
|
|
14
|
+
if (!data) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (data.color !== undefined) {
|
|
18
|
+
this.color = data.color;
|
|
19
|
+
}
|
|
20
|
+
this.font.load(data.font);
|
|
21
|
+
this.lines.load(data.lines);
|
|
22
|
+
if (data.text !== undefined) {
|
|
23
|
+
this.text = data.text;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.TextOptions = TextOptions;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadEmittersShapeCanvas = void 0;
|
|
4
|
+
const EmittersCanvasShapeGenerator_js_1 = require("./EmittersCanvasShapeGenerator.js");
|
|
5
|
+
async function loadEmittersShapeCanvas(engine, refresh = true) {
|
|
6
|
+
const emittersEngine = engine;
|
|
7
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
8
|
+
emittersEngine.addEmitterShapeGenerator("canvas", new EmittersCanvasShapeGenerator_js_1.EmittersCanvasShapeGenerator());
|
|
9
|
+
await emittersEngine.refresh(refresh);
|
|
10
|
+
}
|
|
11
|
+
exports.loadEmittersShapeCanvas = loadEmittersShapeCanvas;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
package/cjs/types.js
ADDED
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTextData = exports.getImageData = exports.getCanvasImageData = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
function getCanvasImageData(ctx, size, offset, clear = true) {
|
|
6
|
+
const imageData = ctx.getImageData(0, 0, size.width, size.height).data;
|
|
7
|
+
if (clear) {
|
|
8
|
+
ctx.clearRect(0, 0, size.width, size.height);
|
|
9
|
+
}
|
|
10
|
+
const pixels = [];
|
|
11
|
+
for (let i = 0; i < imageData.length; i += offset) {
|
|
12
|
+
const idx = i / offset, pos = {
|
|
13
|
+
x: idx % size.width,
|
|
14
|
+
y: Math.floor(idx / size.width),
|
|
15
|
+
};
|
|
16
|
+
if (!pixels[pos.y]) {
|
|
17
|
+
pixels[pos.y] = [];
|
|
18
|
+
}
|
|
19
|
+
pixels[pos.y][pos.x] = {
|
|
20
|
+
r: imageData[i],
|
|
21
|
+
g: imageData[i + 1],
|
|
22
|
+
b: imageData[i + 2],
|
|
23
|
+
a: imageData[i + 3] / 255,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
pixels,
|
|
28
|
+
width: Math.min(...pixels.map((row) => row.length)),
|
|
29
|
+
height: pixels.length,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.getCanvasImageData = getCanvasImageData;
|
|
33
|
+
function getImageData(src, offset) {
|
|
34
|
+
const image = new Image();
|
|
35
|
+
image.crossOrigin = "Anonymous";
|
|
36
|
+
const p = new Promise((resolve, reject) => {
|
|
37
|
+
image.onerror = reject;
|
|
38
|
+
image.onload = () => {
|
|
39
|
+
const canvas = document.createElement("canvas");
|
|
40
|
+
canvas.width = image.width;
|
|
41
|
+
canvas.height = image.height;
|
|
42
|
+
const context = canvas.getContext("2d");
|
|
43
|
+
if (!context) {
|
|
44
|
+
return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
|
|
45
|
+
}
|
|
46
|
+
context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
|
|
47
|
+
resolve(getCanvasImageData(context, canvas, offset));
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
image.src = src;
|
|
51
|
+
return p;
|
|
52
|
+
}
|
|
53
|
+
exports.getImageData = getImageData;
|
|
54
|
+
function getTextData(textOptions, offset, fill) {
|
|
55
|
+
const canvas = document.createElement("canvas"), context = canvas.getContext("2d"), { font, text, lines: linesOptions, color } = textOptions;
|
|
56
|
+
if (!text || !context) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size}px` : font.size, linesData = [];
|
|
60
|
+
let maxWidth = 0, totalHeight = 0;
|
|
61
|
+
for (const line of lines) {
|
|
62
|
+
context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
|
|
63
|
+
const measure = context.measureText(line), lineData = {
|
|
64
|
+
measure,
|
|
65
|
+
text: line,
|
|
66
|
+
height: measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent,
|
|
67
|
+
width: measure.width,
|
|
68
|
+
};
|
|
69
|
+
maxWidth = Math.max(maxWidth || 0, lineData.width);
|
|
70
|
+
totalHeight += lineData.height + linesOptions.spacing;
|
|
71
|
+
linesData.push(lineData);
|
|
72
|
+
}
|
|
73
|
+
canvas.width = maxWidth;
|
|
74
|
+
canvas.height = totalHeight;
|
|
75
|
+
let currentHeight = 0;
|
|
76
|
+
for (const line of linesData) {
|
|
77
|
+
context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
|
|
78
|
+
if (fill) {
|
|
79
|
+
context.fillStyle = color;
|
|
80
|
+
context.fillText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
context.strokeStyle = color;
|
|
84
|
+
context.strokeText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
|
|
85
|
+
}
|
|
86
|
+
currentHeight += line.height + linesOptions.spacing;
|
|
87
|
+
}
|
|
88
|
+
return getCanvasImageData(context, canvas, offset);
|
|
89
|
+
}
|
|
90
|
+
exports.getTextData = getTextData;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { EmitterShapeBase } from "@tsparticles/plugin-emitters";
|
|
2
|
+
import { getRandom, isFunction, isString } from "@tsparticles/engine";
|
|
3
|
+
import { getCanvasImageData, getImageData, getTextData } from "./utils.js";
|
|
4
|
+
export class EmittersCanvasShape extends EmitterShapeBase {
|
|
5
|
+
constructor(position, size, fill, options) {
|
|
6
|
+
super(position, size, fill, options);
|
|
7
|
+
const filter = options.filter;
|
|
8
|
+
let filterFunc = (pixel) => pixel.a > 0;
|
|
9
|
+
if (filter !== undefined) {
|
|
10
|
+
if (isString(filter)) {
|
|
11
|
+
if (Object.hasOwn(window, filter)) {
|
|
12
|
+
const wndFilter = window[filter];
|
|
13
|
+
if (isFunction(wndFilter)) {
|
|
14
|
+
filterFunc = wndFilter;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
filterFunc = filter;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
this.filter = filterFunc;
|
|
23
|
+
this.scale = options.scale;
|
|
24
|
+
this.pixelData = {
|
|
25
|
+
pixels: [],
|
|
26
|
+
height: 0,
|
|
27
|
+
width: 0,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async init() {
|
|
31
|
+
let pixelData;
|
|
32
|
+
const options = this.options, selector = options.selector, pixels = options.pixels, image = options.image, element = options.element, text = options.text, offset = pixels.offset;
|
|
33
|
+
if (image) {
|
|
34
|
+
const url = image.src;
|
|
35
|
+
if (!url) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
pixelData = await getImageData(url, offset);
|
|
39
|
+
}
|
|
40
|
+
else if (text) {
|
|
41
|
+
const data = getTextData(text, offset, this.fill);
|
|
42
|
+
if (!data) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
pixelData = data;
|
|
46
|
+
}
|
|
47
|
+
else if (element || selector) {
|
|
48
|
+
const canvas = element || (selector && document.querySelector(selector));
|
|
49
|
+
if (!canvas) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const context = canvas.getContext("2d");
|
|
53
|
+
if (!context) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
pixelData = getCanvasImageData(context, canvas, offset);
|
|
57
|
+
}
|
|
58
|
+
if (!pixelData) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.pixelData = pixelData;
|
|
62
|
+
}
|
|
63
|
+
async randomPosition() {
|
|
64
|
+
const { height, width } = this.pixelData, data = this.pixelData, position = this.position, scale = this.scale;
|
|
65
|
+
const positionOffset = {
|
|
66
|
+
x: position.x - (width * scale) / 2,
|
|
67
|
+
y: position.y - (height * scale) / 2,
|
|
68
|
+
};
|
|
69
|
+
for (let i = 0; i < 100; i++) {
|
|
70
|
+
const nextIndex = Math.floor(getRandom() * width * height), pixelPos = {
|
|
71
|
+
x: nextIndex % width,
|
|
72
|
+
y: Math.floor(nextIndex / width),
|
|
73
|
+
}, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = this.filter(pixel);
|
|
74
|
+
if (!shouldCreateParticle) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
position: {
|
|
79
|
+
x: pixelPos.x * scale + positionOffset.x,
|
|
80
|
+
y: pixelPos.y * scale + positionOffset.y,
|
|
81
|
+
},
|
|
82
|
+
color: { ...pixel },
|
|
83
|
+
opacity: pixel.a,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
resize(position, size) {
|
|
89
|
+
super.resize(position, size);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EmittersCanvasShape } from "./EmittersCanvasShape.js";
|
|
2
|
+
import { EmittersCanvasShapeOptions } from "./Options/Classes/EmittersCanvasShapeOptions.js";
|
|
3
|
+
export class EmittersCanvasShapeGenerator {
|
|
4
|
+
generate(position, size, fill, options) {
|
|
5
|
+
const shapeOptions = new EmittersCanvasShapeOptions();
|
|
6
|
+
shapeOptions.load(options);
|
|
7
|
+
return new EmittersCanvasShape(position, size, fill, shapeOptions);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PixelsOptions } from "./PixelsOptions.js";
|
|
2
|
+
import { TextOptions } from "./TextOptions.js";
|
|
3
|
+
export class EmittersCanvasShapeOptions {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.filter = (pixel) => pixel.a > 0;
|
|
6
|
+
this.pixels = new PixelsOptions();
|
|
7
|
+
this.scale = 1;
|
|
8
|
+
this.selector = "";
|
|
9
|
+
this.text = new TextOptions();
|
|
10
|
+
}
|
|
11
|
+
load(data) {
|
|
12
|
+
if (!data) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (data.element !== undefined) {
|
|
16
|
+
this.element = data.element;
|
|
17
|
+
}
|
|
18
|
+
if (data.filter !== undefined) {
|
|
19
|
+
this.filter = data.filter;
|
|
20
|
+
}
|
|
21
|
+
this.pixels.load(data.pixels);
|
|
22
|
+
if (data.scale !== undefined) {
|
|
23
|
+
this.scale = data.scale;
|
|
24
|
+
}
|
|
25
|
+
if (data.selector !== undefined) {
|
|
26
|
+
this.selector = data.selector;
|
|
27
|
+
}
|
|
28
|
+
if (data.image !== undefined) {
|
|
29
|
+
this.image = data.image;
|
|
30
|
+
}
|
|
31
|
+
this.text.load(data.text);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class TextFontOptions {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.family = "Verdana";
|
|
4
|
+
this.size = 32;
|
|
5
|
+
this.style = "";
|
|
6
|
+
this.variant = "";
|
|
7
|
+
this.weight = "";
|
|
8
|
+
}
|
|
9
|
+
load(data) {
|
|
10
|
+
if (!data) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (data.family !== undefined) {
|
|
14
|
+
this.family = data.family;
|
|
15
|
+
}
|
|
16
|
+
if (data.size !== undefined) {
|
|
17
|
+
this.size = data.size;
|
|
18
|
+
}
|
|
19
|
+
if (data.style !== undefined) {
|
|
20
|
+
this.style = data.style;
|
|
21
|
+
}
|
|
22
|
+
if (data.variant !== undefined) {
|
|
23
|
+
this.variant = data.variant;
|
|
24
|
+
}
|
|
25
|
+
if (data.weight !== undefined) {
|
|
26
|
+
this.weight = data.weight;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class TextLinesOptions {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.separator = "\n";
|
|
4
|
+
this.spacing = 0;
|
|
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,23 @@
|
|
|
1
|
+
import { TextFontOptions } from "./TextFontOptions.js";
|
|
2
|
+
import { TextLinesOptions } from "./TextLinesOptions.js";
|
|
3
|
+
export class TextOptions {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.color = "#000000";
|
|
6
|
+
this.font = new TextFontOptions();
|
|
7
|
+
this.lines = new TextLinesOptions();
|
|
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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EmittersCanvasShapeGenerator } from "./EmittersCanvasShapeGenerator.js";
|
|
2
|
+
export async function loadEmittersShapeCanvas(engine, refresh = true) {
|
|
3
|
+
const emittersEngine = engine;
|
|
4
|
+
emittersEngine.addEmitterShapeGenerator &&
|
|
5
|
+
emittersEngine.addEmitterShapeGenerator("canvas", new EmittersCanvasShapeGenerator());
|
|
6
|
+
await emittersEngine.refresh(refresh);
|
|
7
|
+
}
|
package/esm/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { errorPrefix, isNumber } from "@tsparticles/engine";
|
|
2
|
+
export function getCanvasImageData(ctx, size, offset, clear = true) {
|
|
3
|
+
const imageData = ctx.getImageData(0, 0, size.width, size.height).data;
|
|
4
|
+
if (clear) {
|
|
5
|
+
ctx.clearRect(0, 0, size.width, size.height);
|
|
6
|
+
}
|
|
7
|
+
const pixels = [];
|
|
8
|
+
for (let i = 0; i < imageData.length; i += offset) {
|
|
9
|
+
const idx = i / offset, pos = {
|
|
10
|
+
x: idx % size.width,
|
|
11
|
+
y: Math.floor(idx / size.width),
|
|
12
|
+
};
|
|
13
|
+
if (!pixels[pos.y]) {
|
|
14
|
+
pixels[pos.y] = [];
|
|
15
|
+
}
|
|
16
|
+
pixels[pos.y][pos.x] = {
|
|
17
|
+
r: imageData[i],
|
|
18
|
+
g: imageData[i + 1],
|
|
19
|
+
b: imageData[i + 2],
|
|
20
|
+
a: imageData[i + 3] / 255,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
pixels,
|
|
25
|
+
width: Math.min(...pixels.map((row) => row.length)),
|
|
26
|
+
height: pixels.length,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function getImageData(src, offset) {
|
|
30
|
+
const image = new Image();
|
|
31
|
+
image.crossOrigin = "Anonymous";
|
|
32
|
+
const p = new Promise((resolve, reject) => {
|
|
33
|
+
image.onerror = reject;
|
|
34
|
+
image.onload = () => {
|
|
35
|
+
const canvas = document.createElement("canvas");
|
|
36
|
+
canvas.width = image.width;
|
|
37
|
+
canvas.height = image.height;
|
|
38
|
+
const context = canvas.getContext("2d");
|
|
39
|
+
if (!context) {
|
|
40
|
+
return reject(new Error(`${errorPrefix} Could not get canvas context`));
|
|
41
|
+
}
|
|
42
|
+
context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
|
|
43
|
+
resolve(getCanvasImageData(context, canvas, offset));
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
image.src = src;
|
|
47
|
+
return p;
|
|
48
|
+
}
|
|
49
|
+
export function getTextData(textOptions, offset, fill) {
|
|
50
|
+
const canvas = document.createElement("canvas"), context = canvas.getContext("2d"), { font, text, lines: linesOptions, color } = textOptions;
|
|
51
|
+
if (!text || !context) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const lines = text.split(linesOptions.separator), fontSize = isNumber(font.size) ? `${font.size}px` : font.size, linesData = [];
|
|
55
|
+
let maxWidth = 0, totalHeight = 0;
|
|
56
|
+
for (const line of lines) {
|
|
57
|
+
context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
|
|
58
|
+
const measure = context.measureText(line), lineData = {
|
|
59
|
+
measure,
|
|
60
|
+
text: line,
|
|
61
|
+
height: measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent,
|
|
62
|
+
width: measure.width,
|
|
63
|
+
};
|
|
64
|
+
maxWidth = Math.max(maxWidth || 0, lineData.width);
|
|
65
|
+
totalHeight += lineData.height + linesOptions.spacing;
|
|
66
|
+
linesData.push(lineData);
|
|
67
|
+
}
|
|
68
|
+
canvas.width = maxWidth;
|
|
69
|
+
canvas.height = totalHeight;
|
|
70
|
+
let currentHeight = 0;
|
|
71
|
+
for (const line of linesData) {
|
|
72
|
+
context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
|
|
73
|
+
if (fill) {
|
|
74
|
+
context.fillStyle = color;
|
|
75
|
+
context.fillText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
context.strokeStyle = color;
|
|
79
|
+
context.strokeText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
|
|
80
|
+
}
|
|
81
|
+
currentHeight += line.height + linesOptions.spacing;
|
|
82
|
+
}
|
|
83
|
+
return getCanvasImageData(context, canvas, offset);
|
|
84
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsparticles/plugin-emitters-shape-canvas",
|
|
3
|
+
"version": "3.0.0-beta.4",
|
|
4
|
+
"description": "tsParticles emitters shape canvas plugin",
|
|
5
|
+
"homepage": "https://particles.js.org",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/tsparticles/tsparticles.git",
|
|
9
|
+
"directory": "plugins/emitterShapes/canvas"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"front-end",
|
|
13
|
+
"frontend",
|
|
14
|
+
"tsparticles",
|
|
15
|
+
"particles.js",
|
|
16
|
+
"particlesjs",
|
|
17
|
+
"particles",
|
|
18
|
+
"particle",
|
|
19
|
+
"canvas",
|
|
20
|
+
"jsparticles",
|
|
21
|
+
"xparticles",
|
|
22
|
+
"particles-js",
|
|
23
|
+
"particles-bg",
|
|
24
|
+
"particles-bg-vue",
|
|
25
|
+
"particles-ts",
|
|
26
|
+
"particles.ts",
|
|
27
|
+
"react-particles-js",
|
|
28
|
+
"react-particles.js",
|
|
29
|
+
"react-particles",
|
|
30
|
+
"react",
|
|
31
|
+
"reactjs",
|
|
32
|
+
"vue-particles",
|
|
33
|
+
"ngx-particles",
|
|
34
|
+
"angular-particles",
|
|
35
|
+
"particleground",
|
|
36
|
+
"vue",
|
|
37
|
+
"vuejs",
|
|
38
|
+
"preact",
|
|
39
|
+
"preactjs",
|
|
40
|
+
"jquery",
|
|
41
|
+
"angularjs",
|
|
42
|
+
"angular",
|
|
43
|
+
"typescript",
|
|
44
|
+
"javascript",
|
|
45
|
+
"animation",
|
|
46
|
+
"web",
|
|
47
|
+
"html5",
|
|
48
|
+
"web-design",
|
|
49
|
+
"webdesign",
|
|
50
|
+
"css",
|
|
51
|
+
"html",
|
|
52
|
+
"css3",
|
|
53
|
+
"animated",
|
|
54
|
+
"background",
|
|
55
|
+
"confetti",
|
|
56
|
+
"canvas",
|
|
57
|
+
"fireworks",
|
|
58
|
+
"fireworks-js",
|
|
59
|
+
"confetti-js",
|
|
60
|
+
"confettijs",
|
|
61
|
+
"fireworksjs",
|
|
62
|
+
"canvas-confetti",
|
|
63
|
+
"tsparticles-plugin"
|
|
64
|
+
],
|
|
65
|
+
"author": "Matteo Bruni <matteo.bruni@me.com>",
|
|
66
|
+
"license": "MIT",
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/tsparticles/tsparticles/issues"
|
|
69
|
+
},
|
|
70
|
+
"funding": [
|
|
71
|
+
{
|
|
72
|
+
"type": "github",
|
|
73
|
+
"url": "https://github.com/sponsors/matteobruni"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"type": "github",
|
|
77
|
+
"url": "https://github.com/sponsors/tsparticles"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"type": "buymeacoffee",
|
|
81
|
+
"url": "https://www.buymeacoffee.com/matteobruni"
|
|
82
|
+
}
|
|
83
|
+
],
|
|
84
|
+
"sideEffects": false,
|
|
85
|
+
"jsdelivr": "tsparticles.plugin.emitters.shape.canvas.min.js",
|
|
86
|
+
"unpkg": "tsparticles.plugin.emitters.shape.canvas.min.js",
|
|
87
|
+
"browser": "browser/index.js",
|
|
88
|
+
"main": "cjs/index.js",
|
|
89
|
+
"module": "esm/index.js",
|
|
90
|
+
"types": "types/index.d.ts",
|
|
91
|
+
"exports": {
|
|
92
|
+
".": {
|
|
93
|
+
"types": "./types/index.d.ts",
|
|
94
|
+
"browser": "./browser/index.js",
|
|
95
|
+
"import": "./esm/index.js",
|
|
96
|
+
"require": "./cjs/index.js",
|
|
97
|
+
"umd": "./umd/index.js",
|
|
98
|
+
"default": "./cjs/index.js"
|
|
99
|
+
},
|
|
100
|
+
"./package.json": "./package.json"
|
|
101
|
+
},
|
|
102
|
+
"dependencies": {
|
|
103
|
+
"@tsparticles/engine": "^3.0.0-beta.4",
|
|
104
|
+
"@tsparticles/plugin-emitters": "^3.0.0-beta.4"
|
|
105
|
+
},
|
|
106
|
+
"publishConfig": {
|
|
107
|
+
"access": "public"
|
|
108
|
+
}
|
|
109
|
+
}
|