@tsparticles/plugin-sounds 3.9.0 → 4.0.0-alpha.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 (44) hide show
  1. package/902.min.js +2 -0
  2. package/902.min.js.LICENSE.txt +1 -0
  3. package/browser/Options/Classes/SoundsEvent.js +1 -1
  4. package/browser/SoundsInstance.js +26 -24
  5. package/browser/index.js +6 -4
  6. package/browser/utils.js +2 -2
  7. package/cjs/Options/Classes/Sounds.js +9 -13
  8. package/cjs/Options/Classes/SoundsAudio.js +4 -8
  9. package/cjs/Options/Classes/SoundsEvent.js +14 -18
  10. package/cjs/Options/Classes/SoundsIcon.js +3 -7
  11. package/cjs/Options/Classes/SoundsIcons.js +8 -12
  12. package/cjs/Options/Classes/SoundsMelody.js +5 -9
  13. package/cjs/Options/Classes/SoundsNote.js +3 -7
  14. package/cjs/Options/Classes/SoundsVolume.js +4 -8
  15. package/cjs/Options/Interfaces/ISounds.js +1 -2
  16. package/cjs/Options/Interfaces/ISoundsAudio.js +1 -2
  17. package/cjs/Options/Interfaces/ISoundsEvent.js +1 -2
  18. package/cjs/Options/Interfaces/ISoundsIcon.js +1 -2
  19. package/cjs/Options/Interfaces/ISoundsIcons.js +1 -2
  20. package/cjs/Options/Interfaces/ISoundsMelody.js +1 -2
  21. package/cjs/Options/Interfaces/ISoundsNote.js +1 -2
  22. package/cjs/Options/Interfaces/ISoundsVolume.js +1 -2
  23. package/cjs/SoundsInstance.js +53 -55
  24. package/cjs/SoundsPlugin.js +12 -16
  25. package/cjs/enums.js +4 -7
  26. package/cjs/index.js +6 -7
  27. package/cjs/types.js +1 -2
  28. package/cjs/utils.js +5 -11
  29. package/dist_browser_SoundsPlugin_js.js +140 -0
  30. package/esm/Options/Classes/SoundsEvent.js +1 -1
  31. package/esm/SoundsInstance.js +26 -24
  32. package/esm/index.js +6 -4
  33. package/esm/utils.js +2 -2
  34. package/package.json +4 -3
  35. package/report.html +5 -4
  36. package/tsparticles.plugin.sounds.js +209 -140
  37. package/tsparticles.plugin.sounds.min.js +1 -1
  38. package/tsparticles.plugin.sounds.min.js.LICENSE.txt +1 -1
  39. package/types/SoundsInstance.d.ts +1 -1
  40. package/types/index.d.ts +1 -1
  41. package/umd/Options/Classes/SoundsEvent.js +1 -1
  42. package/umd/SoundsInstance.js +25 -23
  43. package/umd/index.js +41 -5
  44. package/umd/utils.js +2 -2
@@ -1,15 +1,15 @@
1
- import { clamp, executeOnSingleOrMultiple, getLogger, isArray, isNumber, itemFromArray, itemFromSingleOrMultiple, mouseDownEvent, percentDenominator, touchStartEvent, } from "@tsparticles/engine";
1
+ import { clamp, executeOnSingleOrMultiple, getLogger, isArray, isNumber, itemFromArray, itemFromSingleOrMultiple, mouseDownEvent, percentDenominator, safeDocument, touchStartEvent, } from "@tsparticles/engine";
2
2
  import { ImageDisplay, SoundsEventType } from "./enums.js";
3
3
  import { getNoteFrequency, isWindowMuted, unmuteWindow } from "./utils.js";
4
4
  const zIndexOffset = 1, rightOffset = 1, minVolume = 0;
5
5
  function initImage(data) {
6
- const img = document.createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
6
+ const img = safeDocument().createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
7
7
  setIconStyle(img, pos.top + margin, pos.right -
8
8
  (margin * (rightOffsets.length + rightOffset) +
9
9
  width +
10
10
  rightOffsets.reduce((a, b) => a + b, defaultAccumulator)), display, options.fullScreen.zIndex + zIndexOffset, width, margin, style);
11
11
  img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
12
- const parent = container.canvas.element?.parentNode ?? document.body;
12
+ const parent = container.canvas.element?.parentNode ?? safeDocument().body;
13
13
  parent.append(img);
14
14
  img.addEventListener("click", () => {
15
15
  void clickCb();
@@ -24,12 +24,11 @@ function removeImage(image) {
24
24
  }
25
25
  function setIconStyle(icon, top, left, display, zIndex, width, margin, style) {
26
26
  icon.style.userSelect = "none";
27
- icon.style.webkitUserSelect = "none";
28
27
  icon.style.position = "absolute";
29
- icon.style.top = `${top + margin}px`;
30
- icon.style.left = `${left - margin - width}px`;
28
+ icon.style.top = `${(top + margin).toString()}px`;
29
+ icon.style.left = `${(left - margin - width).toString()}px`;
31
30
  icon.style.display = display;
32
- icon.style.zIndex = `${zIndex + zIndexOffset}`;
31
+ icon.style.zIndex = (zIndex + zIndexOffset).toString();
33
32
  icon.style.cssText += style;
34
33
  }
35
34
  export class SoundsInstance {
@@ -56,7 +55,7 @@ export class SoundsInstance {
56
55
  if (this._container !== args.container) {
57
56
  return;
58
57
  }
59
- if (!this._container || !!this._container.muted || this._container.destroyed) {
58
+ if (!!this._container.muted || this._container.destroyed) {
60
59
  executeOnSingleOrMultiple(event.event, item => {
61
60
  this._engine.removeEventListener(item, cb);
62
61
  });
@@ -67,10 +66,17 @@ export class SoundsInstance {
67
66
  }
68
67
  const defaultNoteIndex = 0;
69
68
  if (event.audio) {
70
- this._playBuffer(itemFromSingleOrMultiple(event.audio));
69
+ const audio = itemFromSingleOrMultiple(event.audio);
70
+ if (!audio) {
71
+ return;
72
+ }
73
+ this._playBuffer(audio);
71
74
  }
72
75
  else if (event.melodies) {
73
76
  const melody = itemFromArray(event.melodies);
77
+ if (!melody) {
78
+ return;
79
+ }
74
80
  if (melody.melodies.length) {
75
81
  await Promise.allSettled(melody.melodies.map(m => this._playNote(m.notes, defaultNoteIndex, melody.loop)));
76
82
  }
@@ -80,6 +86,9 @@ export class SoundsInstance {
80
86
  }
81
87
  else if (event.notes) {
82
88
  const note = itemFromArray(event.notes);
89
+ if (!note) {
90
+ return;
91
+ }
83
92
  await this._playNote([note], defaultNoteIndex, false);
84
93
  }
85
94
  })();
@@ -158,8 +167,7 @@ export class SoundsInstance {
158
167
  if (!note) {
159
168
  return;
160
169
  }
161
- const value = note.value;
162
- const promises = executeOnSingleOrMultiple(value, async (_, idx) => {
170
+ const value = note.value, promises = executeOnSingleOrMultiple(value, async (_, idx) => {
163
171
  return this._playNoteValue(notes, noteIdx, idx);
164
172
  });
165
173
  await (isArray(promises) ? Promise.allSettled(promises) : promises);
@@ -168,9 +176,6 @@ export class SoundsInstance {
168
176
  if (loop && nextNoteIdx >= notes.length) {
169
177
  nextNoteIdx = nextNoteIdx % notes.length;
170
178
  }
171
- if (this._container.muted) {
172
- return;
173
- }
174
179
  await this._playNote(notes, nextNoteIdx, loop);
175
180
  };
176
181
  this._playNoteValue = async (notes, noteIdx, valueIdx) => {
@@ -179,6 +184,9 @@ export class SoundsInstance {
179
184
  return;
180
185
  }
181
186
  const value = itemFromSingleOrMultiple(note.value, valueIdx, true);
187
+ if (!value) {
188
+ return;
189
+ }
182
190
  try {
183
191
  const freq = getNoteFrequency(value);
184
192
  if (!isNumber(freq)) {
@@ -201,11 +209,7 @@ export class SoundsInstance {
201
209
  if (!soundsOptions) {
202
210
  return;
203
211
  }
204
- const audioContext = this._getAudioContext();
205
- if (!this._audioSources) {
206
- this._audioSources = [];
207
- }
208
- const gain = audioContext.createGain();
212
+ const audioContext = this._getAudioContext(), gain = audioContext.createGain();
209
213
  gain.connect(audioContext.destination);
210
214
  gain.gain.value = soundsOptions.volume.value / percentDenominator;
211
215
  this._gain = gain;
@@ -228,11 +232,11 @@ export class SoundsInstance {
228
232
  this._updateMuteStatus = async () => {
229
233
  const container = this._container, audioContext = this._getAudioContext();
230
234
  if (container.muted) {
231
- await audioContext?.suspend();
235
+ await audioContext.suspend();
232
236
  await this._mute();
233
237
  }
234
238
  else {
235
- await audioContext?.resume();
239
+ await audioContext.resume();
236
240
  this._unmute();
237
241
  this._playMuteSound();
238
242
  }
@@ -416,9 +420,7 @@ export class SoundsInstance {
416
420
  }
417
421
  _getAudioContext() {
418
422
  const container = this._container;
419
- if (!container.audioContext) {
420
- container.audioContext = new AudioContext();
421
- }
423
+ container.audioContext ??= new AudioContext();
422
424
  return container.audioContext;
423
425
  }
424
426
  }
package/esm/index.js CHANGED
@@ -1,5 +1,7 @@
1
- import { SoundsPlugin } from "./SoundsPlugin.js";
2
- export async function loadSoundsPlugin(engine, refresh = true) {
3
- engine.checkVersion("3.9.0");
4
- await engine.addPlugin(new SoundsPlugin(engine), refresh);
1
+ export function loadSoundsPlugin(engine) {
2
+ engine.checkVersion("4.0.0-alpha.0");
3
+ engine.register(async (e) => {
4
+ const { SoundsPlugin } = await import("./SoundsPlugin.js");
5
+ e.addPlugin(new SoundsPlugin(engine));
6
+ });
5
7
  }
package/esm/utils.js CHANGED
@@ -17,11 +17,11 @@ export function getNoteFrequency(note) {
17
17
  if (!result?.length) {
18
18
  return;
19
19
  }
20
- const noteKey = result[groupKey] || result[defaultMatchKey], noteItem = notes.get(noteKey);
20
+ const noteKey = result[groupKey] ?? result[defaultMatchKey], noteItem = notes.get(noteKey);
21
21
  if (!noteItem) {
22
22
  return;
23
23
  }
24
- return noteItem[parseInt(result[innerGroupKey] || "0")];
24
+ return noteItem[parseInt(result[innerGroupKey] ?? "0")];
25
25
  }
26
26
  let muted = true;
27
27
  export const isWindowMuted = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/plugin-sounds",
3
- "version": "3.9.0",
3
+ "version": "4.0.0-alpha.0",
4
4
  "description": "tsParticles sounds plugin",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -86,9 +86,10 @@
86
86
  "./package.json": "./package.json"
87
87
  },
88
88
  "dependencies": {
89
- "@tsparticles/engine": "3.9.0"
89
+ "@tsparticles/engine": "4.0.0-alpha.0"
90
90
  },
91
91
  "publishConfig": {
92
92
  "access": "public"
93
- }
93
+ },
94
+ "type": "module"
94
95
  }