Install @fireworks-js/svelte via npm
masterTo use fireworks in a Svelte application, install the @fireworks-js/svelte package using npm.
npm install @fireworks-js/svelterepository·master·Indexed 23 days ago
https://github.com/crashmax-dev/fireworks-jsA lightweight, zero-dependency JavaScript library for creating fireworks animations. It supports vanilla JS and provides official integrations for React, Vue, Angular, Svelte, Solid, Preact, and Web Components. The library includes a Fireworks API for controlling animation lifecycles (start, stop, pause, launch), customizable visual and physics options, mouse interactivity, and sound effect support.
To use fireworks in a Svelte application, install the @fireworks-js/svelte package using npm.
npm install @fireworks-js/svelteThe Fireworks component provides a Svelte wrapper for the fireworks engine. You can bind the component instance to a variable using bind:this to access the underlying fireworks instance and control its lifecycle (e.g., starting or stopping).
Key props:
bind:this: Binds the component instance to a variable.autostart: Boolean determining if the fireworks start automatically.options: An object of type FireworksOptions to configure the visual effects.class: CSS class to apply to the fireworks container.<script lang="ts">
import { Fireworks } from '@fireworks-js/svelte'
import type { FireworksOptions } from '@fireworks-js/svelte'
let fw: Fireworks
let options: FireworksOptions = {
opacity: 0.5
}
function toggleFireworks() {
const fireworks = fw.fireworksInstance()
if (fireworks.isRunning) {
fireworks.waitStop()
} else {
fireworks.start()
}
}
</script>
<Fireworks bind:this={fw} autostart={false} {options} class="fireworks" />The library provides dedicated packages for various frameworks. Install the one corresponding to your project:
npm install @fireworks-js/reactnpm install @fireworks-js/preactnpm install @fireworks-js/solidnpm install @fireworks-js/vuenpm install @fireworks-js/sveltenpm install @fireworks-js/angularnpm install @fireworks-js/webYou can include fireworks-js directly in your HTML using a CDN. Note that when using the UMD build via CDN, you must access the constructor via Fireworks.default.
<!-- jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/fireworks-js@2.x/dist/index.umd.js"></script>
<!-- UNPKG -->
<script src="https://unpkg.com/fireworks-js@2.x/dist/index.umd.js"></script>
<!-- Usage -->
<script>
const container = document.querySelector('.fireworks')
const fireworks = new Fireworks.default(container)
fireworks.start()
</script>To use the vanilla JavaScript version of the library, install the fireworks-js package using your preferred package manager.
npm install fireworks-js
yarn add fireworks-js
pnpm add fireworks-jsThe mouse option allows fireworks to respond to user input. It accepts an object with the following properties:
click (boolean): Enables fireworks on click. Default is false.move (boolean): Enables fireworks on mouse movement. Default is false.max (number): The maximum number of fireworks. Note that max has no effect if click is set to false.mouse: {
click: false,
move: false,
max: 1
}The Fireworks constructor accepts an optional options object to customize the visual and behavioral aspects of the fireworks. Many properties accept a min and max object, which allows the engine to randomly select values within that range for variety.
Key configuration groups include:
hue, brightness, decay, opacity, lineWidth, lineStyle, traceLength, traceSpeed, flickering.acceleration, friction, gravity, explosion.delay, intensity, particles, autoresize.mouse (supports click and move).sound (requires enabled: true and a files array).const fireworks = new Fireworks(container, {
autoresize: true,
opacity: 0.5,
acceleration: 1.05,
friction: 0.97,
gravity: 1.5,
particles: 50,
traceLength: 3,
traceSpeed: 10,
explosion: 5,
intensity: 30,
flickering: 50,
lineStyle: 'round',
hue: {
min: 0,
max: 360
},
delay: {
min: 30,
max: 60
},
rocketsPoint: {
min: 50,
max: 50
},
lineWidth: {
explosion: {
min: 1,
max: 3
},
trace: {
min: 1,
max: 2
}
},
brightness: {
min: 50,
max: 80
},
decay: {
min: 0.015,
max: 0.03
},
mouse: {
click: false,
move: false,
max: 1
}
})The sound option enables audio feedback for the fireworks. It accepts an object with the following properties:
enabled (boolean): Whether sound is active. Default is false.files (string[]): An array of paths to audio files.volume (object): An object containing min and max values to randomly select the volume level.NgFireworksModule into your Angular module (usually AppModule). This module exports the FireworksDirective, which allows you to apply fireworks effects to HTML elements using a directive. You can also use the FireworksOptions type from fireworks-js to type your configuration objects.The @fireworks-js/web package provides a custom HTML element <fireworks-js> that allows you to embed fireworks animations directly into your HTML. The component manages its own lifecycle, starting the animation when connected to the DOM and stopping it when disconnected.
You can configure the animation and its appearance using two attributes:
options: A JSON string representing a FireworksOptions object. This controls the animation parameters (e.g., particle count, colors, physics).style: A string of CSS rules applied to the internal container div. This allows you to control the size, position, or other visual aspects of the fireworks container.<fireworks-js
options='{"autostart": true, "particles": 50}'
style='height: 500px; width: 100%;'>
</fireworks-js>To use the library in a standard JavaScript environment, import the Fireworks class, provide a DOM container, and call .start().
import { Fireworks } from 'fireworks-js'
const container = document.querySelector('.container')
const fireworks = new Fireworks(container, { /* options */ })
fireworks.start()The Fireworks instance provides several methods to control the lifecycle and state of the animation:
.start(): Begins the fireworks animation..launch(count): Launches a specific number of fireworks. count is a number (default: 1)..stop(dispose): Stops the fireworks. dispose is a boolean (default: false)..waitStop(dispose): Asynchronously stops the fireworks. dispose is a boolean (default: false)..pause(): Pauses or resumes the fireworks..clear(): Clears the canvas of all current fireworks..currentOptions: Returns the current configuration options..updateOptions(options): Forces an update of the fireworks options..updateSize(sizes): Forces an update of the canvas size..updateBoundaries(boundaries): Forces an update of the canvas boundaries.