1 | var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2 | if (kind === "m") throw new TypeError("Private method is not writable");
|
3 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5 | return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6 | };
|
7 | var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10 | return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11 | };
|
12 | var _ImageDrawer_images;
|
13 | import { isInArray } from "../../Utils";
|
14 | import { ShapeType } from "../../Enums";
|
15 | import { downloadSvgImage, loadImage, replaceColorSvg } from "./Utils";
|
16 |
|
17 |
|
18 |
|
19 | export class ImageDrawer {
|
20 | constructor() {
|
21 | _ImageDrawer_images.set(this, void 0);
|
22 | __classPrivateFieldSet(this, _ImageDrawer_images, [], "f");
|
23 | }
|
24 | getSidesCount() {
|
25 | return 12;
|
26 | }
|
27 | getImages(container) {
|
28 | const containerImages = __classPrivateFieldGet(this, _ImageDrawer_images, "f").find((t) => t.id === container.id);
|
29 | if (!containerImages) {
|
30 | __classPrivateFieldGet(this, _ImageDrawer_images, "f").push({
|
31 | id: container.id,
|
32 | images: [],
|
33 | });
|
34 | return this.getImages(container);
|
35 | }
|
36 | else {
|
37 | return containerImages;
|
38 | }
|
39 | }
|
40 | addImage(container, image) {
|
41 | const containerImages = this.getImages(container);
|
42 | containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
|
43 | }
|
44 | async init(container) {
|
45 | await this.loadImagesFromParticlesOptions(container, container.actualOptions.particles);
|
46 | await this.loadImagesFromParticlesOptions(container, container.actualOptions.interactivity.modes.trail.particles);
|
47 | for (const manualParticle of container.actualOptions.manualParticles) {
|
48 | await this.loadImagesFromParticlesOptions(container, manualParticle.options);
|
49 | }
|
50 | const emitterOptions = container.actualOptions;
|
51 | if (emitterOptions.emitters) {
|
52 | if (emitterOptions.emitters instanceof Array) {
|
53 | for (const emitter of emitterOptions.emitters) {
|
54 | await this.loadImagesFromParticlesOptions(container, emitter.particles);
|
55 | }
|
56 | }
|
57 | else {
|
58 | await this.loadImagesFromParticlesOptions(container, emitterOptions.emitters.particles);
|
59 | }
|
60 | }
|
61 | const interactiveEmitters = emitterOptions.interactivity.modes.emitters;
|
62 | if (interactiveEmitters) {
|
63 | if (interactiveEmitters instanceof Array) {
|
64 | for (const emitter of interactiveEmitters) {
|
65 | await this.loadImagesFromParticlesOptions(container, emitter.particles);
|
66 | }
|
67 | }
|
68 | else {
|
69 | await this.loadImagesFromParticlesOptions(container, interactiveEmitters.particles);
|
70 | }
|
71 | }
|
72 | }
|
73 | destroy() {
|
74 | __classPrivateFieldSet(this, _ImageDrawer_images, [], "f");
|
75 | }
|
76 | async loadImagesFromParticlesOptions(container, options) {
|
77 | var _a, _b, _c;
|
78 | const shapeOptions = options === null || options === void 0 ? void 0 : options.shape;
|
79 | if (!(shapeOptions === null || shapeOptions === void 0 ? void 0 : shapeOptions.type) ||
|
80 | !shapeOptions.options ||
|
81 | (!isInArray(ShapeType.image, shapeOptions.type) && !isInArray(ShapeType.images, shapeOptions.type))) {
|
82 | return;
|
83 | }
|
84 | const idx = __classPrivateFieldGet(this, _ImageDrawer_images, "f").findIndex((t) => t.id === container.id);
|
85 | if (idx >= 0) {
|
86 | __classPrivateFieldGet(this, _ImageDrawer_images, "f").splice(idx, 1);
|
87 | }
|
88 | const imageOptions = (_a = shapeOptions.options[ShapeType.images]) !== null && _a !== void 0 ? _a : shapeOptions.options[ShapeType.image];
|
89 | if (imageOptions instanceof Array) {
|
90 | for (const optionsImage of imageOptions) {
|
91 | await this.loadImageShape(container, optionsImage);
|
92 | }
|
93 | }
|
94 | else {
|
95 | await this.loadImageShape(container, imageOptions);
|
96 | }
|
97 | if (options === null || options === void 0 ? void 0 : options.groups) {
|
98 | for (const groupName in options.groups) {
|
99 | const group = options.groups[groupName];
|
100 | await this.loadImagesFromParticlesOptions(container, group);
|
101 | }
|
102 | }
|
103 | if ((_c = (_b = options === null || options === void 0 ? void 0 : options.destroy) === null || _b === void 0 ? void 0 : _b.split) === null || _c === void 0 ? void 0 : _c.particles) {
|
104 | await this.loadImagesFromParticlesOptions(container, options === null || options === void 0 ? void 0 : options.destroy.split.particles);
|
105 | }
|
106 | }
|
107 | async loadImageShape(container, imageShape) {
|
108 | try {
|
109 | const imageFunc = imageShape.replaceColor ? downloadSvgImage : loadImage;
|
110 | const image = await imageFunc(imageShape.src);
|
111 | if (image) {
|
112 | this.addImage(container, image);
|
113 | }
|
114 | }
|
115 | catch (_a) {
|
116 | console.warn(`tsParticles error - ${imageShape.src} not found`);
|
117 | }
|
118 | }
|
119 | draw(context, particle, radius, opacity) {
|
120 | var _a, _b;
|
121 | if (!context) {
|
122 | return;
|
123 | }
|
124 | const image = particle.image;
|
125 | const element = (_a = image === null || image === void 0 ? void 0 : image.data) === null || _a === void 0 ? void 0 : _a.element;
|
126 | if (!element) {
|
127 | return;
|
128 | }
|
129 | const ratio = (_b = image === null || image === void 0 ? void 0 : image.ratio) !== null && _b !== void 0 ? _b : 1;
|
130 | const pos = {
|
131 | x: -radius,
|
132 | y: -radius,
|
133 | };
|
134 | if (!(image === null || image === void 0 ? void 0 : image.data.svgData) || !(image === null || image === void 0 ? void 0 : image.replaceColor)) {
|
135 | context.globalAlpha = opacity;
|
136 | }
|
137 | context.drawImage(element, pos.x, pos.y, radius * 2, (radius * 2) / ratio);
|
138 | if (!(image === null || image === void 0 ? void 0 : image.data.svgData) || !(image === null || image === void 0 ? void 0 : image.replaceColor)) {
|
139 | context.globalAlpha = 1;
|
140 | }
|
141 | }
|
142 | loadShape(particle) {
|
143 | var _a, _b, _c, _d, _e, _f, _g;
|
144 | if (particle.shape !== "image" && particle.shape !== "images") {
|
145 | return;
|
146 | }
|
147 | const images = this.getImages(particle.container).images;
|
148 | const imageData = particle.shapeData;
|
149 | const image = (_a = images.find((t) => t.source === imageData.src)) !== null && _a !== void 0 ? _a : images[0];
|
150 | const color = particle.getFillColor();
|
151 | let imageRes;
|
152 | if (!image) {
|
153 | return;
|
154 | }
|
155 | if (image.svgData !== undefined && imageData.replaceColor && color) {
|
156 | const svgColoredData = replaceColorSvg(image, color, (_c = (_b = particle.opacity) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : 1);
|
157 |
|
158 | const svg = new Blob([svgColoredData], { type: "image/svg+xml" });
|
159 | const domUrl = URL || window.URL || window.webkitURL || window;
|
160 | const url = domUrl.createObjectURL(svg);
|
161 |
|
162 | const img = new Image();
|
163 | imageRes = {
|
164 | data: Object.assign(Object.assign({}, image), { svgData: svgColoredData }),
|
165 | ratio: imageData.width / imageData.height,
|
166 | replaceColor: (_d = imageData.replaceColor) !== null && _d !== void 0 ? _d : imageData.replace_color,
|
167 | source: imageData.src,
|
168 | };
|
169 | img.addEventListener("load", () => {
|
170 | const pImage = particle.image;
|
171 | if (pImage) {
|
172 | pImage.loaded = true;
|
173 | image.element = img;
|
174 | }
|
175 | domUrl.revokeObjectURL(url);
|
176 | });
|
177 | img.addEventListener("error", () => {
|
178 | domUrl.revokeObjectURL(url);
|
179 |
|
180 | loadImage(imageData.src).then((img2) => {
|
181 | const pImage = particle.image;
|
182 | if (pImage) {
|
183 | image.element = img2 === null || img2 === void 0 ? void 0 : img2.element;
|
184 | pImage.loaded = true;
|
185 | }
|
186 | });
|
187 | });
|
188 | img.src = url;
|
189 | }
|
190 | else {
|
191 | imageRes = {
|
192 | data: image,
|
193 | loaded: true,
|
194 | ratio: imageData.width / imageData.height,
|
195 | replaceColor: (_e = imageData.replaceColor) !== null && _e !== void 0 ? _e : imageData.replace_color,
|
196 | source: imageData.src,
|
197 | };
|
198 | }
|
199 | if (!imageRes.ratio) {
|
200 | imageRes.ratio = 1;
|
201 | }
|
202 | const fill = (_f = imageData.fill) !== null && _f !== void 0 ? _f : particle.fill;
|
203 | const close = (_g = imageData.close) !== null && _g !== void 0 ? _g : particle.close;
|
204 | const imageShape = {
|
205 | image: imageRes,
|
206 | fill,
|
207 | close,
|
208 | };
|
209 | particle.image = imageShape.image;
|
210 | particle.fill = imageShape.fill;
|
211 | particle.close = imageShape.close;
|
212 | }
|
213 | }
|
214 | _ImageDrawer_images = new WeakMap();
|