2023-02-17 11:06:01 +01:00

136 lines
5.3 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageDrawer = void 0;
const Utils_1 = require("./Utils");
class ImageDrawer {
constructor() {
this._images = [];
}
addImage(container, image) {
const containerImages = this.getImages(container);
containerImages === null || containerImages === void 0 ? void 0 : containerImages.images.push(image);
}
destroy() {
this._images = [];
}
draw(context, particle, radius, opacity) {
var _a;
const image = particle.image, element = image === null || image === void 0 ? void 0 : image.element;
if (!element) {
return;
}
const ratio = (_a = image === null || image === void 0 ? void 0 : image.ratio) !== null && _a !== void 0 ? _a : 1, pos = {
x: -radius,
y: -radius,
};
context.globalAlpha = opacity;
context.drawImage(element, pos.x, pos.y, radius * 2, (radius * 2) / ratio);
context.globalAlpha = 1;
}
getImages(container) {
const containerImages = this._images.find((t) => t.id === container.id);
if (!containerImages) {
this._images.push({
id: container.id,
images: [],
});
return this.getImages(container);
}
else {
return containerImages;
}
}
getSidesCount() {
return 12;
}
loadShape(particle) {
if (particle.shape !== "image" && particle.shape !== "images") {
return;
}
const container = particle.container, images = this.getImages(container).images, imageData = particle.shapeData, image = images.find((t) => t.source === imageData.src);
if (!image) {
this.loadImageShape(container, imageData).then(() => {
this.loadShape(particle);
});
}
}
particleInit(container, particle) {
var _a;
if (particle.shape !== "image" && particle.shape !== "images") {
return;
}
const images = this.getImages(container).images, imageData = particle.shapeData, color = particle.getFillColor(), replaceColor = (_a = imageData.replaceColor) !== null && _a !== void 0 ? _a : imageData.replace_color, image = images.find((t) => t.source === imageData.src);
if (!image) {
return;
}
if (image.loading) {
setTimeout(() => {
this.particleInit(container, particle);
});
return;
}
(() => __awaiter(this, void 0, void 0, function* () {
var _b, _c;
let imageRes;
if (image.svgData && color) {
imageRes = yield (0, Utils_1.replaceImageColor)(image, imageData, color, particle);
}
else {
imageRes = {
color,
data: image,
element: image.element,
loaded: true,
ratio: imageData.width / imageData.height,
replaceColor: replaceColor,
source: imageData.src,
};
}
if (!imageRes.ratio) {
imageRes.ratio = 1;
}
const fill = (_b = imageData.fill) !== null && _b !== void 0 ? _b : particle.fill, close = (_c = imageData.close) !== null && _c !== void 0 ? _c : particle.close, imageShape = {
image: imageRes,
fill,
close,
};
particle.image = imageShape.image;
particle.fill = imageShape.fill;
particle.close = imageShape.close;
}))();
}
loadImageShape(container, imageShape) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const source = imageShape.src;
if (!source) {
throw new Error("Error tsParticles - No image.src");
}
try {
const image = {
source: source,
type: source.substring(source.length - 3),
error: false,
loading: true,
};
this.addImage(container, image);
const imageFunc = ((_a = imageShape.replaceColor) !== null && _a !== void 0 ? _a : imageShape.replace_color) ? Utils_1.downloadSvgImage : Utils_1.loadImage;
yield imageFunc(image);
}
catch (_b) {
throw new Error(`tsParticles error - ${imageShape.src} not found`);
}
});
}
}
exports.ImageDrawer = ImageDrawer;