1 | import { EventDispatcher } from "./Utils/EventDispatcher";
|
2 | import { Loader } from "./Core/Loader";
|
3 | import { Plugins } from "./Core/Utils/Plugins";
|
4 | export class Engine {
|
5 | constructor() {
|
6 | this._domArray = [];
|
7 | this._eventDispatcher = new EventDispatcher();
|
8 | this._initialized = false;
|
9 | this._loader = new Loader(this);
|
10 | this.plugins = new Plugins(this);
|
11 | }
|
12 | addEventListener(type, listener) {
|
13 | this._eventDispatcher.addEventListener(type, listener);
|
14 | }
|
15 | async addInteractor(name, interactorInitializer) {
|
16 | this.plugins.addInteractor(name, interactorInitializer);
|
17 | await this.refresh();
|
18 | }
|
19 | async addMover(name, moverInitializer) {
|
20 | this.plugins.addParticleMover(name, moverInitializer);
|
21 | await this.refresh();
|
22 | }
|
23 | async addParticleUpdater(name, updaterInitializer) {
|
24 | this.plugins.addParticleUpdater(name, updaterInitializer);
|
25 | await this.refresh();
|
26 | }
|
27 | async addPathGenerator(name, generator) {
|
28 | this.plugins.addPathGenerator(name, generator);
|
29 | await this.refresh();
|
30 | }
|
31 | async addPlugin(plugin) {
|
32 | this.plugins.addPlugin(plugin);
|
33 | await this.refresh();
|
34 | }
|
35 | async addPreset(preset, options, override = false) {
|
36 | this.plugins.addPreset(preset, options, override);
|
37 | await this.refresh();
|
38 | }
|
39 | async addShape(shape, drawer, init, afterEffect, destroy) {
|
40 | let customDrawer;
|
41 | if (typeof drawer === "function") {
|
42 | customDrawer = {
|
43 | afterEffect: afterEffect,
|
44 | destroy: destroy,
|
45 | draw: drawer,
|
46 | init: init,
|
47 | };
|
48 | }
|
49 | else {
|
50 | customDrawer = drawer;
|
51 | }
|
52 | this.plugins.addShapeDrawer(shape, customDrawer);
|
53 | await this.refresh();
|
54 | }
|
55 | dispatchEvent(type, args) {
|
56 | this._eventDispatcher.dispatchEvent(type, args);
|
57 | }
|
58 | dom() {
|
59 | return this._domArray;
|
60 | }
|
61 | domItem(index) {
|
62 | const dom = this.dom(), item = dom[index];
|
63 | if (item && !item.destroyed) {
|
64 | return item;
|
65 | }
|
66 | dom.splice(index, 1);
|
67 | }
|
68 | init() {
|
69 | if (!this._initialized) {
|
70 | this._initialized = true;
|
71 | }
|
72 | }
|
73 | async load(tagId, options) {
|
74 | return this._loader.load(tagId, options);
|
75 | }
|
76 | async loadFromArray(tagId, options, index) {
|
77 | return this._loader.load(tagId, options, index);
|
78 | }
|
79 | async loadJSON(tagId, pathConfigJson, index) {
|
80 | return this._loader.loadJSON(tagId, pathConfigJson, index);
|
81 | }
|
82 | async refresh() {
|
83 | for (const instance of this.dom()) {
|
84 | await instance.refresh();
|
85 | }
|
86 | }
|
87 | removeEventListener(type, listener) {
|
88 | this._eventDispatcher.removeEventListener(type, listener);
|
89 | }
|
90 | async set(id, element, options) {
|
91 | return this._loader.set(id, element, options);
|
92 | }
|
93 | async setJSON(id, element, pathConfigJson, index) {
|
94 | return this._loader.setJSON(id, element, pathConfigJson, index);
|
95 | }
|
96 | setOnClickHandler(callback) {
|
97 | const dom = this.dom();
|
98 | if (!dom.length) {
|
99 | throw new Error("Can only set click handlers after calling tsParticles.load() or tsParticles.loadJSON()");
|
100 | }
|
101 | for (const domItem of dom) {
|
102 | domItem.addClickHandler(callback);
|
103 | }
|
104 | }
|
105 | }
|