-
-
Notifications
You must be signed in to change notification settings - Fork 800
/
EmittersPlugin.ts
127 lines (105 loc) · 4.15 KB
/
EmittersPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { EmitterOptions, IEmitterModeOptions, IEmitterOptions } from "./types.js";
import {
type IOptions,
type IPlugin,
type RecursivePartial,
executeOnSingleOrMultiple,
isArray,
isInArray,
} from "@tsparticles/engine";
import { Emitter } from "./Options/Classes/Emitter.js";
import { EmitterClickMode } from "./Enums/EmitterClickMode.js";
import type { EmitterContainer } from "./EmitterContainer.js";
import { Emitters } from "./Emitters.js";
import type { EmittersEngine } from "./EmittersEngine.js";
import type { IEmitter } from "./Options/Interfaces/IEmitter.js";
/**
*/
export class EmittersPlugin implements IPlugin {
readonly id;
private readonly _engine;
constructor(engine: EmittersEngine) {
this._engine = engine;
this.id = "emitters";
}
getPlugin(container: EmitterContainer): Promise<Emitters> {
return Promise.resolve(new Emitters(this._engine, container));
}
loadOptions(options: EmitterOptions, source?: RecursivePartial<IEmitterOptions>): void {
if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
return;
}
if (source?.emitters) {
options.emitters = executeOnSingleOrMultiple(source.emitters, emitter => {
const tmp = new Emitter();
tmp.load(emitter);
return tmp;
});
}
const interactivityEmitters = source?.interactivity?.modes?.emitters;
if (interactivityEmitters) {
if (isArray(interactivityEmitters)) {
options.interactivity.modes.emitters = {
random: {
count: 1,
enable: true,
},
value: interactivityEmitters.map(s => {
const tmp = new Emitter();
tmp.load(s);
return tmp;
}),
};
} else {
const emitterMode = interactivityEmitters as IEmitterModeOptions;
if (emitterMode.value !== undefined) {
const defaultCount = 1;
if (isArray(emitterMode.value)) {
options.interactivity.modes.emitters = {
random: {
count: emitterMode.random.count ?? defaultCount,
enable: emitterMode.random.enable ?? false,
},
value: emitterMode.value.map(s => {
const tmp = new Emitter();
tmp.load(s);
return tmp;
}),
};
} else {
const tmp = new Emitter();
tmp.load(emitterMode.value);
options.interactivity.modes.emitters = {
random: {
count: emitterMode.random.count ?? defaultCount,
enable: emitterMode.random.enable ?? false,
},
value: tmp,
};
}
} else {
const emitterOptions = (options.interactivity.modes.emitters = {
random: {
count: 1,
enable: false,
},
value: new Emitter(),
});
emitterOptions.value.load(interactivityEmitters as IEmitter);
}
}
}
}
needsPlugin(options?: RecursivePartial<IOptions & IEmitterOptions>): boolean {
if (!options) {
return false;
}
const emitters = options.emitters;
return (
(isArray(emitters) && !!emitters.length) ||
emitters !== undefined ||
(!!options.interactivity?.events?.onClick?.mode &&
isInArray(EmitterClickMode.emitter, options.interactivity.events.onClick.mode))
);
}
}