npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-sound

v0.6.0

Published

An utility for playing sounds in Svelte using svelte actions

Downloads

525

Readme

svelte-sound

TypeScript Npm package version GitHub license Made for svelte

Svelte Actions to play interaction sounds on target DOM events

Features

  • Lightweight and performant
  • Only Howler core is used
  • Loads Howler using dynamic imports to support partial hydration
  • Scalable (can be used in complex game interactions)
  • Truly reactive by default

Installation

npm i svelte-sound

or

yarn add svelte-sound

or

pnpm i svelte-sound

Usage

svelte-sound can be used in three ways,

  1. sound
  2. useSound
  3. Sound class

sound

This can be directly used on elements within svelte as a svelte action. This actions following mandatory options, | Option | Type | Description | | --- | --- | --- | | src | string | The source of the sound file | | events | [PlayEvent, StopEvent?] | An array of events to play and stop the sound | and valid Howler Core Options as optional options

<script>
  import { sound } from "svelte-sound";
  import click_mp4 from "./assets/click.mp4";
</script>

<button use:sound={{src: click_mp4, events: ["click"]}}>
    Click Me!!
</button>

useSound

This can be reused multiple times on multiple elements within svelte. This returns a svelte action. This method accepts following parameters, | Parameter | Type | Description | | --- | --- | --- | | src | string | The source of the sound file | | events | [PlayEvent, StopEvent?] | An array of events to play and stop the sound | | options | HowlerOptions? | An object of valid Howler Core Options |

<script>
  import { useSound } from "svelte-sound";
  import click_mp4 from "./assets/click.mp4";

  const click_sound = useSound(click_mp4, ["click"])
</script>

<button use:click_sound>
    Click Me!!
</button>

<!-- Options can be overwritten -->
<button use:click_sound={{events:["dblclick"]}}>
    Click Me Twice!!
</button>

Sound class

This can be used to play the sound programmatically. This returns a Sound class instance. This method accepts following parameters, | Parameter | Type | Description | | --- | --- | --- | | src | string | The source of the sound file | | options | HowlerOptions? | An object of valid Howler Core Options |

import { Sound } from "svelte-sound";
import click_mp4 from "./assets/click.mp4";

const click_sound = new Sound(click_mp4);

function playSound() {
  click_sound.play();
} // playSound can be called anywhere in the code

Example

For usage example have a look at example directory

FAQs

How to Stop the sound/audio?

You can always pass an event as the second element of the events array to stop the sound on that event. e.g.

<script>
  import { sound } from "svelte-sound";
  import click_mp4 from "./assets/click.mp4";
</script>

<button use:sound={{src: click_mp4, events: ["click", "dblclick"]}}>
    Click Me!!
</button>

In the above example the sound will be played on click and stopped on dblclick

Alternatively you can use play and stop methods added to the element by the action. To play or stop the sound you can call the respective methods on the element. e.g.

<script>
  import { sound } from "svelte-sound";
  import click_mp4 from "./assets/click.mp4";

  let button;
</script>

<button
  bind:this={button}
  on:dblclick={() => button.stop()}
  use:sound={{src: click_mp4, events: ["click"]}}>
    Click Me!!
</button>

In the above example the sound will be played on click and stopped on dblclick using the stop method which we done programmatically.

How to use this library programmatically without any DOM events?

You can use Sound class exported by the package to play the sound programmatically. e.g.

import { Sound } from "svelte-sound";
import click_mp4 from "./assets/click.mp4";

const click_sound = new Sound(click_mp4);

function playSound() {
  click_sound.play();
}

In the above example the sound will be played on calling the play method of the click_sound object.

License

MIT


Made with ❤️ by Rajaniraiyn

^^^ move to top ^^^