@tsparticles/engine
Version:
Easily create highly customizable particle, confetti and fireworks animations and use them as animated backgrounds for your website. Ready to use components available also for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Riot.js, Inferno.
142 lines (141 loc) • 5.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Engine = void 0;
const ErrorUtils_js_1 = require("../Utils/ErrorUtils.js");
const Utils_js_1 = require("../Utils/Utils.js");
const Constants_js_1 = require("./Utils/Constants.js");
const EventDispatcher_js_1 = require("../Utils/EventDispatcher.js");
const PluginManager_js_1 = require("./Utils/PluginManager.js");
const MathUtils_js_1 = require("../Utils/MathUtils.js");
class Engine {
pluginManager = new PluginManager_js_1.PluginManager(this);
#anonymousElementId = 0;
#anonymousElementIds = new WeakMap();
#domArray = [];
#eventDispatcher = new EventDispatcher_js_1.EventDispatcher();
#initialized = false;
#loadSequence = 0;
#loadSequences = new Map();
get items() {
return this.#domArray;
}
get version() {
return "4.6.0";
}
addEventListener(type, listener) {
this.#eventDispatcher.addEventListener(type, listener);
}
checkVersion(pluginVersion) {
if (this.version === pluginVersion) {
return;
}
throw new Error((0, ErrorUtils_js_1.getErrorMessage)(ErrorUtils_js_1.ErrorCodes.engineVersionMismatch, this.version, pluginVersion));
}
dispatchEvent(type, args) {
this.#eventDispatcher.dispatchEvent(type, args);
}
async init() {
if (this.#initialized) {
return;
}
await this.pluginManager.init();
this.#initialized = true;
}
item(index) {
const items = this.items, item = items[index];
if (item?.destroyed) {
items.splice(index, Constants_js_1.removeDeleteCount);
return;
}
return item;
}
async load(params) {
let domSourceElement;
if (typeof HTMLElement !== "undefined" && params.element instanceof HTMLElement) {
domSourceElement = params.element;
}
const id = params.id ??
(domSourceElement ? this.#getElementId(domSourceElement) : undefined) ??
`tsparticles${Math.floor((0, MathUtils_js_1.getRandom)() * Constants_js_1.loadRandomFactor).toString()}`, loadSequence = ++this.#loadSequence;
this.#loadSequences.set(id, loadSequence);
try {
await this.init();
const { Container } = await import("./Container.js"), { index, url } = params, options = url ? await (0, Utils_js_1.getDataFromUrl)({ fallback: params.options, url, index }) : params.options;
if (this.#loadSequences.get(id) !== loadSequence) {
return undefined;
}
const currentOptions = (0, Utils_js_1.itemFromSingleOrMultiple)(options, index), { items } = this, oldIndex = items.findIndex(v => v.id.description === id), newItem = new Container({
dispatchCallback: (eventType, args) => {
this.dispatchEvent(eventType, args);
},
id,
onDestroy: (remove) => {
if (!remove) {
return;
}
const mainArr = this.items, idx = mainArr.indexOf(newItem);
if (idx >= Constants_js_1.removeMinIndex) {
mainArr.splice(idx, Constants_js_1.removeDeleteCount);
}
},
pluginManager: this.pluginManager,
sourceOptions: currentOptions,
});
if (oldIndex >= Constants_js_1.loadMinIndex) {
const old = this.item(oldIndex), deleteCount = old ? Constants_js_1.one : Constants_js_1.none;
if (old && !old.destroyed) {
old.destroy(false);
}
items.splice(oldIndex, deleteCount, newItem);
}
else {
items.push(newItem);
}
const sourceCanvas = typeof OffscreenCanvas !== "undefined" && params.element instanceof OffscreenCanvas
? params.element
: (0, Utils_js_1.getCanvasFromContainer)((0, Utils_js_1.getDomContainer)(id, domSourceElement));
newItem.canvas.loadCanvas(sourceCanvas);
try {
await newItem.start();
}
catch (error) {
newItem.destroy(false);
const failedIndex = items.indexOf(newItem);
if (failedIndex >= Constants_js_1.loadMinIndex) {
items.splice(failedIndex, Constants_js_1.removeDeleteCount);
}
throw error;
}
if (newItem.destroyed) {
return undefined;
}
return newItem;
}
finally {
if (this.#loadSequences.get(id) === loadSequence) {
this.#loadSequences.delete(id);
}
}
}
async refresh(refresh = true) {
if (!refresh) {
return;
}
await Promise.all(this.items.map(t => t.refresh()));
}
removeEventListener(type, listener) {
this.#eventDispatcher.removeEventListener(type, listener);
}
#getAnonymousElementId(element) {
let id = this.#anonymousElementIds.get(element);
if (!id) {
id = `tsparticles${Math.floor((0, MathUtils_js_1.getRandom)() * Constants_js_1.loadRandomFactor).toString()}_${this.#anonymousElementId++}`;
this.#anonymousElementIds.set(element, id);
}
return id;
}
#getElementId(element) {
return element.id.length ? element.id : this.#getAnonymousElementId(element);
}
}
exports.Engine = Engine;