@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.
Files changed (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +79 -0
  3. package/browser/EmittersCanvasShape.js +91 -0
  4. package/browser/EmittersCanvasShapeGenerator.js +9 -0
  5. package/browser/Options/Classes/EmittersCanvasShapeOptions.js +33 -0
  6. package/browser/Options/Classes/PixelsOptions.js +13 -0
  7. package/browser/Options/Classes/TextFontOptions.js +29 -0
  8. package/browser/Options/Classes/TextLinesOptions.js +17 -0
  9. package/browser/Options/Classes/TextOptions.js +23 -0
  10. package/browser/Options/Interfaces/IEmittersCanvasShapeOptions.js +1 -0
  11. package/browser/Options/Interfaces/IPixelsOptions.js +1 -0
  12. package/browser/Options/Interfaces/ITextFontOptions.js +1 -0
  13. package/browser/Options/Interfaces/ITextLinesOptions.js +1 -0
  14. package/browser/Options/Interfaces/ITextOptions.js +1 -0
  15. package/browser/index.js +7 -0
  16. package/browser/package.json +1 -0
  17. package/browser/types.js +1 -0
  18. package/browser/utils.js +84 -0
  19. package/cjs/EmittersCanvasShape.js +95 -0
  20. package/cjs/EmittersCanvasShapeGenerator.js +13 -0
  21. package/cjs/Options/Classes/EmittersCanvasShapeOptions.js +37 -0
  22. package/cjs/Options/Classes/PixelsOptions.js +17 -0
  23. package/cjs/Options/Classes/TextFontOptions.js +33 -0
  24. package/cjs/Options/Classes/TextLinesOptions.js +21 -0
  25. package/cjs/Options/Classes/TextOptions.js +27 -0
  26. package/cjs/Options/Interfaces/IEmittersCanvasShapeOptions.js +2 -0
  27. package/cjs/Options/Interfaces/IPixelsOptions.js +2 -0
  28. package/cjs/Options/Interfaces/ITextFontOptions.js +2 -0
  29. package/cjs/Options/Interfaces/ITextLinesOptions.js +2 -0
  30. package/cjs/Options/Interfaces/ITextOptions.js +2 -0
  31. package/cjs/index.js +11 -0
  32. package/cjs/package.json +1 -0
  33. package/cjs/types.js +2 -0
  34. package/cjs/utils.js +90 -0
  35. package/esm/EmittersCanvasShape.js +91 -0
  36. package/esm/EmittersCanvasShapeGenerator.js +9 -0
  37. package/esm/Options/Classes/EmittersCanvasShapeOptions.js +33 -0
  38. package/esm/Options/Classes/PixelsOptions.js +13 -0
  39. package/esm/Options/Classes/TextFontOptions.js +29 -0
  40. package/esm/Options/Classes/TextLinesOptions.js +17 -0
  41. package/esm/Options/Classes/TextOptions.js +23 -0
  42. package/esm/Options/Interfaces/IEmittersCanvasShapeOptions.js +1 -0
  43. package/esm/Options/Interfaces/IPixelsOptions.js +1 -0
  44. package/esm/Options/Interfaces/ITextFontOptions.js +1 -0
  45. package/esm/Options/Interfaces/ITextLinesOptions.js +1 -0
  46. package/esm/Options/Interfaces/ITextOptions.js +1 -0
  47. package/esm/index.js +7 -0
  48. package/esm/package.json +1 -0
  49. package/esm/types.js +1 -0
  50. package/esm/utils.js +84 -0
  51. package/package.json +109 -0
  52. package/report.html +39 -0
  53. package/tsparticles.plugin.emitters.shape.canvas.js +452 -0
  54. package/tsparticles.plugin.emitters.shape.canvas.min.js +2 -0
  55. package/tsparticles.plugin.emitters.shape.canvas.min.js.LICENSE.txt +1 -0
  56. package/types/EmittersCanvasShape.d.ts +13 -0
  57. package/types/EmittersCanvasShapeGenerator.d.ts +6 -0
  58. package/types/Options/Classes/EmittersCanvasShapeOptions.d.ts +15 -0
  59. package/types/Options/Classes/PixelsOptions.d.ts +7 -0
  60. package/types/Options/Classes/TextFontOptions.d.ts +11 -0
  61. package/types/Options/Classes/TextLinesOptions.d.ts +8 -0
  62. package/types/Options/Classes/TextOptions.d.ts +12 -0
  63. package/types/Options/Interfaces/IEmittersCanvasShapeOptions.d.ts +12 -0
  64. package/types/Options/Interfaces/IPixelsOptions.d.ts +3 -0
  65. package/types/Options/Interfaces/ITextFontOptions.d.ts +7 -0
  66. package/types/Options/Interfaces/ITextLinesOptions.d.ts +4 -0
  67. package/types/Options/Interfaces/ITextOptions.d.ts +8 -0
  68. package/types/index.d.ts +2 -0
  69. package/types/types.d.ts +12 -0
  70. package/types/utils.d.ts +6 -0
  71. package/umd/EmittersCanvasShape.js +105 -0
  72. package/umd/EmittersCanvasShapeGenerator.js +23 -0
  73. package/umd/Options/Classes/EmittersCanvasShapeOptions.js +47 -0
  74. package/umd/Options/Classes/PixelsOptions.js +27 -0
  75. package/umd/Options/Classes/TextFontOptions.js +43 -0
  76. package/umd/Options/Classes/TextLinesOptions.js +31 -0
  77. package/umd/Options/Classes/TextOptions.js +37 -0
  78. package/umd/Options/Interfaces/IEmittersCanvasShapeOptions.js +12 -0
  79. package/umd/Options/Interfaces/IPixelsOptions.js +12 -0
  80. package/umd/Options/Interfaces/ITextFontOptions.js +12 -0
  81. package/umd/Options/Interfaces/ITextLinesOptions.js +12 -0
  82. package/umd/Options/Interfaces/ITextOptions.js +12 -0
  83. package/umd/index.js +21 -0
  84. package/umd/types.js +12 -0
  85. package/umd/utils.js +100 -0
@@ -0,0 +1,105 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "@tsparticles/plugin-emitters", "@tsparticles/engine", "./utils.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EmittersCanvasShape = void 0;
13
+ const plugin_emitters_1 = require("@tsparticles/plugin-emitters");
14
+ const engine_1 = require("@tsparticles/engine");
15
+ const utils_js_1 = require("./utils.js");
16
+ class EmittersCanvasShape extends plugin_emitters_1.EmitterShapeBase {
17
+ constructor(position, size, fill, options) {
18
+ super(position, size, fill, options);
19
+ const filter = options.filter;
20
+ let filterFunc = (pixel) => pixel.a > 0;
21
+ if (filter !== undefined) {
22
+ if ((0, engine_1.isString)(filter)) {
23
+ if (Object.hasOwn(window, filter)) {
24
+ const wndFilter = window[filter];
25
+ if ((0, engine_1.isFunction)(wndFilter)) {
26
+ filterFunc = wndFilter;
27
+ }
28
+ }
29
+ }
30
+ else {
31
+ filterFunc = filter;
32
+ }
33
+ }
34
+ this.filter = filterFunc;
35
+ this.scale = options.scale;
36
+ this.pixelData = {
37
+ pixels: [],
38
+ height: 0,
39
+ width: 0,
40
+ };
41
+ }
42
+ async init() {
43
+ let pixelData;
44
+ const options = this.options, selector = options.selector, pixels = options.pixels, image = options.image, element = options.element, text = options.text, offset = pixels.offset;
45
+ if (image) {
46
+ const url = image.src;
47
+ if (!url) {
48
+ return;
49
+ }
50
+ pixelData = await (0, utils_js_1.getImageData)(url, offset);
51
+ }
52
+ else if (text) {
53
+ const data = (0, utils_js_1.getTextData)(text, offset, this.fill);
54
+ if (!data) {
55
+ return;
56
+ }
57
+ pixelData = data;
58
+ }
59
+ else if (element || selector) {
60
+ const canvas = element || (selector && document.querySelector(selector));
61
+ if (!canvas) {
62
+ return;
63
+ }
64
+ const context = canvas.getContext("2d");
65
+ if (!context) {
66
+ return;
67
+ }
68
+ pixelData = (0, utils_js_1.getCanvasImageData)(context, canvas, offset);
69
+ }
70
+ if (!pixelData) {
71
+ return;
72
+ }
73
+ this.pixelData = pixelData;
74
+ }
75
+ async randomPosition() {
76
+ const { height, width } = this.pixelData, data = this.pixelData, position = this.position, scale = this.scale;
77
+ const positionOffset = {
78
+ x: position.x - (width * scale) / 2,
79
+ y: position.y - (height * scale) / 2,
80
+ };
81
+ for (let i = 0; i < 100; i++) {
82
+ const nextIndex = Math.floor((0, engine_1.getRandom)() * width * height), pixelPos = {
83
+ x: nextIndex % width,
84
+ y: Math.floor(nextIndex / width),
85
+ }, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = this.filter(pixel);
86
+ if (!shouldCreateParticle) {
87
+ continue;
88
+ }
89
+ return {
90
+ position: {
91
+ x: pixelPos.x * scale + positionOffset.x,
92
+ y: pixelPos.y * scale + positionOffset.y,
93
+ },
94
+ color: { ...pixel },
95
+ opacity: pixel.a,
96
+ };
97
+ }
98
+ return null;
99
+ }
100
+ resize(position, size) {
101
+ super.resize(position, size);
102
+ }
103
+ }
104
+ exports.EmittersCanvasShape = EmittersCanvasShape;
105
+ });
@@ -0,0 +1,23 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./EmittersCanvasShape.js", "./Options/Classes/EmittersCanvasShapeOptions.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EmittersCanvasShapeGenerator = void 0;
13
+ const EmittersCanvasShape_js_1 = require("./EmittersCanvasShape.js");
14
+ const EmittersCanvasShapeOptions_js_1 = require("./Options/Classes/EmittersCanvasShapeOptions.js");
15
+ class EmittersCanvasShapeGenerator {
16
+ generate(position, size, fill, options) {
17
+ const shapeOptions = new EmittersCanvasShapeOptions_js_1.EmittersCanvasShapeOptions();
18
+ shapeOptions.load(options);
19
+ return new EmittersCanvasShape_js_1.EmittersCanvasShape(position, size, fill, shapeOptions);
20
+ }
21
+ }
22
+ exports.EmittersCanvasShapeGenerator = EmittersCanvasShapeGenerator;
23
+ });
@@ -0,0 +1,47 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./PixelsOptions.js", "./TextOptions.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EmittersCanvasShapeOptions = void 0;
13
+ const PixelsOptions_js_1 = require("./PixelsOptions.js");
14
+ const TextOptions_js_1 = require("./TextOptions.js");
15
+ class EmittersCanvasShapeOptions {
16
+ constructor() {
17
+ this.filter = (pixel) => pixel.a > 0;
18
+ this.pixels = new PixelsOptions_js_1.PixelsOptions();
19
+ this.scale = 1;
20
+ this.selector = "";
21
+ this.text = new TextOptions_js_1.TextOptions();
22
+ }
23
+ load(data) {
24
+ if (!data) {
25
+ return;
26
+ }
27
+ if (data.element !== undefined) {
28
+ this.element = data.element;
29
+ }
30
+ if (data.filter !== undefined) {
31
+ this.filter = data.filter;
32
+ }
33
+ this.pixels.load(data.pixels);
34
+ if (data.scale !== undefined) {
35
+ this.scale = data.scale;
36
+ }
37
+ if (data.selector !== undefined) {
38
+ this.selector = data.selector;
39
+ }
40
+ if (data.image !== undefined) {
41
+ this.image = data.image;
42
+ }
43
+ this.text.load(data.text);
44
+ }
45
+ }
46
+ exports.EmittersCanvasShapeOptions = EmittersCanvasShapeOptions;
47
+ });
@@ -0,0 +1,27 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PixelsOptions = void 0;
13
+ class PixelsOptions {
14
+ constructor() {
15
+ this.offset = 4;
16
+ }
17
+ load(data) {
18
+ if (!data) {
19
+ return;
20
+ }
21
+ if (data.offset !== undefined) {
22
+ this.offset = data.offset;
23
+ }
24
+ }
25
+ }
26
+ exports.PixelsOptions = PixelsOptions;
27
+ });
@@ -0,0 +1,43 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TextFontOptions = void 0;
13
+ class TextFontOptions {
14
+ constructor() {
15
+ this.family = "Verdana";
16
+ this.size = 32;
17
+ this.style = "";
18
+ this.variant = "";
19
+ this.weight = "";
20
+ }
21
+ load(data) {
22
+ if (!data) {
23
+ return;
24
+ }
25
+ if (data.family !== undefined) {
26
+ this.family = data.family;
27
+ }
28
+ if (data.size !== undefined) {
29
+ this.size = data.size;
30
+ }
31
+ if (data.style !== undefined) {
32
+ this.style = data.style;
33
+ }
34
+ if (data.variant !== undefined) {
35
+ this.variant = data.variant;
36
+ }
37
+ if (data.weight !== undefined) {
38
+ this.weight = data.weight;
39
+ }
40
+ }
41
+ }
42
+ exports.TextFontOptions = TextFontOptions;
43
+ });
@@ -0,0 +1,31 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TextLinesOptions = void 0;
13
+ class TextLinesOptions {
14
+ constructor() {
15
+ this.separator = "\n";
16
+ this.spacing = 0;
17
+ }
18
+ load(data) {
19
+ if (!data) {
20
+ return;
21
+ }
22
+ if (data.separator !== undefined) {
23
+ this.separator = data.separator;
24
+ }
25
+ if (data.spacing !== undefined) {
26
+ this.spacing = data.spacing;
27
+ }
28
+ }
29
+ }
30
+ exports.TextLinesOptions = TextLinesOptions;
31
+ });
@@ -0,0 +1,37 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./TextFontOptions.js", "./TextLinesOptions.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TextOptions = void 0;
13
+ const TextFontOptions_js_1 = require("./TextFontOptions.js");
14
+ const TextLinesOptions_js_1 = require("./TextLinesOptions.js");
15
+ class TextOptions {
16
+ constructor() {
17
+ this.color = "#000000";
18
+ this.font = new TextFontOptions_js_1.TextFontOptions();
19
+ this.lines = new TextLinesOptions_js_1.TextLinesOptions();
20
+ this.text = "";
21
+ }
22
+ load(data) {
23
+ if (!data) {
24
+ return;
25
+ }
26
+ if (data.color !== undefined) {
27
+ this.color = data.color;
28
+ }
29
+ this.font.load(data.font);
30
+ this.lines.load(data.lines);
31
+ if (data.text !== undefined) {
32
+ this.text = data.text;
33
+ }
34
+ }
35
+ }
36
+ exports.TextOptions = TextOptions;
37
+ });
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
package/umd/index.js ADDED
@@ -0,0 +1,21 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./EmittersCanvasShapeGenerator.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.loadEmittersShapeCanvas = void 0;
13
+ const EmittersCanvasShapeGenerator_js_1 = require("./EmittersCanvasShapeGenerator.js");
14
+ async function loadEmittersShapeCanvas(engine, refresh = true) {
15
+ const emittersEngine = engine;
16
+ emittersEngine.addEmitterShapeGenerator &&
17
+ emittersEngine.addEmitterShapeGenerator("canvas", new EmittersCanvasShapeGenerator_js_1.EmittersCanvasShapeGenerator());
18
+ await emittersEngine.refresh(refresh);
19
+ }
20
+ exports.loadEmittersShapeCanvas = loadEmittersShapeCanvas;
21
+ });
package/umd/types.js ADDED
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });
package/umd/utils.js ADDED
@@ -0,0 +1,100 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "@tsparticles/engine"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getTextData = exports.getImageData = exports.getCanvasImageData = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ function getCanvasImageData(ctx, size, offset, clear = true) {
15
+ const imageData = ctx.getImageData(0, 0, size.width, size.height).data;
16
+ if (clear) {
17
+ ctx.clearRect(0, 0, size.width, size.height);
18
+ }
19
+ const pixels = [];
20
+ for (let i = 0; i < imageData.length; i += offset) {
21
+ const idx = i / offset, pos = {
22
+ x: idx % size.width,
23
+ y: Math.floor(idx / size.width),
24
+ };
25
+ if (!pixels[pos.y]) {
26
+ pixels[pos.y] = [];
27
+ }
28
+ pixels[pos.y][pos.x] = {
29
+ r: imageData[i],
30
+ g: imageData[i + 1],
31
+ b: imageData[i + 2],
32
+ a: imageData[i + 3] / 255,
33
+ };
34
+ }
35
+ return {
36
+ pixels,
37
+ width: Math.min(...pixels.map((row) => row.length)),
38
+ height: pixels.length,
39
+ };
40
+ }
41
+ exports.getCanvasImageData = getCanvasImageData;
42
+ function getImageData(src, offset) {
43
+ const image = new Image();
44
+ image.crossOrigin = "Anonymous";
45
+ const p = new Promise((resolve, reject) => {
46
+ image.onerror = reject;
47
+ image.onload = () => {
48
+ const canvas = document.createElement("canvas");
49
+ canvas.width = image.width;
50
+ canvas.height = image.height;
51
+ const context = canvas.getContext("2d");
52
+ if (!context) {
53
+ return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
54
+ }
55
+ context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
56
+ resolve(getCanvasImageData(context, canvas, offset));
57
+ };
58
+ });
59
+ image.src = src;
60
+ return p;
61
+ }
62
+ exports.getImageData = getImageData;
63
+ function getTextData(textOptions, offset, fill) {
64
+ const canvas = document.createElement("canvas"), context = canvas.getContext("2d"), { font, text, lines: linesOptions, color } = textOptions;
65
+ if (!text || !context) {
66
+ return;
67
+ }
68
+ const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size}px` : font.size, linesData = [];
69
+ let maxWidth = 0, totalHeight = 0;
70
+ for (const line of lines) {
71
+ context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
72
+ const measure = context.measureText(line), lineData = {
73
+ measure,
74
+ text: line,
75
+ height: measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent,
76
+ width: measure.width,
77
+ };
78
+ maxWidth = Math.max(maxWidth || 0, lineData.width);
79
+ totalHeight += lineData.height + linesOptions.spacing;
80
+ linesData.push(lineData);
81
+ }
82
+ canvas.width = maxWidth;
83
+ canvas.height = totalHeight;
84
+ let currentHeight = 0;
85
+ for (const line of linesData) {
86
+ context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
87
+ if (fill) {
88
+ context.fillStyle = color;
89
+ context.fillText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
90
+ }
91
+ else {
92
+ context.strokeStyle = color;
93
+ context.strokeText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
94
+ }
95
+ currentHeight += line.height + linesOptions.spacing;
96
+ }
97
+ return getCanvasImageData(context, canvas, offset);
98
+ }
99
+ exports.getTextData = getTextData;
100
+ });