diff --git a/src/App.svelte b/src/App.svelte index 33b9f26..587587e 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -17,17 +17,27 @@ let rollThemBones = new Audio("https://soundbible.com/mp3/Shake And Roll Dice-SoundBible.com-591494296.mp3"); let playSounds = true; + let timeout: NodeJS.Timeout; function setSoundOptions(event: any) { playSounds = event.detail.soundEnabled; } - function roll(event: any) { - // console.log("called roll with", details); + function handleRoll(event: any) { + clearTimeout(timeout); rollThemBones.pause(); rollThemBones.currentTime = 0; + if (playSounds) { + rollThemBones.volume = 0.6 + 0.4 * Math.random(); // vary the volume + rollThemBones.playbackRate = 0.6 + 0.8 * Math.random(); // vary the playback rate rollThemBones.play(); + + let playTime = rollThemBones.duration * 1000 * (0.3 + 0.7 * Math.random()); // vary the playback duration + console.log(`rolling die at volume ${rollThemBones.volume}, playbackRate ${rollThemBones.playbackRate}, play duration ${playTime}`); + timeout = setTimeout(() => { + rollThemBones.pause(); + }, playTime); } } @@ -40,37 +50,37 @@
- + - + - + - + - + - + - + - + - + - + - +