@tsparticles/plugin-sounds 4.0.0-alpha.2 → 4.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/249.min.js +1 -0
- package/807.min.js +33 -0
- package/browser/Options/Classes/Sounds.js +5 -0
- package/browser/Options/Classes/SoundsAudio.js +2 -0
- package/browser/Options/Classes/SoundsEvent.js +5 -0
- package/browser/Options/Classes/SoundsIcon.js +5 -0
- package/browser/Options/Classes/SoundsIcons.js +5 -0
- package/browser/Options/Classes/SoundsMelody.js +3 -0
- package/browser/Options/Classes/SoundsNote.js +2 -0
- package/browser/Options/Classes/SoundsVolume.js +4 -0
- package/browser/SoundsPlugin.js +7 -6
- package/browser/SoundsPluginInstance.js +437 -0
- package/browser/constants.js +1 -0
- package/browser/index.js +3 -3
- package/cjs/Options/Classes/Sounds.js +5 -0
- package/cjs/Options/Classes/SoundsAudio.js +2 -0
- package/cjs/Options/Classes/SoundsEvent.js +5 -0
- package/cjs/Options/Classes/SoundsIcon.js +5 -0
- package/cjs/Options/Classes/SoundsIcons.js +5 -0
- package/cjs/Options/Classes/SoundsMelody.js +3 -0
- package/cjs/Options/Classes/SoundsNote.js +2 -0
- package/cjs/Options/Classes/SoundsVolume.js +4 -0
- package/cjs/SoundsPlugin.js +7 -6
- package/cjs/SoundsPluginInstance.js +437 -0
- package/cjs/constants.js +1 -0
- package/cjs/index.js +3 -3
- package/dist_browser_SoundsPluginInstance_js.js +40 -0
- package/dist_browser_SoundsPlugin_js.js +16 -26
- package/esm/Options/Classes/Sounds.js +5 -0
- package/esm/Options/Classes/SoundsAudio.js +2 -0
- package/esm/Options/Classes/SoundsEvent.js +5 -0
- package/esm/Options/Classes/SoundsIcon.js +5 -0
- package/esm/Options/Classes/SoundsIcons.js +5 -0
- package/esm/Options/Classes/SoundsMelody.js +3 -0
- package/esm/Options/Classes/SoundsNote.js +2 -0
- package/esm/Options/Classes/SoundsVolume.js +4 -0
- package/esm/SoundsPlugin.js +7 -6
- package/esm/SoundsPluginInstance.js +437 -0
- package/esm/constants.js +1 -0
- package/esm/index.js +3 -3
- package/package.json +2 -2
- package/report.html +3 -3
- package/tsparticles.plugin.sounds.js +11 -11
- package/tsparticles.plugin.sounds.min.js +2 -2
- package/types/SoundsPlugin.d.ts +4 -5
- package/types/{SoundsInstance.d.ts → SoundsPluginInstance.d.ts} +1 -1
- package/types/constants.d.ts +1 -0
- package/types/index.d.ts +1 -1
- package/umd/Options/Classes/Sounds.js +5 -0
- package/umd/Options/Classes/SoundsAudio.js +2 -0
- package/umd/Options/Classes/SoundsEvent.js +5 -0
- package/umd/Options/Classes/SoundsIcon.js +5 -0
- package/umd/Options/Classes/SoundsIcons.js +5 -0
- package/umd/Options/Classes/SoundsMelody.js +3 -0
- package/umd/Options/Classes/SoundsNote.js +2 -0
- package/umd/Options/Classes/SoundsVolume.js +4 -0
- package/umd/SoundsPlugin.js +46 -11
- package/umd/SoundsPluginInstance.js +451 -0
- package/umd/constants.js +14 -0
- package/umd/index.js +3 -3
- package/902.min.js +0 -2
- package/902.min.js.LICENSE.txt +0 -1
- package/browser/SoundsInstance.js +0 -426
- package/cjs/SoundsInstance.js +0 -426
- package/esm/SoundsInstance.js +0 -426
- package/tsparticles.plugin.sounds.min.js.LICENSE.txt +0 -1
- package/umd/SoundsInstance.js +0 -440
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "@tsparticles/engine", "./enums.js", "./utils.js", "./constants.js"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SoundsPluginInstance = void 0;
|
|
13
|
+
const engine_1 = require("@tsparticles/engine");
|
|
14
|
+
const enums_js_1 = require("./enums.js");
|
|
15
|
+
const utils_js_1 = require("./utils.js");
|
|
16
|
+
const constants_js_1 = require("./constants.js");
|
|
17
|
+
const zIndexOffset = 1, rightOffset = 1, minVolume = 0;
|
|
18
|
+
function initImage(data) {
|
|
19
|
+
const img = (0, engine_1.safeDocument)().createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
|
|
20
|
+
setIconStyle(img, pos.top + margin, pos.right -
|
|
21
|
+
(margin * (rightOffsets.length + rightOffset) + width + rightOffsets.reduce((a, b) => a + b, defaultAccumulator)), display, options.fullScreen.zIndex + zIndexOffset, width, margin, style);
|
|
22
|
+
img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
|
|
23
|
+
const parent = container.canvas.element?.parentNode ?? (0, engine_1.safeDocument)().body;
|
|
24
|
+
parent.append(img);
|
|
25
|
+
img.addEventListener("click", () => {
|
|
26
|
+
void clickCb();
|
|
27
|
+
});
|
|
28
|
+
return img;
|
|
29
|
+
}
|
|
30
|
+
function removeImage(image) {
|
|
31
|
+
if (!image) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
image.remove();
|
|
35
|
+
}
|
|
36
|
+
function setIconStyle(icon, top, left, display, zIndex, width, margin, style) {
|
|
37
|
+
icon.style.userSelect = "none";
|
|
38
|
+
icon.style.position = "absolute";
|
|
39
|
+
icon.style.top = `${(top + margin).toString()}px`;
|
|
40
|
+
icon.style.left = `${(left - margin - width).toString()}px`;
|
|
41
|
+
icon.style.display = display;
|
|
42
|
+
icon.style.zIndex = (zIndex + zIndexOffset).toString();
|
|
43
|
+
icon.style.cssText += style;
|
|
44
|
+
}
|
|
45
|
+
class SoundsPluginInstance {
|
|
46
|
+
_audioMap;
|
|
47
|
+
_audioSources;
|
|
48
|
+
_container;
|
|
49
|
+
_engine;
|
|
50
|
+
_gain;
|
|
51
|
+
_muteImg;
|
|
52
|
+
_unmuteImg;
|
|
53
|
+
_volume;
|
|
54
|
+
_volumeDownImg;
|
|
55
|
+
_volumeUpImg;
|
|
56
|
+
constructor(container, engine) {
|
|
57
|
+
this._container = container;
|
|
58
|
+
this._engine = engine;
|
|
59
|
+
this._volume = 0;
|
|
60
|
+
this._audioSources = [];
|
|
61
|
+
this._audioMap = new Map();
|
|
62
|
+
}
|
|
63
|
+
async init() {
|
|
64
|
+
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
65
|
+
if (!soundsOptions?.enable) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (soundsOptions.autoPlay && (0, utils_js_1.isWindowMuted)()) {
|
|
69
|
+
const firstClickHandler = () => {
|
|
70
|
+
removeEventListener(constants_js_1.mouseDownEvent, firstClickHandler);
|
|
71
|
+
removeEventListener(constants_js_1.touchStartEvent, firstClickHandler);
|
|
72
|
+
(0, utils_js_1.unmuteWindow)();
|
|
73
|
+
void this.unmute();
|
|
74
|
+
}, listenerOptions = {
|
|
75
|
+
capture: true,
|
|
76
|
+
once: true,
|
|
77
|
+
};
|
|
78
|
+
addEventListener(constants_js_1.mouseDownEvent, firstClickHandler, listenerOptions);
|
|
79
|
+
addEventListener(constants_js_1.touchStartEvent, firstClickHandler, listenerOptions);
|
|
80
|
+
}
|
|
81
|
+
this._volume = soundsOptions.volume.value;
|
|
82
|
+
const events = soundsOptions.events;
|
|
83
|
+
this._audioMap = new Map();
|
|
84
|
+
for (const event of events) {
|
|
85
|
+
if (!event.audio) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const promises = (0, engine_1.executeOnSingleOrMultiple)(event.audio, async (audio) => {
|
|
89
|
+
const response = await fetch(audio.source);
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const arrayBuffer = await response.arrayBuffer(), audioContext = this._getAudioContext(), audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
|
94
|
+
this._audioMap.set(audio.source, audioBuffer);
|
|
95
|
+
});
|
|
96
|
+
if (promises instanceof Promise) {
|
|
97
|
+
await promises;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
await Promise.allSettled(promises);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async mute() {
|
|
105
|
+
if (!this._container.muted) {
|
|
106
|
+
await this.toggleMute();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async start() {
|
|
110
|
+
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
111
|
+
if (!soundsOptions?.enable || !container.canvas.element) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
container.muted = true;
|
|
115
|
+
const canvas = container.canvas.element, pos = {
|
|
116
|
+
top: canvas.offsetTop,
|
|
117
|
+
right: canvas.offsetLeft + canvas.offsetWidth,
|
|
118
|
+
}, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10, toggleMute = async () => {
|
|
119
|
+
await this.toggleMute();
|
|
120
|
+
}, enableIcons = soundsOptions.icons.enable, display = enableIcons ? enums_js_1.ImageDisplay.Block : enums_js_1.ImageDisplay.None;
|
|
121
|
+
this._muteImg = initImage({
|
|
122
|
+
container,
|
|
123
|
+
options,
|
|
124
|
+
pos,
|
|
125
|
+
display,
|
|
126
|
+
iconOptions: mute,
|
|
127
|
+
margin,
|
|
128
|
+
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
129
|
+
clickCb: toggleMute,
|
|
130
|
+
});
|
|
131
|
+
this._unmuteImg = initImage({
|
|
132
|
+
container,
|
|
133
|
+
options,
|
|
134
|
+
pos,
|
|
135
|
+
display: enums_js_1.ImageDisplay.None,
|
|
136
|
+
iconOptions: unmute,
|
|
137
|
+
margin,
|
|
138
|
+
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
139
|
+
clickCb: toggleMute,
|
|
140
|
+
});
|
|
141
|
+
this._volumeDownImg = initImage({
|
|
142
|
+
container,
|
|
143
|
+
options,
|
|
144
|
+
pos,
|
|
145
|
+
display,
|
|
146
|
+
iconOptions: volumeDown,
|
|
147
|
+
margin,
|
|
148
|
+
rightOffsets: [volumeUp.width],
|
|
149
|
+
clickCb: async () => {
|
|
150
|
+
await this.volumeDown();
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
this._volumeUpImg = initImage({
|
|
154
|
+
container,
|
|
155
|
+
options,
|
|
156
|
+
pos,
|
|
157
|
+
display,
|
|
158
|
+
iconOptions: volumeUp,
|
|
159
|
+
margin,
|
|
160
|
+
rightOffsets: [],
|
|
161
|
+
clickCb: async () => {
|
|
162
|
+
await this.volumeUp();
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
if (!(0, utils_js_1.isWindowMuted)() && soundsOptions.autoPlay) {
|
|
166
|
+
await this.unmute();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
stop() {
|
|
170
|
+
this._container.muted = true;
|
|
171
|
+
void (async () => {
|
|
172
|
+
await this._mute();
|
|
173
|
+
removeImage(this._muteImg);
|
|
174
|
+
removeImage(this._unmuteImg);
|
|
175
|
+
removeImage(this._volumeDownImg);
|
|
176
|
+
removeImage(this._volumeUpImg);
|
|
177
|
+
})();
|
|
178
|
+
}
|
|
179
|
+
async toggleMute() {
|
|
180
|
+
const container = this._container;
|
|
181
|
+
container.muted = !container.muted;
|
|
182
|
+
this._updateMuteIcons();
|
|
183
|
+
await this._updateMuteStatus();
|
|
184
|
+
}
|
|
185
|
+
async unmute() {
|
|
186
|
+
if (this._container.muted) {
|
|
187
|
+
await this.toggleMute();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async volumeDown() {
|
|
191
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
192
|
+
if (!soundsOptions?.enable) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (container.muted) {
|
|
196
|
+
this._volume = 0;
|
|
197
|
+
}
|
|
198
|
+
this._volume -= soundsOptions.volume.step;
|
|
199
|
+
await this._updateVolume();
|
|
200
|
+
}
|
|
201
|
+
async volumeUp() {
|
|
202
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
203
|
+
if (!soundsOptions?.enable) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this._volume += soundsOptions.volume.step;
|
|
207
|
+
await this._updateVolume();
|
|
208
|
+
}
|
|
209
|
+
_addBuffer = audioCtx => {
|
|
210
|
+
const buffer = audioCtx.createBufferSource();
|
|
211
|
+
this._audioSources.push(buffer);
|
|
212
|
+
return buffer;
|
|
213
|
+
};
|
|
214
|
+
_addOscillator = audioCtx => {
|
|
215
|
+
const oscillator = audioCtx.createOscillator();
|
|
216
|
+
this._audioSources.push(oscillator);
|
|
217
|
+
return oscillator;
|
|
218
|
+
};
|
|
219
|
+
_getAudioContext() {
|
|
220
|
+
const container = this._container;
|
|
221
|
+
container.audioContext ??= new AudioContext();
|
|
222
|
+
return container.audioContext;
|
|
223
|
+
}
|
|
224
|
+
_initEvents = () => {
|
|
225
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
226
|
+
if (!soundsOptions?.enable || !container.canvas.element) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const event of soundsOptions.events) {
|
|
230
|
+
const cb = (args) => {
|
|
231
|
+
if (!args) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
void (async () => {
|
|
235
|
+
const filterNotValid = event.filter && !event.filter(args);
|
|
236
|
+
if (this._container !== args.container) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (!!this._container.muted || this._container.destroyed) {
|
|
240
|
+
(0, engine_1.executeOnSingleOrMultiple)(event.event, item => {
|
|
241
|
+
this._engine.removeEventListener(item, cb);
|
|
242
|
+
});
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (filterNotValid) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const defaultNoteIndex = 0;
|
|
249
|
+
if (event.audio) {
|
|
250
|
+
const audio = (0, engine_1.itemFromSingleOrMultiple)(event.audio);
|
|
251
|
+
if (!audio) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this._playBuffer(audio);
|
|
255
|
+
}
|
|
256
|
+
else if (event.melodies) {
|
|
257
|
+
const melody = (0, engine_1.itemFromArray)(event.melodies);
|
|
258
|
+
if (!melody) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (melody.melodies.length) {
|
|
262
|
+
await Promise.allSettled(melody.melodies.map(m => this._playNote(m.notes, defaultNoteIndex, melody.loop)));
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
await this._playNote(melody.notes, defaultNoteIndex, melody.loop);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
else if (event.notes) {
|
|
269
|
+
const note = (0, engine_1.itemFromArray)(event.notes);
|
|
270
|
+
if (!note) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
await this._playNote([note], defaultNoteIndex, false);
|
|
274
|
+
}
|
|
275
|
+
})();
|
|
276
|
+
};
|
|
277
|
+
(0, engine_1.executeOnSingleOrMultiple)(event.event, item => {
|
|
278
|
+
this._engine.addEventListener(item, cb);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
_mute = async () => {
|
|
283
|
+
const container = this._container, audioContext = this._getAudioContext();
|
|
284
|
+
for (const source of this._audioSources) {
|
|
285
|
+
this._removeAudioSource(source);
|
|
286
|
+
}
|
|
287
|
+
if (this._gain) {
|
|
288
|
+
this._gain.disconnect();
|
|
289
|
+
}
|
|
290
|
+
await audioContext.close();
|
|
291
|
+
container.audioContext = undefined;
|
|
292
|
+
this._engine.dispatchEvent(enums_js_1.SoundsEventType.mute, { container: this._container });
|
|
293
|
+
};
|
|
294
|
+
_playBuffer = audio => {
|
|
295
|
+
const audioBuffer = this._audioMap.get(audio.source);
|
|
296
|
+
if (!audioBuffer) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const audioCtx = this._container.audioContext;
|
|
300
|
+
if (!audioCtx) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const source = this._addBuffer(audioCtx);
|
|
304
|
+
source.loop = audio.loop;
|
|
305
|
+
source.buffer = audioBuffer;
|
|
306
|
+
source.connect(this._gain ?? audioCtx.destination);
|
|
307
|
+
source.start();
|
|
308
|
+
};
|
|
309
|
+
_playFrequency = async (frequency, duration) => {
|
|
310
|
+
if (!this._gain || this._container.muted) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const audioContext = this._getAudioContext(), oscillator = this._addOscillator(audioContext);
|
|
314
|
+
oscillator.connect(this._gain);
|
|
315
|
+
oscillator.type = "sine";
|
|
316
|
+
oscillator.frequency.value = frequency;
|
|
317
|
+
oscillator.start();
|
|
318
|
+
return new Promise(resolve => {
|
|
319
|
+
setTimeout(() => {
|
|
320
|
+
this._removeAudioSource(oscillator);
|
|
321
|
+
resolve();
|
|
322
|
+
}, duration);
|
|
323
|
+
});
|
|
324
|
+
};
|
|
325
|
+
_playMuteSound = () => {
|
|
326
|
+
if (this._container.muted) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
330
|
+
gain.connect(audioContext.destination);
|
|
331
|
+
gain.gain.value = 0;
|
|
332
|
+
const oscillator = audioContext.createOscillator();
|
|
333
|
+
oscillator.connect(gain);
|
|
334
|
+
oscillator.type = "sine";
|
|
335
|
+
oscillator.frequency.value = 1;
|
|
336
|
+
oscillator.start();
|
|
337
|
+
setTimeout(() => {
|
|
338
|
+
oscillator.stop();
|
|
339
|
+
oscillator.disconnect();
|
|
340
|
+
gain.disconnect();
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
_playNote = async (notes, noteIdx, loop) => {
|
|
344
|
+
if (this._container.muted) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const note = notes[noteIdx];
|
|
348
|
+
if (!note) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const value = note.value, promises = (0, engine_1.executeOnSingleOrMultiple)(value, async (_, idx) => {
|
|
352
|
+
return this._playNoteValue(notes, noteIdx, idx);
|
|
353
|
+
});
|
|
354
|
+
await ((0, engine_1.isArray)(promises) ? Promise.allSettled(promises) : promises);
|
|
355
|
+
const indexOffset = 1;
|
|
356
|
+
let nextNoteIdx = noteIdx + indexOffset;
|
|
357
|
+
if (loop && nextNoteIdx >= notes.length) {
|
|
358
|
+
nextNoteIdx = nextNoteIdx % notes.length;
|
|
359
|
+
}
|
|
360
|
+
await this._playNote(notes, nextNoteIdx, loop);
|
|
361
|
+
};
|
|
362
|
+
_playNoteValue = async (notes, noteIdx, valueIdx) => {
|
|
363
|
+
const note = notes[noteIdx];
|
|
364
|
+
if (!note) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
const value = (0, engine_1.itemFromSingleOrMultiple)(note.value, valueIdx, true);
|
|
368
|
+
if (!value) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
try {
|
|
372
|
+
const freq = (0, utils_js_1.getNoteFrequency)(value);
|
|
373
|
+
if (!(0, engine_1.isNumber)(freq)) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
await this._playFrequency(freq, note.duration);
|
|
377
|
+
}
|
|
378
|
+
catch (e) {
|
|
379
|
+
(0, engine_1.getLogger)().error(e);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
_removeAudioSource = source => {
|
|
383
|
+
source.stop();
|
|
384
|
+
source.disconnect();
|
|
385
|
+
const deleteCount = 1;
|
|
386
|
+
this._audioSources.splice(this._audioSources.indexOf(source), deleteCount);
|
|
387
|
+
};
|
|
388
|
+
_unmute = () => {
|
|
389
|
+
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
390
|
+
if (!soundsOptions) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
394
|
+
gain.connect(audioContext.destination);
|
|
395
|
+
gain.gain.value = soundsOptions.volume.value / engine_1.percentDenominator;
|
|
396
|
+
this._gain = gain;
|
|
397
|
+
this._initEvents();
|
|
398
|
+
this._engine.dispatchEvent(enums_js_1.SoundsEventType.unmute, { container: this._container });
|
|
399
|
+
};
|
|
400
|
+
_updateMuteIcons = () => {
|
|
401
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
402
|
+
if (!soundsOptions?.enable || !soundsOptions.icons.enable) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const muteImg = this._muteImg, unmuteImg = this._unmuteImg;
|
|
406
|
+
if (muteImg) {
|
|
407
|
+
muteImg.style.display = container.muted ? "block" : "none";
|
|
408
|
+
}
|
|
409
|
+
if (unmuteImg) {
|
|
410
|
+
unmuteImg.style.display = container.muted ? "none" : "block";
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
_updateMuteStatus = async () => {
|
|
414
|
+
const container = this._container, audioContext = this._getAudioContext();
|
|
415
|
+
if (container.muted) {
|
|
416
|
+
await audioContext.suspend();
|
|
417
|
+
await this._mute();
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
await audioContext.resume();
|
|
421
|
+
this._unmute();
|
|
422
|
+
this._playMuteSound();
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
_updateVolume = async () => {
|
|
426
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
427
|
+
if (!soundsOptions?.enable) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
(0, engine_1.clamp)(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
|
|
431
|
+
let stateChanged = false;
|
|
432
|
+
if (this._volume <= minVolume && !container.muted) {
|
|
433
|
+
this._volume = 0;
|
|
434
|
+
container.muted = true;
|
|
435
|
+
stateChanged = true;
|
|
436
|
+
}
|
|
437
|
+
else if (this._volume > minVolume && container.muted) {
|
|
438
|
+
container.muted = false;
|
|
439
|
+
stateChanged = true;
|
|
440
|
+
}
|
|
441
|
+
if (stateChanged) {
|
|
442
|
+
this._updateMuteIcons();
|
|
443
|
+
await this._updateMuteStatus();
|
|
444
|
+
}
|
|
445
|
+
if (this._gain?.gain) {
|
|
446
|
+
this._gain.gain.value = this._volume / engine_1.percentDenominator;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
exports.SoundsPluginInstance = SoundsPluginInstance;
|
|
451
|
+
});
|
package/umd/constants.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.touchStartEvent = exports.mouseDownEvent = void 0;
|
|
13
|
+
exports.mouseDownEvent = "pointerdown", exports.touchStartEvent = "touchstart";
|
|
14
|
+
});
|
package/umd/index.js
CHANGED
|
@@ -44,9 +44,9 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
44
44
|
var __syncRequire = typeof module === "object" && typeof module.exports === "object";
|
|
45
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
46
|
exports.loadSoundsPlugin = loadSoundsPlugin;
|
|
47
|
-
function loadSoundsPlugin(engine) {
|
|
48
|
-
engine.checkVersion("4.0.0-alpha.
|
|
49
|
-
engine.register(async (e) => {
|
|
47
|
+
async function loadSoundsPlugin(engine) {
|
|
48
|
+
engine.checkVersion("4.0.0-alpha.20");
|
|
49
|
+
await engine.register(async (e) => {
|
|
50
50
|
const { SoundsPlugin } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./SoundsPlugin.js"))) : new Promise((resolve_1, reject_1) => { require(["./SoundsPlugin.js"], resolve_1, reject_1); }).then(__importStar));
|
|
51
51
|
e.addPlugin(new SoundsPlugin(engine));
|
|
52
52
|
});
|
package/902.min.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/*! For license information please see 902.min.js.LICENSE.txt */
|
|
2
|
-
(this.webpackChunk_tsparticles_plugin_sounds=this.webpackChunk_tsparticles_plugin_sounds||[]).push([[902],{902(t,e,i){i.d(e,{SoundsPlugin:()=>b});var s,n,o=i(303);class a{constructor(){this.loop=!1,this.source=""}load(t){(0,o.isNull)(t)||((0,o.isObject)(t)?(void 0!==t.loop&&(this.loop=t.loop),void 0!==t.source&&(this.source=t.source)):this.source=t)}}class u{constructor(){this.duration=500,this.value=[]}load(t){(0,o.isNull)(t)||(void 0!==t.duration&&(this.duration=t.duration),void 0!==t.value&&(this.value=t.value))}}class l{constructor(){this.loop=!1,this.melodies=[],this.notes=[]}load(t){(0,o.isNull)(t)||(void 0!==t.loop&&(this.loop=t.loop),void 0!==t.melodies&&(this.melodies=t.melodies.map((t=>{const e=new l;return e.load(t),e}))),void 0!==t.notes&&(this.notes=t.notes.map((t=>{const e=new u;return e.load(t),e}))))}}class c{constructor(){this.event=[],this.notes=[]}load(t){if(!(0,o.isNull)(t)&&(void 0!==t.event&&(this.event=t.event),void 0!==t.audio&&((0,o.isArray)(t.audio)?this.audio=t.audio.map((t=>{const e=new a;return e.load(t),e})):(this.audio=new a,this.audio.load(t.audio))),void 0!==t.notes&&(this.notes=t.notes.map((t=>{const e=new u;return e.load(t),e}))),void 0!==t.melodies&&(this.melodies=t.melodies.map((t=>{const e=new l;return e.load(t),e}))),t.filter))if((0,o.isString)(t.filter)){const e=globalThis[t.filter];(0,o.isFunction)(e)&&(this.filter=e)}else this.filter=t.filter}}class r{constructor(){this.width=24,this.height=24,this.style=""}load(t){(0,o.isNull)(t)||(void 0!==t.path&&(this.path=t.path),void 0!==t.svg&&(this.svg=t.svg),void 0!==t.width&&(this.width=t.width),void 0!==t.height&&(this.height=t.height))}}class h{constructor(){this.mute=new r,this.unmute=new r,this.volumeDown=new r,this.volumeUp=new r,this.enable=!1,this.mute.svg='<?xml version="1.0"?>\n<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"\n xml:space="preserve" xmlns="http://www.w3.org/2000/svg"\n xmlns:xlink="http://www.w3.org/1999/xlink">\n <g id="Layer_1">\n <path fill="#fff" d="M19.707,5.293c-0.391-0.391-1.023-0.391-1.414,0l-1.551,1.551c-0.345-0.688-0.987-1.02-1.604-1.02c-0.449,0-0.905,0.152-1.356,0.453l-2.672,1.781C10.357,8.561,8.904,9,8,9c-1.654,0-3,1.346-3,3v2c0,1.237,0.754,2.302,1.826,2.76l-1.533,1.533c-0.391,0.391-0.391,1.023,0,1.414C5.488,19.902,5.744,20,6,20s0.512-0.098,0.707-0.293l2.527-2.527c0.697,0.174,1.416,0.455,1.875,0.762l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C16.035,20.176,17,19.495,17,18V9.414l2.707-2.707C20.098,6.316,20.098,5.684,19.707,5.293z M14.891,7.941c0.038-0.025,0.073-0.046,0.104-0.062C14.998,7.914,15,7.954,15,8v1.293l-2,2V9.202L14.891,7.941z M7,12c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146v2.439l-2.83,2.83C8.757,15.046,8.356,15,8,15c-0.552,0-1-0.448-1-1V12z M10.301,15.406L12,13.707v2.439C11.519,15.859,10.925,15.604,10.301,15.406z M14.994,18.12c-0.03-0.016-0.065-0.036-0.104-0.062L13,16.798v-4.091l2-2V18C15,18.046,14.998,18.086,14.994,18.12z"/>\n </g>\n</svg>',this.unmute.svg='<?xml version="1.0"?>\n<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"\n xml:space="preserve" xmlns="http://www.w3.org/2000/svg"\n xmlns:xlink="http://www.w3.org/1999/xlink">\n <g id="Layer_1">\n <path fill="#fff" d="M17.138,5.824c-0.449,0-0.905,0.152-1.356,0.453l-2.672,1.781C12.357,8.561,10.904,9,10,9c-1.654,0-3,1.346-3,3v2c0,1.654,1.346,3,3,3c0.904,0,2.357,0.439,3.109,0.941l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C18.035,20.176,19,19.495,19,18V8C19,6.505,18.035,5.824,17.138,5.824z M14,16.146C12.907,15.495,11.211,15,10,15c-0.552,0-1-0.448-1-1v-2c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146V16.146z M17,18c0,0.046-0.002,0.086-0.006,0.12c-0.03-0.016-0.065-0.036-0.104-0.062L15,16.798V9.202l1.891-1.261c0.038-0.025,0.073-0.046,0.104-0.062C16.998,7.914,17,7.954,17,8V18z"/>\n </g>\n</svg>',this.volumeDown.svg='<?xml version="1.0"?>\n<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"\n xml:space="preserve" xmlns="http://www.w3.org/2000/svg"\n xmlns:xlink="http://www.w3.org/1999/xlink">\n <g id="Layer_1">\n <path fill="#fff" d="M15.138,5.824c-0.449,0-0.905,0.152-1.356,0.453l-2.672,1.781C10.357,8.561,8.904,9,8,9c-1.654,0-3,1.346-3,3v2c0,1.654,1.346,3,3,3c0.904,0,2.357,0.439,3.109,0.941l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C16.035,20.176,17,19.495,17,18V8C17,6.505,16.035,5.824,15.138,5.824z M8,15c-0.552,0-1-0.448-1-1v-2c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146v6.293C10.907,15.495,9.211,15,8,15z M15,18c0,0.046-0.002,0.086-0.006,0.12c-0.03-0.016-0.065-0.036-0.104-0.062L13,16.798V9.202l1.891-1.261c0.038-0.025,0.073-0.046,0.104-0.062C14.998,7.914,15,7.954,15,8V18z"/>\n <path fill="#fff" d="M18.292,10.294c-0.39,0.391-0.39,1.023,0.002,1.414c0.345,0.345,0.535,0.803,0.535,1.291c0,0.489-0.19,0.948-0.536,1.294c-0.391,0.39-0.391,1.023,0,1.414C18.488,15.902,18.744,16,19,16s0.512-0.098,0.707-0.293c0.724-0.723,1.122-1.685,1.122-2.708s-0.398-1.984-1.123-2.707C19.317,9.903,18.683,9.901,18.292,10.294z"/>\n </g>\n</svg>',this.volumeUp.svg='<?xml version="1.0"?>\n<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"\n xml:space="preserve" xmlns="http://www.w3.org/2000/svg"\n xmlns:xlink="http://www.w3.org/1999/xlink">\n <g id="Layer_1">\n <path fill="#fff" d="M16.706,10.292c-0.389-0.389-1.023-0.391-1.414,0.002c-0.39,0.391-0.39,1.023,0.002,1.414c0.345,0.345,0.535,0.803,0.535,1.291c0,0.489-0.19,0.948-0.536,1.294c-0.391,0.39-0.391,1.023,0,1.414C15.488,15.902,15.744,16,16,16s0.512-0.098,0.707-0.293c0.724-0.723,1.122-1.685,1.122-2.708S17.431,11.015,16.706,10.292z"/>\n <path fill="#fff" d="M18.706,8.292c-0.391-0.389-1.023-0.39-1.414,0.002c-0.39,0.391-0.39,1.024,0.002,1.414c0.879,0.877,1.363,2.044,1.364,3.287c0.001,1.246-0.484,2.417-1.365,3.298c-0.391,0.391-0.391,1.023,0,1.414C17.488,17.902,17.744,18,18,18s0.512-0.098,0.707-0.293c1.259-1.259,1.952-2.933,1.951-4.713C20.657,11.217,19.964,9.547,18.706,8.292z"/>\n <path fill="#fff" d="M20.706,6.292c-0.391-0.389-1.023-0.39-1.414,0.002c-0.39,0.391-0.39,1.024,0.002,1.414c1.412,1.409,2.191,3.285,2.192,5.284c0.002,2.002-0.777,3.885-2.193,5.301c-0.391,0.391-0.391,1.023,0,1.414C19.488,19.902,19.744,20,20,20s0.512-0.098,0.707-0.293c1.794-1.794,2.781-4.18,2.779-6.717C23.485,10.457,22.497,8.078,20.706,6.292z"/>\n <path fill="#fff" d="M12.138,5.824c-0.449,0-0.905,0.152-1.356,0.453L8.109,8.059C7.357,8.561,5.904,9,5,9c-1.654,0-3,1.346-3,3v2c0,1.654,1.346,3,3,3c0.904,0,2.357,0.439,3.109,0.941l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C13.035,20.176,14,19.495,14,18V8C14,6.505,13.035,5.824,12.138,5.824z M5,15c-0.552,0-1-0.448-1-1v-2c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146v6.293C7.907,15.495,6.211,15,5,15z M12,18c0,0.046-0.002,0.086-0.006,0.12c-0.03-0.016-0.065-0.036-0.104-0.062L10,16.798V9.202l1.891-1.261c0.038-0.025,0.073-0.046,0.104-0.062C11.998,7.914,12,7.954,12,8V18z"/>\n </g>\n</svg>'}load(t){(0,o.isNull)(t)||(void 0!==t.enable&&(this.enable=t.enable),this.mute.load(t.mute),this.unmute.load(t.unmute),this.volumeDown.load(t.volumeDown),this.volumeUp.load(t.volumeUp))}}class d{constructor(){this.value=100,this.max=100,this.min=0,this.step=10}load(t){(0,o.isNull)(t)||((0,o.isObject)(t)?(void 0!==t.max&&(this.max=t.max),void 0!==t.min&&(this.min=t.min),void 0!==t.step&&(this.step=t.step),void 0!==t.value&&(this.value=t.value)):this.value=t)}}class m{constructor(){this.autoPlay=!0,this.enable=!1,this.events=[],this.icons=new h,this.volume=new d}load(t){(0,o.isNull)(t)||(void 0!==t.autoPlay&&(this.autoPlay=t.autoPlay),void 0!==t.enable&&(this.enable=t.enable),void 0!==t.events&&(this.events=t.events.map((t=>{const e=new c;return e.load(t),e}))),this.icons.load(t.icons),void 0!==t.volume&&this.volume.load(t.volume))}}!function(t){t.mute="soundsMuted",t.unmute="soundsUnmuted"}(s||(s={})),function(t){t.Block="block",t.None="none"}(n||(n={}));const v=new Map;function p(t){const e=/(([A-G]b?)(\d))|pause/i.exec(t);if(!e?.length)return;const i=e[2]??e[0],s=v.get(i);return s?s[parseInt(e[3]??"0")]:void 0}v.set("C",[16.35,32.7,65.41,130.81,261.63,523.25,1046.5,2093,4186.01]),v.set("Db",[17.32,34.65,69.3,138.59,277.18,554.37,1108.73,2217.46,4434.92]),v.set("D",[18.35,36.71,73.42,146.83,293.66,587.33,1174.66,2349.32,4698.63]),v.set("Eb",[19.45,38.89,77.78,155.56,311.13,622.25,1244.51,2489.02,4978.03]),v.set("E",[20.6,41.2,82.41,164.81,329.63,659.25,1318.51,2637.02,5274.04]),v.set("F",[21.83,43.65,87.31,174.61,349.23,698.46,1396.91,2793.83,5587.65]),v.set("Gb",[23.12,46.25,92.5,185,369.99,739.99,1479.98,2959.96,5919.91]),v.set("G",[24.5,49,98,196,392,783.99,1567.98,3135.96,6271.93]),v.set("Ab",[25.96,51.91,103.83,207.65,415.3,830.61,1661.22,3322.44,6644.88]),v.set("A",[27.5,55,110,220,440,880,1760,3520,7040]),v.set("Bb",[29.14,58.27,116.54,233.08,466.16,932.33,1864.66,3729.31,7458.62]),v.set("B",[30.87,61.74,123.47,246.94,493.88,987.77,1975.53,3951.07,7902.13]),v.set("pause",[0]);let g=!0;const f=()=>g,_=()=>{g=!1};function w(t){const e=(0,o.safeDocument)().createElement("img"),{clickCb:i,container:s,display:n,iconOptions:a,margin:u,options:l,pos:c,rightOffsets:r}=t,{width:h,path:d,style:m,svg:v}=a;!function(t,e,i,s,n,o,a,u){t.style.userSelect="none",t.style.position="absolute",t.style.top=`${(e+a).toString()}px`,t.style.left=`${(i-a-o).toString()}px`,t.style.display=s,t.style.zIndex=(n+1).toString(),t.style.cssText+=u}(e,c.top+u,c.right-(u*(r.length+1)+h+r.reduce(((t,e)=>t+e),0)),n,l.fullScreen.zIndex+1,h,u,m),e.src=d??(v?`data:image/svg+xml;base64,${btoa(v)}`:"");return(s.canvas.element?.parentNode??(0,o.safeDocument)().body).append(e),e.addEventListener("click",(()=>{i()})),e}function y(t){t&&t.remove()}class x{constructor(t,e){this._addBuffer=t=>{const e=t.createBufferSource();return this._audioSources.push(e),e},this._addOscillator=t=>{const e=t.createOscillator();return this._audioSources.push(e),e},this._initEvents=()=>{const t=this._container,e=t.actualOptions.sounds;if(e?.enable&&t.canvas.element)for(const t of e.events){const e=i=>{(async()=>{const s=t.filter&&!t.filter(i);if(this._container!==i.container)return;if(this._container.muted||this._container.destroyed)return void(0,o.executeOnSingleOrMultiple)(t.event,(t=>{this._engine.removeEventListener(t,e)}));if(s)return;if(t.audio){const e=(0,o.itemFromSingleOrMultiple)(t.audio);if(!e)return;this._playBuffer(e)}else if(t.melodies){const e=(0,o.itemFromArray)(t.melodies);if(!e)return;e.melodies.length?await Promise.allSettled(e.melodies.map((t=>this._playNote(t.notes,0,e.loop)))):await this._playNote(e.notes,0,e.loop)}else if(t.notes){const e=(0,o.itemFromArray)(t.notes);if(!e)return;await this._playNote([e],0,!1)}})()};(0,o.executeOnSingleOrMultiple)(t.event,(t=>{this._engine.addEventListener(t,e)}))}},this._mute=async()=>{const t=this._container,e=this._getAudioContext();for(const t of this._audioSources)this._removeAudioSource(t);this._gain&&this._gain.disconnect(),await e.close(),t.audioContext=void 0,this._engine.dispatchEvent(s.mute,{container:this._container})},this._playBuffer=t=>{const e=this._audioMap.get(t.source);if(!e)return;const i=this._container.audioContext;if(!i)return;const s=this._addBuffer(i);s.loop=t.loop,s.buffer=e,s.connect(this._gain??i.destination),s.start()},this._playFrequency=async(t,e)=>{if(!this._gain||this._container.muted)return;const i=this._getAudioContext(),s=this._addOscillator(i);return s.connect(this._gain),s.type="sine",s.frequency.value=t,s.start(),new Promise((t=>{setTimeout((()=>{this._removeAudioSource(s),t()}),e)}))},this._playMuteSound=()=>{if(this._container.muted)return;const t=this._getAudioContext(),e=t.createGain();e.connect(t.destination),e.gain.value=0;const i=t.createOscillator();i.connect(e),i.type="sine",i.frequency.value=1,i.start(),setTimeout((()=>{i.stop(),i.disconnect(),e.disconnect()}))},this._playNote=async(t,e,i)=>{if(this._container.muted)return;const s=t[e];if(!s)return;const n=s.value,a=(0,o.executeOnSingleOrMultiple)(n,(async(i,s)=>this._playNoteValue(t,e,s)));await((0,o.isArray)(a)?Promise.allSettled(a):a);let u=e+1;i&&u>=t.length&&(u%=t.length),await this._playNote(t,u,i)},this._playNoteValue=async(t,e,i)=>{const s=t[e];if(!s)return;const n=(0,o.itemFromSingleOrMultiple)(s.value,i,!0);if(n)try{const t=p(n);if(!(0,o.isNumber)(t))return;await this._playFrequency(t,s.duration)}catch(t){(0,o.getLogger)().error(t)}},this._removeAudioSource=t=>{t.stop(),t.disconnect();this._audioSources.splice(this._audioSources.indexOf(t),1)},this._unmute=()=>{const t=this._container.actualOptions.sounds;if(!t)return;const e=this._getAudioContext(),i=e.createGain();i.connect(e.destination),i.gain.value=t.volume.value/o.percentDenominator,this._gain=i,this._initEvents(),this._engine.dispatchEvent(s.unmute,{container:this._container})},this._updateMuteIcons=()=>{const t=this._container,e=t.actualOptions.sounds;if(!e?.enable||!e.icons.enable)return;const i=this._muteImg,s=this._unmuteImg;i&&(i.style.display=t.muted?"block":"none"),s&&(s.style.display=t.muted?"none":"block")},this._updateMuteStatus=async()=>{const t=this._container,e=this._getAudioContext();t.muted?(await e.suspend(),await this._mute()):(await e.resume(),this._unmute(),this._playMuteSound())},this._updateVolume=async()=>{const t=this._container,e=t.actualOptions.sounds;if(!e?.enable)return;(0,o.clamp)(this._volume,e.volume.min,e.volume.max);let i=!1;this._volume<=0&&!t.muted?(this._volume=0,t.muted=!0,i=!0):this._volume>0&&t.muted&&(t.muted=!1,i=!0),i&&(this._updateMuteIcons(),await this._updateMuteStatus()),this._gain?.gain&&(this._gain.gain.value=this._volume/o.percentDenominator)},this._container=t,this._engine=e,this._volume=0,this._audioSources=[],this._audioMap=new Map}async init(){const t=this._container.actualOptions.sounds;if(!t?.enable)return;if(t.autoPlay&&f()){const t=()=>{removeEventListener(o.mouseDownEvent,t),removeEventListener(o.touchStartEvent,t),_(),this.unmute()},e={capture:!0,once:!0};addEventListener(o.mouseDownEvent,t,e),addEventListener(o.touchStartEvent,t,e)}this._volume=t.volume.value;const e=t.events;this._audioMap=new Map;for(const t of e){if(!t.audio)continue;const e=(0,o.executeOnSingleOrMultiple)(t.audio,(async t=>{const e=await fetch(t.source);if(!e.ok)return;const i=await e.arrayBuffer(),s=this._getAudioContext(),n=await s.decodeAudioData(i);this._audioMap.set(t.source,n)}));e instanceof Promise?await e:await Promise.allSettled(e)}}async mute(){this._container.muted||await this.toggleMute()}async start(){const t=this._container,e=t.actualOptions,i=e.sounds;if(!i?.enable||!t.canvas.element)return;t.muted=!0;const s=t.canvas.element,o={top:s.offsetTop,right:s.offsetLeft+s.offsetWidth},{mute:a,unmute:u,volumeDown:l,volumeUp:c}=i.icons,r=async()=>{await this.toggleMute()},h=i.icons.enable?n.Block:n.None;this._muteImg=w({container:t,options:e,pos:o,display:h,iconOptions:a,margin:10,rightOffsets:[l.width,c.width],clickCb:r}),this._unmuteImg=w({container:t,options:e,pos:o,display:n.None,iconOptions:u,margin:10,rightOffsets:[l.width,c.width],clickCb:r}),this._volumeDownImg=w({container:t,options:e,pos:o,display:h,iconOptions:l,margin:10,rightOffsets:[c.width],clickCb:async()=>{await this.volumeDown()}}),this._volumeUpImg=w({container:t,options:e,pos:o,display:h,iconOptions:c,margin:10,rightOffsets:[],clickCb:async()=>{await this.volumeUp()}}),!f()&&i.autoPlay&&await this.unmute()}stop(){this._container.muted=!0,(async()=>{await this._mute(),y(this._muteImg),y(this._unmuteImg),y(this._volumeDownImg),y(this._volumeUpImg)})()}async toggleMute(){const t=this._container;t.muted=!t.muted,this._updateMuteIcons(),await this._updateMuteStatus()}async unmute(){this._container.muted&&await this.toggleMute()}async volumeDown(){const t=this._container,e=t.actualOptions.sounds;e?.enable&&(t.muted&&(this._volume=0),this._volume-=e.volume.step,await this._updateVolume())}async volumeUp(){const t=this._container.actualOptions.sounds;t?.enable&&(this._volume+=t.volume.step,await this._updateVolume())}_getAudioContext(){const t=this._container;return t.audioContext??=new AudioContext,t.audioContext}}const C=()=>{removeEventListener(o.mouseDownEvent,C),removeEventListener(o.touchStartEvent,C),_()};class b{constructor(t){this.id="sounds",this._engine=t;const e={capture:!0,once:!0};addEventListener(o.mouseDownEvent,C,e),addEventListener(o.touchStartEvent,C,e)}getPlugin(t){return Promise.resolve(new x(t,this._engine))}loadOptions(t,e){if(!this.needsPlugin(t)&&!this.needsPlugin(e))return;let i=t.sounds;void 0===i?.load&&(t.sounds=i=new m),i.load(e?.sounds)}needsPlugin(t){return t?.sounds?.enable??!1}}}}]);
|
package/902.min.js.LICENSE.txt
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/*! tsParticles Sounds Plugin v4.0.0-alpha.2 by Matteo Bruni */
|