Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ Spice Up Your Svelte App with Sound Interactions using svelte-sound ๐Ÿ˜Ž

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Spice Up Your Svelte App with Sound Interactions using svelte-sound ๐Ÿ˜Ž


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

How many of you have wondered how games are so addictive? One of the top reasons for that is the sound interactions they provide. ๐ŸŽฎ

When we think of human senses, we think of eyesight, hearing, taste, touch and smell. As web developers, we develop our sites and apps for eyesight and touch (mobile devices), but we canโ€™t do much with smell and taste. ๐Ÿ˜ข But what about hearing? Itโ€™s achievable and itโ€™s done on many places. ๐Ÿ™Œ

One such example of audio/sound interactions is Josh W Comeauโ€™s site (even he has dedicated a post for it here). His site is full of delightful sounds that make you want to explore more. ๐Ÿคฉ

After wondering about this for a while, I did some research and started digging deep into it and finally ended up creating svelte-sound. ๐Ÿš€

svelte-sound is a library that provides svelte actions to play interaction sounds on target DOM events. It uses Howler.js under the hood for cross-browser compatibility and performance. ๐Ÿ’ฏ It also supports dynamic imports and tree-shaking for advanced optimizations. ๐ŸŒณ

In this post, I will show you how to install and use svelte-sound in your svelte projects. I will also explain how it works internally using Svelte Action and Howler.js. ๐Ÿง

Are you ready to make your svelte app more engaging and fun? ๐Ÿ˜Ž Letโ€™s get started! ๐Ÿ‘‡

What is svelte-sound?

Svelte-sound is a svelte action library that lets you play interaction sounds on target DOM events with ease and flexibility. It has the following features:

  • Lightweight and performant: It uses only the core of howler.js, a popular JavaScript audio library for the modern web. Howler.js handles all the edge cases and bugs across browsers and platforms, supports all codecs for full cross-browser compatibility, and caches sounds for better performance.
  • First in class TypeScript support: It comes with TypeScript definitions out of the box, so you can enjoy type checking and auto-completion in your IDE.
  • Dynamic imports: It loads howler.js using dynamic imports to support partial hydration. This means that howler.js will only be loaded when it's needed, saving bandwidth and improving loading time.
  • Scalable: It can be used for simple sound effects or complex game interactions. You can control everything from play, pause and seek to rate, fade and loop, just to name a few.
  • Truly reactive by default: It leverages Svelte's reactivity system to update sounds automatically when their props change. You don't need to worry about managing state or lifecycle methods.

With svelte-sound, you can add sound interactions to your Svelte app with just a few lines of code. Let's see how it works. ๐Ÿ”Š

Getting Started

Its straight forward to start using svelte-sound in your svelte projects.

Installation

You can install svelte-sound using your preferred package manager. For example:

npm install svelte-sound

or

yarn add svelte-sound

or

pnpm install svelte-sound

Usage

we can use svelte-sound in following ways,

  • The sound action directly on an element, or
  • The useSound reusable action builder that returns a svelte action.

Letโ€™s see how each of them works.๐Ÿ‘‡

sound action

The sound action can be directly used on elements within svelte as a svelte action. You need to import it from "svelte-sound" and pass an object with two properties: src and events. The src is the URL of the sound file you want to play, and the events is an array of DOM events that will trigger the sound.

Property Type Description
src string url of the audio file to be used as an audio source
events string[] any valid javascript events as array of string

Note: Additionally the option can take any valid Howler.js Core Options

For example:

<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 >

In this example, we are using a local sound file called "click.mp4" that will play when we click on the button. You can also use online sound files by passing their URLs as strings. For example:

<script>
import { sound } from โ€œsvelte-soundโ€;
</ script >

<button use:sound={{ 
src: โ€œhttps://example.com/sounds/click.mp4โ€,
events: [โ€œclickโ€]
}}> 
Click Me!! ๐Ÿ”Š 
</button>

You can also specify multiple events for the same sound file by adding them to the array. For example:

<script>
import { sound } from โ€œsvelte-soundโ€;
import hover_mp4 from โ€œ./assets/hover.mp4โ€;
</script>

<button use:sound={{
src: hover_mp4,
events: [โ€œmouseoverโ€, โ€œmouseoutโ€]
}}> 
Hover Me!! ๐ŸŽถ 
</button>

In this example, we are using a local sound file called "hover.mp4" that will play when we hover over or out of the button.

useSound action builder

The useSound function can be reused multiple times on multiple elements within svelte. It returns a svelte action that you can apply to any element, which is re-useable and overridable.

useSound's API is very similar to that of sound but it takes three parameters

Parameter Type Description
src* string URL of the audio file to load as string
events* string[] any valid javascript events as array of string
options HowlOptions Howler.js options to be passed into the Howler constructor

Note: * are required parameters

You need to import it from "svelte-sound" and pass two arguments: the URL of the sound file (either local or online) and an array of DOM events that will trigger the sound. For example:

<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 >

In this example, we are creating a reusable action called click_sound that will play a local sound file called "click.mp4" when we click on any element that uses it.

You can also overwrite the options for each element by passing an object with new properties. For example:

<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โ€]}}>
Double Click Me!! ๐ŸŽต๐ŸŽต
</ button >

In this example, we are changing the event for one of the buttons to "dblclick" instead of "click".

Play below with the code โ†“โ†“โ†“

Internals

โš ๏ธ Beware of too much of technical jargon below

This package uses Svelte Action to play sounds on DOM events with Howler.js.

I built this library in a way that it uses dynamic imports and tree-shaking for advanced optimizations. Thus ensures very lightweight (less than 1kb) and more performant (using Howler.js)

Svelte Action

A Svelte Action is a function that can be applied to any element using the use directive. It receives a node and some parameters, and returns an object with a destroy function.

Howler.js in svelte-sound

Howler.js is a JavaScript audio library that supports multiple file formats, cross-browser compatibility, spatial sound effects, fading, looping, caching and more.

svelte-sound uses Howler.js under the hood to play sound files on target DOM events. It loads Howler.js using dynamic imports to support partial hydration. It also uses only Howler core module which has all the essential features without any extra plugins.

How svelte-sound creates actions?

svelte-sound exports two functions: sound and useSound. Both of them are Svelte Actions that use Howler.js internally.

Conclusion

In this post, I showed you how to use svelte-sound, a library that I created to add sound effects to your svelte app. We also explored how it works internally using Svelte Action and Howler.js.

As Josh W Comeau, the original inspiration for this package, said:

A million possibilities

The thing that strikes me about using audio on the web is that there is so much under-explored territory. I've been experimenting with sound for a while now, and I still feel like I'm just scratching the surface.
You've been given the tools to start experimenting, and I'd encourage you to have some fun with this, and see where it takes you =)

I hope you enjoyed this post and found it useful. If you have any questions or feedback, please let me know in the comments below.๐Ÿ˜Š

And if you liked svelte-sound, please consider giving it a star on GitHub. It would mean a lot to me.๐Ÿ™

...



๐Ÿ“Œ kbar-svelte-mini - ctrl+k menu for your Svelte website


๐Ÿ“ˆ 39.61 Punkte

๐Ÿ“Œ Medium CVE-2021-29261: Svelte Svelte


๐Ÿ“ˆ 36.11 Punkte

๐Ÿ“Œ Wireshark 2.0.0/2.0.1 SPICE Dissector packet-spice.c Denial of Service


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ Medium CVE-2017-15108: Spice-space Spice-vdagent


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ spice-gtk bis 0.34 spice-client Messages Pufferรผberlauf


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ spice-gtk bis 0.34 spice-client Messages Pufferรผberlauf


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ Medium CVE-2018-10873: Spice project Spice


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ spice-gtk up to 0.34 spice-client Messages memory corruption


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ spice-vdagentd up to 0.20 Unix Domain Socket spice-vdagent-sock allocation of resources


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ spice-vdagentd up to 0.20 File Transfer spice-vdagent-sock allocation of resources


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ CVE-2016-4419 | Wireshark 2.0.0/2.0.1 SPICE Dissector packet-spice.c resource management (EDB-38996 / Nessus ID 89104)


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ Wireshark 2.0.0/2.0.1 SPICE Dissector packet-spice.c Denial of Service


๐Ÿ“ˆ 33.08 Punkte

๐Ÿ“Œ SANS ICS Concept Videos - Industrial Protocol Interactions Using Modbus


๐Ÿ“ˆ 25.17 Punkte

๐Ÿ“Œ How to Test User Interactions Using the React Testing Library


๐Ÿ“ˆ 25.17 Punkte

๐Ÿ“Œ This AI Research Proposes a Framework to Model User Interactions with LLMs using Normanโ€™s Seven Stages of Action


๐Ÿ“ˆ 25.17 Punkte

๐Ÿ“Œ Meet HOI-Diff: Text-Driven Synthesis of 3D Human-Object Interactions Using Diffusion Models


๐Ÿ“ˆ 25.17 Punkte

๐Ÿ“Œ Your First Svelte Project: A Todo List App


๐Ÿ“ˆ 24.62 Punkte

๐Ÿ“Œ Svelte beats react: faster, simpler, and better for your next web app!


๐Ÿ“ˆ 24.62 Punkte

๐Ÿ“Œ Title: Boost Your Database Interactions with Prisma! โœจ๐Ÿ”


๐Ÿ“ˆ 23.55 Punkte

๐Ÿ“Œ How to Access Proxmox VE Virtual Machines and LXC Containers Remotely via SPICE Protocol using Virt-Viewer


๐Ÿ“ˆ 21.67 Punkte

๐Ÿ“Œ The World's Biggest Spice Company is Using AI To Find New Flavors


๐Ÿ“ˆ 21.67 Punkte

๐Ÿ“Œ Svelte Tips and Features to Enhance Your Developer Experience


๐Ÿ“ˆ 21.56 Punkte

๐Ÿ“Œ How to Set Up a Svelte App with Rollup


๐Ÿ“ˆ 21.12 Punkte

๐Ÿ“Œ DeskScapes 10 is a simple way to spice up (and animate) your desktop


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Spice Up Your Website with These Color-Related Functions


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Spice Up Your Website with These Color-Related Functions


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Slack adds a GIF picker to let you spice up your work messages. Here's how


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Best Official Kodi Skins to Spice Up Your Interface


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Microsoft now lets you spice up your Teams calls with custom backgrounds


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Adding spice to your sudo session with a lecture file on Linux or Unix


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ To all you virt-manager and SPICE Display users out there. Finally get your clipboard sharing, dynamic resolution and other things working.


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Surface Originals skins are a killer way to spice up your Microsoft Surface


๐Ÿ“ˆ 20.05 Punkte

๐Ÿ“Œ Spice up your Resident Evil Village playthrough with these PC mods


๐Ÿ“ˆ 20.05 Punkte











matomo