@tsparticles/plugin-sounds 3.0.0-alpha.0 → 3.0.0-beta.0

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.
Files changed (37) hide show
  1. package/README.md +15 -11
  2. package/browser/Options/Classes/SoundsAudio.js +2 -1
  3. package/browser/Options/Classes/SoundsEvent.js +4 -3
  4. package/browser/Options/Classes/SoundsVolume.js +2 -1
  5. package/browser/SoundsInstance.js +300 -279
  6. package/browser/index.js +6 -8
  7. package/browser/utils.js +0 -1
  8. package/cjs/Options/Classes/SoundsAudio.js +2 -1
  9. package/cjs/Options/Classes/SoundsEvent.js +4 -3
  10. package/cjs/Options/Classes/SoundsVolume.js +2 -1
  11. package/cjs/SoundsInstance.js +283 -281
  12. package/cjs/index.js +6 -19
  13. package/cjs/utils.js +0 -1
  14. package/esm/Options/Classes/SoundsAudio.js +2 -1
  15. package/esm/Options/Classes/SoundsEvent.js +4 -3
  16. package/esm/Options/Classes/SoundsVolume.js +2 -1
  17. package/esm/SoundsInstance.js +300 -279
  18. package/esm/index.js +6 -8
  19. package/esm/utils.js +0 -1
  20. package/package.json +6 -5
  21. package/report.html +4 -4
  22. package/tsparticles.plugin.sounds.js +343 -305
  23. package/tsparticles.plugin.sounds.min.js +1 -1
  24. package/tsparticles.plugin.sounds.min.js.LICENSE.txt +1 -8
  25. package/types/Options/Classes/SoundsAudio.d.ts +1 -1
  26. package/types/Options/Classes/SoundsEvent.d.ts +1 -1
  27. package/types/Options/Classes/SoundsVolume.d.ts +1 -1
  28. package/types/SoundsInstance.d.ts +15 -15
  29. package/types/enums.d.ts +4 -0
  30. package/types/index.d.ts +1 -1
  31. package/types/types.d.ts +16 -0
  32. package/umd/Options/Classes/SoundsAudio.js +3 -2
  33. package/umd/Options/Classes/SoundsEvent.js +5 -4
  34. package/umd/Options/Classes/SoundsVolume.js +3 -2
  35. package/umd/SoundsInstance.js +301 -280
  36. package/umd/index.js +6 -8
  37. package/umd/utils.js +0 -1
@@ -1,6 +1,20 @@
1
- import { clamp, itemFromArray, itemFromSingleOrMultiple } from "@tsparticles/engine";
2
- import { executeOnSingleOrMultiple } from "@tsparticles/engine";
1
+ import { clamp, executeOnSingleOrMultiple, getLogger, isArray, isNumber, itemFromArray, itemFromSingleOrMultiple, } from "@tsparticles/engine";
3
2
  import { getNoteFrequency } from "./utils";
3
+ function initImage(data) {
4
+ const img = document.createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, svg } = iconOptions;
5
+ setIconStyle(img, pos.top + margin, pos.right - (margin * (rightOffsets.length + 1) + width + rightOffsets.reduce((a, b) => a + b, 0)), display, options.fullScreen.zIndex + 1, width, margin);
6
+ img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
7
+ const parent = container.canvas.element?.parentNode || document.body;
8
+ parent.append(img);
9
+ img.addEventListener("click", clickCb);
10
+ return img;
11
+ }
12
+ function removeImage(image) {
13
+ if (!image) {
14
+ return;
15
+ }
16
+ image.remove();
17
+ }
4
18
  function setIconStyle(icon, top, left, display, zIndex, width, margin) {
5
19
  icon.style.userSelect = "none";
6
20
  icon.style.webkitUserSelect = "none";
@@ -12,6 +26,228 @@ function setIconStyle(icon, top, left, display, zIndex, width, margin) {
12
26
  }
13
27
  export class SoundsInstance {
14
28
  constructor(container, engine) {
29
+ this._addBuffer = (audioCtx) => {
30
+ const buffer = audioCtx.createBufferSource();
31
+ this._audioSources.push(buffer);
32
+ return buffer;
33
+ };
34
+ this._addOscillator = (audioCtx) => {
35
+ const oscillator = audioCtx.createOscillator();
36
+ this._audioSources.push(oscillator);
37
+ return oscillator;
38
+ };
39
+ this._initEvents = () => {
40
+ const container = this._container, soundsOptions = container.actualOptions.sounds;
41
+ if (!soundsOptions?.enable || !container.canvas.element) {
42
+ return;
43
+ }
44
+ for (const event of soundsOptions.events) {
45
+ const cb = async (args) => {
46
+ if (this._container !== args.container) {
47
+ return;
48
+ }
49
+ if (!this._container || this._container.muted || this._container.destroyed) {
50
+ executeOnSingleOrMultiple(event.event, (item) => {
51
+ this._engine.removeEventListener(item, cb);
52
+ });
53
+ return;
54
+ }
55
+ if (event.filter && !event.filter(args)) {
56
+ return;
57
+ }
58
+ if (event.audio) {
59
+ this._playBuffer(itemFromSingleOrMultiple(event.audio));
60
+ }
61
+ else if (event.melodies) {
62
+ const melody = itemFromArray(event.melodies);
63
+ if (melody.melodies.length) {
64
+ await Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes, 0, melody.loop)));
65
+ }
66
+ else {
67
+ await this._playNote(melody.notes, 0, melody.loop);
68
+ }
69
+ }
70
+ else if (event.notes) {
71
+ const note = itemFromArray(event.notes);
72
+ await this._playNote([note], 0, false);
73
+ }
74
+ };
75
+ executeOnSingleOrMultiple(event.event, (item) => {
76
+ this._engine.addEventListener(item, cb);
77
+ });
78
+ }
79
+ };
80
+ this._mute = () => {
81
+ const container = this._container;
82
+ if (!container.audioContext) {
83
+ return;
84
+ }
85
+ for (const source of this._audioSources) {
86
+ this._removeAudioSource(source);
87
+ }
88
+ if (this._gain) {
89
+ this._gain.disconnect();
90
+ }
91
+ container.audioContext.close();
92
+ container.audioContext = undefined;
93
+ this._engine.dispatchEvent("soundsMuted", { container: this._container });
94
+ };
95
+ this._playBuffer = (audio) => {
96
+ const audioBuffer = this._audioMap.get(audio.source);
97
+ if (!audioBuffer) {
98
+ return;
99
+ }
100
+ const audioCtx = this._container.audioContext;
101
+ if (!audioCtx) {
102
+ return;
103
+ }
104
+ const source = this._addBuffer(audioCtx);
105
+ source.loop = audio.loop;
106
+ source.buffer = audioBuffer;
107
+ source.connect(this._gain ?? audioCtx.destination);
108
+ source.start();
109
+ };
110
+ this._playFrequency = async (frequency, duration) => {
111
+ if (!this._container.audioContext || !this._gain) {
112
+ return;
113
+ }
114
+ const oscillator = this._addOscillator(this._container.audioContext);
115
+ oscillator.connect(this._gain);
116
+ oscillator.type = "sine";
117
+ oscillator.frequency.value = frequency;
118
+ oscillator.start();
119
+ return new Promise((resolve) => {
120
+ setTimeout(() => {
121
+ this._removeAudioSource(oscillator);
122
+ resolve();
123
+ }, duration);
124
+ });
125
+ };
126
+ this._playMuteSound = () => {
127
+ const container = this._container;
128
+ if (!container.audioContext) {
129
+ return;
130
+ }
131
+ const gain = container.audioContext.createGain();
132
+ gain.connect(container.audioContext.destination);
133
+ gain.gain.value = 0;
134
+ const oscillator = container.audioContext.createOscillator();
135
+ oscillator.connect(gain);
136
+ oscillator.type = "sine";
137
+ oscillator.frequency.value = 1;
138
+ oscillator.start();
139
+ setTimeout(() => {
140
+ oscillator.stop();
141
+ oscillator.disconnect();
142
+ gain.disconnect();
143
+ });
144
+ };
145
+ this._playNote = async (notes, noteIdx, loop) => {
146
+ if (this._container.muted) {
147
+ return;
148
+ }
149
+ const note = notes[noteIdx];
150
+ if (!note) {
151
+ return;
152
+ }
153
+ const value = note.value;
154
+ const promises = executeOnSingleOrMultiple(value, async (_, idx) => {
155
+ return this._playNoteValue(notes, noteIdx, idx);
156
+ });
157
+ await (isArray(promises) ? Promise.allSettled(promises) : promises);
158
+ let nextNoteIdx = noteIdx + 1;
159
+ if (loop && nextNoteIdx >= notes.length) {
160
+ nextNoteIdx = nextNoteIdx % notes.length;
161
+ }
162
+ if (this._container.muted) {
163
+ return;
164
+ }
165
+ await this._playNote(notes, nextNoteIdx, loop);
166
+ };
167
+ this._playNoteValue = async (notes, noteIdx, valueIdx) => {
168
+ const note = notes[noteIdx];
169
+ if (!note) {
170
+ return;
171
+ }
172
+ const value = itemFromSingleOrMultiple(note.value, valueIdx, true);
173
+ try {
174
+ const freq = getNoteFrequency(value);
175
+ if (!isNumber(freq)) {
176
+ return;
177
+ }
178
+ await this._playFrequency(freq, note.duration);
179
+ }
180
+ catch (e) {
181
+ getLogger().error(e);
182
+ }
183
+ };
184
+ this._removeAudioSource = (source) => {
185
+ source.stop();
186
+ source.disconnect();
187
+ this._audioSources.splice(this._audioSources.indexOf(source), 1);
188
+ };
189
+ this._unmute = () => {
190
+ const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
191
+ if (!soundsOptions) {
192
+ return;
193
+ }
194
+ if (!container.audioContext) {
195
+ container.audioContext = new AudioContext();
196
+ }
197
+ const { audioContext } = container;
198
+ if (!this._audioSources) {
199
+ this._audioSources = [];
200
+ }
201
+ const gain = audioContext.createGain();
202
+ gain.connect(audioContext.destination);
203
+ gain.gain.value = soundsOptions.volume.value / 100;
204
+ this._gain = gain;
205
+ this._initEvents();
206
+ this._engine.dispatchEvent("soundsUnmuted", { container: this._container });
207
+ };
208
+ this._updateMuteIcons = () => {
209
+ const container = this._container, muteImg = this._muteImg, unmuteImg = this._unmuteImg;
210
+ if (muteImg) {
211
+ muteImg.style.display = container.muted ? "block" : "none";
212
+ }
213
+ if (unmuteImg) {
214
+ unmuteImg.style.display = container.muted ? "none" : "block";
215
+ }
216
+ };
217
+ this._updateMuteStatus = () => {
218
+ const container = this._container;
219
+ if (container.muted) {
220
+ this._mute();
221
+ }
222
+ else {
223
+ this._unmute();
224
+ this._playMuteSound();
225
+ }
226
+ };
227
+ this._updateVolume = () => {
228
+ const container = this._container, soundsOptions = container.actualOptions.sounds;
229
+ if (!soundsOptions?.enable) {
230
+ return;
231
+ }
232
+ clamp(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
233
+ let stateChanged = false;
234
+ if (this._volume <= 0 && !container.muted) {
235
+ this._volume = 0;
236
+ container.muted = true;
237
+ stateChanged = true;
238
+ }
239
+ else if (this._volume > 0 && container.muted) {
240
+ container.muted = false;
241
+ stateChanged = true;
242
+ }
243
+ if (stateChanged) {
244
+ this._updateMuteIcons();
245
+ this._updateMuteStatus();
246
+ }
247
+ if (this._gain?.gain) {
248
+ this._gain.gain.value = this._volume / 100;
249
+ }
250
+ };
15
251
  this._container = container;
16
252
  this._engine = engine;
17
253
  this._volume = 0;
@@ -20,7 +256,7 @@ export class SoundsInstance {
20
256
  }
21
257
  async init() {
22
258
  const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
23
- if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable)) {
259
+ if (!soundsOptions?.enable) {
24
260
  return;
25
261
  }
26
262
  this._volume = soundsOptions.volume.value;
@@ -43,294 +279,79 @@ export class SoundsInstance {
43
279
  }
44
280
  }
45
281
  async start() {
46
- var _a, _b, _c, _d;
47
282
  const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
48
- if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable) || !container.canvas.element) {
283
+ if (!soundsOptions?.enable || !container.canvas.element) {
49
284
  return;
50
285
  }
51
286
  container.muted = true;
52
- this._muteImg = document.createElement("img");
53
- this._unmuteImg = document.createElement("img");
54
- this._volumeDownImg = document.createElement("img");
55
- this._volumeUpImg = document.createElement("img");
56
- const muteImg = this._muteImg, unmuteImg = this._unmuteImg, volumeDownImg = this._volumeDownImg, volumeUpImg = this._volumeUpImg, containerTop = container.canvas.element.offsetTop, containerRight = container.canvas.element.offsetLeft + container.canvas.element.offsetWidth, iconsOptions = soundsOptions.icons, muteOptions = iconsOptions.mute, unmuteOptions = iconsOptions.unmute, volumeDownOptions = iconsOptions.volumeDown, volumeUpOptions = iconsOptions.volumeUp, margin = 10;
57
- setIconStyle(muteImg, containerTop + margin, containerRight - margin * 3 - muteOptions.width - volumeDownOptions.width - volumeUpOptions.width, "block", options.fullScreen.zIndex + 1, muteOptions.width, margin);
58
- setIconStyle(unmuteImg, containerTop + margin, containerRight - margin * 3 - unmuteOptions.width - volumeDownOptions.width - volumeUpOptions.width, "none", options.fullScreen.zIndex + 1, unmuteOptions.width, margin);
59
- setIconStyle(volumeDownImg, containerTop + margin, containerRight - margin * 2 - volumeDownOptions.width - volumeUpOptions.width, "block", options.fullScreen.zIndex + 1, volumeDownOptions.width, margin);
60
- setIconStyle(volumeUpImg, containerTop + margin, containerRight - margin - volumeUpOptions.width, "block", options.fullScreen.zIndex + 1, volumeUpOptions.width, margin);
61
- muteImg.src = (_a = muteOptions.path) !== null && _a !== void 0 ? _a : (muteOptions.svg ? `data:image/svg+xml;base64,${btoa(muteOptions.svg)}` : "");
62
- unmuteImg.src =
63
- (_b = unmuteOptions.path) !== null && _b !== void 0 ? _b : (unmuteOptions.svg ? `data:image/svg+xml;base64,${btoa(unmuteOptions.svg)}` : "");
64
- volumeDownImg.src =
65
- (_c = volumeDownOptions.path) !== null && _c !== void 0 ? _c : (volumeDownOptions.svg ? `data:image/svg+xml;base64,${btoa(volumeDownOptions.svg)}` : "");
66
- volumeUpImg.src =
67
- (_d = volumeUpOptions.path) !== null && _d !== void 0 ? _d : (volumeUpOptions.svg ? `data:image/svg+xml;base64,${btoa(volumeUpOptions.svg)}` : "");
68
- const parent = container.canvas.element.parentNode || document.body;
69
- parent.append(muteImg);
70
- parent.append(unmuteImg);
71
- parent.append(volumeDownImg);
72
- parent.append(volumeUpImg);
287
+ const canvas = container.canvas.element, pos = {
288
+ top: canvas.offsetTop,
289
+ right: canvas.offsetLeft + canvas.offsetWidth,
290
+ }, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10;
73
291
  const toggleMute = () => {
74
292
  container.muted = !container.muted;
75
293
  this._updateMuteIcons();
76
294
  this._updateMuteStatus();
77
295
  };
78
- const volumeDown = () => {
79
- if (container.muted) {
80
- this._volume = 0;
81
- }
82
- this._volume -= soundsOptions.volume.step;
83
- this._updateVolume();
84
- };
85
- const volumeUp = () => {
86
- if (container.muted) {
87
- this._volume = 0;
88
- }
89
- this._volume += soundsOptions.volume.step;
90
- this._updateVolume();
91
- };
92
- muteImg.addEventListener("click", toggleMute);
93
- unmuteImg.addEventListener("click", toggleMute);
94
- volumeDownImg.addEventListener("click", volumeDown);
95
- volumeUpImg.addEventListener("click", volumeUp);
96
- }
97
- stop() {
98
- this._container.muted = true;
99
- this._mute();
100
- if (this._muteImg) {
101
- this._muteImg.remove();
102
- }
103
- if (this._unmuteImg) {
104
- this._unmuteImg.remove();
105
- }
106
- if (this._volumeDownImg) {
107
- this._volumeDownImg.remove();
108
- }
109
- if (this._volumeUpImg) {
110
- this._volumeUpImg.remove();
111
- }
112
- }
113
- _addBuffer(audioCtx) {
114
- const buffer = audioCtx.createBufferSource();
115
- this._audioSources.push(buffer);
116
- return buffer;
117
- }
118
- _addOscillator(audioCtx) {
119
- const oscillator = audioCtx.createOscillator();
120
- this._audioSources.push(oscillator);
121
- return oscillator;
122
- }
123
- _initEvents() {
124
- const container = this._container, soundsOptions = container.actualOptions.sounds;
125
- if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable) || !container.canvas.element) {
126
- return;
127
- }
128
- for (const event of soundsOptions.events) {
129
- const cb = async (args) => {
130
- if (this._container !== args.container) {
131
- return;
132
- }
133
- if (!this._container || this._container.muted || this._container.destroyed) {
134
- executeOnSingleOrMultiple(event.event, (item) => {
135
- this._engine.removeEventListener(item, cb);
136
- });
137
- return;
138
- }
139
- if (event.filter && !event.filter(args)) {
140
- return;
141
- }
142
- if (event.audio) {
143
- this._playBuffer(itemFromSingleOrMultiple(event.audio));
144
- }
145
- else if (event.melodies) {
146
- const melody = itemFromArray(event.melodies);
147
- if (melody.melodies.length) {
148
- await Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes, 0, melody.loop)));
149
- }
150
- else {
151
- await this._playNote(melody.notes, 0, melody.loop);
152
- }
153
- }
154
- else if (event.notes) {
155
- const note = itemFromArray(event.notes);
156
- await this._playNote([note], 0, false);
157
- }
158
- };
159
- executeOnSingleOrMultiple(event.event, (item) => {
160
- this._engine.addEventListener(item, cb);
161
- });
162
- }
163
- }
164
- _mute() {
165
- const container = this._container;
166
- if (!container.audioContext) {
167
- return;
168
- }
169
- for (const source of this._audioSources) {
170
- this._removeAudioSource(source);
171
- }
172
- if (this._gain) {
173
- this._gain.disconnect();
174
- }
175
- container.audioContext.close();
176
- container.audioContext = undefined;
177
- this._engine.dispatchEvent("soundsMuted", { container: this._container });
178
- }
179
- _playBuffer(audio) {
180
- var _a;
181
- const audioBuffer = this._audioMap.get(audio.source);
182
- if (!audioBuffer) {
183
- return;
184
- }
185
- const audioCtx = this._container.audioContext;
186
- if (!audioCtx) {
187
- return;
188
- }
189
- const source = this._addBuffer(audioCtx);
190
- source.loop = audio.loop;
191
- source.buffer = audioBuffer;
192
- source.connect((_a = this._gain) !== null && _a !== void 0 ? _a : audioCtx.destination);
193
- source.start();
194
- }
195
- async _playFrequency(frequency, duration) {
196
- if (!this._container.audioContext || !this._gain) {
197
- return;
198
- }
199
- const oscillator = this._addOscillator(this._container.audioContext);
200
- oscillator.connect(this._gain);
201
- oscillator.type = "sine";
202
- oscillator.frequency.value = frequency;
203
- oscillator.start();
204
- return new Promise((resolve) => {
205
- setTimeout(() => {
206
- this._removeAudioSource(oscillator);
207
- resolve();
208
- }, duration);
296
+ this._muteImg = initImage({
297
+ container,
298
+ options,
299
+ pos,
300
+ display: "block",
301
+ iconOptions: mute,
302
+ margin,
303
+ rightOffsets: [volumeDown.width, volumeUp.width],
304
+ clickCb: toggleMute,
209
305
  });
210
- }
211
- _playMuteSound() {
212
- const container = this._container;
213
- if (!container.audioContext) {
214
- return;
215
- }
216
- const gain = container.audioContext.createGain();
217
- gain.connect(container.audioContext.destination);
218
- gain.gain.value = 0;
219
- const oscillator = container.audioContext.createOscillator();
220
- oscillator.connect(gain);
221
- oscillator.type = "sine";
222
- oscillator.frequency.value = 1;
223
- oscillator.start();
224
- setTimeout(() => {
225
- oscillator.stop();
226
- oscillator.disconnect();
227
- gain.disconnect();
306
+ this._unmuteImg = initImage({
307
+ container,
308
+ options,
309
+ pos,
310
+ display: "none",
311
+ iconOptions: unmute,
312
+ margin,
313
+ rightOffsets: [volumeDown.width, volumeUp.width],
314
+ clickCb: toggleMute,
228
315
  });
229
- }
230
- async _playNote(notes, noteIdx, loop) {
231
- if (this._container.muted) {
232
- return;
233
- }
234
- const note = notes[noteIdx];
235
- if (!note) {
236
- return;
237
- }
238
- const value = note.value;
239
- const promises = executeOnSingleOrMultiple(value, async (_, idx) => {
240
- return this._playNoteValue(notes, noteIdx, idx);
316
+ this._volumeDownImg = initImage({
317
+ container,
318
+ options,
319
+ pos,
320
+ display: "block",
321
+ iconOptions: volumeDown,
322
+ margin,
323
+ rightOffsets: [volumeUp.width],
324
+ clickCb: () => {
325
+ if (container.muted) {
326
+ this._volume = 0;
327
+ }
328
+ this._volume -= soundsOptions.volume.step;
329
+ this._updateVolume();
330
+ },
331
+ });
332
+ this._volumeUpImg = initImage({
333
+ container,
334
+ options,
335
+ pos,
336
+ display: "block",
337
+ iconOptions: volumeUp,
338
+ margin,
339
+ rightOffsets: [],
340
+ clickCb: () => {
341
+ if (container.muted) {
342
+ this._volume = 0;
343
+ }
344
+ this._volume += soundsOptions.volume.step;
345
+ this._updateVolume();
346
+ },
241
347
  });
242
- await (promises instanceof Array ? Promise.allSettled(promises) : promises);
243
- let nextNoteIdx = noteIdx + 1;
244
- if (loop && nextNoteIdx >= notes.length) {
245
- nextNoteIdx = nextNoteIdx % notes.length;
246
- }
247
- if (this._container.muted) {
248
- return;
249
- }
250
- await this._playNote(notes, nextNoteIdx, loop);
251
- }
252
- async _playNoteValue(notes, noteIdx, valueIdx) {
253
- const note = notes[noteIdx];
254
- if (!note) {
255
- return;
256
- }
257
- const value = itemFromSingleOrMultiple(note.value, valueIdx, true);
258
- try {
259
- const freq = getNoteFrequency(value);
260
- if (typeof freq !== "number") {
261
- return;
262
- }
263
- await this._playFrequency(freq, note.duration);
264
- }
265
- catch (e) {
266
- console.error(e);
267
- }
268
- }
269
- _removeAudioSource(source) {
270
- source.stop();
271
- source.disconnect();
272
- this._audioSources.splice(this._audioSources.indexOf(source), 1);
273
- }
274
- _unmute() {
275
- const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
276
- if (!soundsOptions) {
277
- return;
278
- }
279
- if (!container.audioContext) {
280
- container.audioContext = new AudioContext();
281
- }
282
- if (!this._audioSources) {
283
- this._audioSources = [];
284
- }
285
- const gain = container.audioContext.createGain();
286
- gain.connect(container.audioContext.destination);
287
- gain.gain.value = soundsOptions.volume.value / 100;
288
- this._gain = gain;
289
- this._initEvents();
290
- this._engine.dispatchEvent("soundsUnmuted", { container: this._container });
291
- }
292
- _updateMuteIcons() {
293
- const container = this._container, muteImg = this._muteImg, unmuteImg = this._unmuteImg;
294
- if (muteImg) {
295
- muteImg.style.display = container.muted ? "block" : "none";
296
- }
297
- if (unmuteImg) {
298
- unmuteImg.style.display = container.muted ? "none" : "block";
299
- }
300
- }
301
- _updateMuteStatus() {
302
- const container = this._container;
303
- if (container.muted) {
304
- this._mute();
305
- }
306
- else {
307
- this._unmute();
308
- this._playMuteSound();
309
- }
310
348
  }
311
- _updateVolume() {
312
- var _a;
313
- const container = this._container, soundsOptions = container.actualOptions.sounds;
314
- if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable)) {
315
- return;
316
- }
317
- clamp(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
318
- let stateChanged = false;
319
- if (this._volume <= 0 && !container.muted) {
320
- this._volume = 0;
321
- container.muted = true;
322
- stateChanged = true;
323
- }
324
- else if (this._volume > 0 && container.muted) {
325
- container.muted = false;
326
- stateChanged = true;
327
- }
328
- if (stateChanged) {
329
- this._updateMuteIcons();
330
- this._updateMuteStatus();
331
- }
332
- if ((_a = this._gain) === null || _a === void 0 ? void 0 : _a.gain) {
333
- this._gain.gain.value = this._volume / 100;
334
- }
349
+ stop() {
350
+ this._container.muted = true;
351
+ this._mute();
352
+ removeImage(this._muteImg);
353
+ removeImage(this._unmuteImg);
354
+ removeImage(this._volumeDownImg);
355
+ removeImage(this._volumeUpImg);
335
356
  }
336
357
  }
package/esm/index.js CHANGED
@@ -9,21 +9,19 @@ class SoundsPlugin {
9
9
  return new SoundsInstance(container, this._engine);
10
10
  }
11
11
  loadOptions(options, source) {
12
- if (!this.needsPlugin(source)) {
12
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
13
13
  return;
14
14
  }
15
15
  let soundsOptions = options.sounds;
16
- if ((soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.load) === undefined) {
16
+ if (soundsOptions?.load === undefined) {
17
17
  options.sounds = soundsOptions = new Sounds();
18
18
  }
19
- soundsOptions.load(source === null || source === void 0 ? void 0 : source.sounds);
19
+ soundsOptions.load(source?.sounds);
20
20
  }
21
21
  needsPlugin(options) {
22
- var _a, _b;
23
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.sounds) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
22
+ return options?.sounds?.enable ?? false;
24
23
  }
25
24
  }
26
- export async function loadSoundsPlugin(engine) {
27
- const plugin = new SoundsPlugin(engine);
28
- await engine.addPlugin(plugin);
25
+ export async function loadSoundsPlugin(engine, refresh = true) {
26
+ await engine.addPlugin(new SoundsPlugin(engine), refresh);
29
27
  }
package/esm/utils.js CHANGED
@@ -17,7 +17,6 @@ export function getNoteFrequency(note) {
17
17
  if (!result || !result.length) {
18
18
  return;
19
19
  }
20
- console.log(result);
21
20
  const noteKey = result[2] || result[0], noteItem = notes.get(noteKey);
22
21
  if (!noteItem) {
23
22
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/plugin-sounds",
3
- "version": "3.0.0-alpha.0",
3
+ "version": "3.0.0-beta.0",
4
4
  "description": "tsParticles sounds plugin",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -72,10 +72,11 @@
72
72
  "unpkg": "tsparticles.plugin.sounds.min.js",
73
73
  "module": "esm/index.js",
74
74
  "types": "types/index.d.ts",
75
+ "sideEffects": false,
76
+ "dependencies": {
77
+ "@tsparticles/engine": "^3.0.0-beta.0"
78
+ },
75
79
  "publishConfig": {
76
80
  "access": "public"
77
- },
78
- "dependencies": {
79
- "@tsparticles/engine": "^3.0.0-alpha.0"
80
81
  }
81
- }
82
+ }