1 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | import { Utils } from "../Utils";
|
11 | import { ShapeType } from "../Enums";
|
12 | export class ImageDrawer {
|
13 | constructor() {
|
14 | this.images = [];
|
15 | }
|
16 | getSidesCount() {
|
17 | return 12;
|
18 | }
|
19 | getImages(container) {
|
20 | const containerImages = this.images.filter((t) => t.id === container.id);
|
21 | if (!containerImages.length) {
|
22 | this.images.push({
|
23 | id: container.id,
|
24 | images: [],
|
25 | });
|
26 | return this.getImages(container);
|
27 | }
|
28 | return containerImages[0];
|
29 | }
|
30 | addImage(container, image) {
|
31 | const containerImages = this.getImages(container);
|
32 | containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
|
33 | }
|
34 | init(container) {
|
35 | var _a;
|
36 | return __awaiter(this, void 0, void 0, function* () {
|
37 | const options = container.actualOptions;
|
38 | const shapeOptions = options.particles.shape;
|
39 | if (!Utils.isInArray(ShapeType.image, shapeOptions.type) &&
|
40 | !Utils.isInArray(ShapeType.images, shapeOptions.type)) {
|
41 | return;
|
42 | }
|
43 | const imageOptions = (_a = shapeOptions.options[ShapeType.images]) !== null && _a !== void 0 ? _a : shapeOptions.options[ShapeType.image];
|
44 | if (imageOptions instanceof Array) {
|
45 | for (const optionsImage of imageOptions) {
|
46 | yield this.loadImageShape(container, optionsImage);
|
47 | }
|
48 | }
|
49 | else {
|
50 | yield this.loadImageShape(container, imageOptions);
|
51 | }
|
52 | });
|
53 | }
|
54 | destroy() {
|
55 | this.images = [];
|
56 | }
|
57 | loadImageShape(container, imageShape) {
|
58 | return __awaiter(this, void 0, void 0, function* () {
|
59 | try {
|
60 | const image = imageShape.replaceColor
|
61 | ? yield Utils.downloadSvgImage(imageShape.src)
|
62 | : yield Utils.loadImage(imageShape.src);
|
63 | if (image) {
|
64 | this.addImage(container, image);
|
65 | }
|
66 | }
|
67 | catch (_a) {
|
68 | console.warn(`tsParticles error - ${imageShape.src} not found`);
|
69 | }
|
70 | });
|
71 | }
|
72 | draw(context, particle, radius, opacity) {
|
73 | var _a, _b;
|
74 | if (!context) {
|
75 | return;
|
76 | }
|
77 | const image = particle.image;
|
78 | const element = (_a = image === null || image === void 0 ? void 0 : image.data) === null || _a === void 0 ? void 0 : _a.element;
|
79 | if (!element) {
|
80 | return;
|
81 | }
|
82 | const ratio = (_b = image === null || image === void 0 ? void 0 : image.ratio) !== null && _b !== void 0 ? _b : 1;
|
83 | const pos = {
|
84 | x: -radius,
|
85 | y: -radius,
|
86 | };
|
87 | if (!(image === null || image === void 0 ? void 0 : image.data.svgData) || !(image === null || image === void 0 ? void 0 : image.replaceColor)) {
|
88 | context.globalAlpha = opacity;
|
89 | }
|
90 | context.drawImage(element, pos.x, pos.y, radius * 2, (radius * 2) / ratio);
|
91 | if (!(image === null || image === void 0 ? void 0 : image.data.svgData) || !(image === null || image === void 0 ? void 0 : image.replaceColor)) {
|
92 | context.globalAlpha = 1;
|
93 | }
|
94 | }
|
95 | }
|