Interactive Demo & Implementation Guide
Fine-tune the waves, particles, and background gradient in real-time.
npm install js-waves-particles
Include the script tag. The class is exposed globally as WaveParticles.
<script src="dist/wave-particles.iife.js"></script>
<script>
const animation = new WaveParticles({
config: {
waves: [{ amplitude: 40, yOffset: 0.6 }]
}
});
</script>
Initialize inside useEffect and ensure you call destroy() to prevent memory
leaks
during hot-reloads or route changes.
import { useEffect } from 'react';
import WaveParticles from 'js-waves-particles';
function Background() {
useEffect(() => {
const animation = new WaveParticles();
return () => animation.destroy();
}, []);
return null;
}
Use the onMounted and onUnmounted lifecycle hooks.
<script setup>
import { onMounted, onUnmounted } from 'vue';
import WaveParticles from 'js-waves-particles';
onMounted(() => {
const animation = new WaveParticles();
onUnmounted(() => animation.destroy());
});
</script>
Implement AfterViewInit and OnDestroy interfaces.
import { Component, AfterViewInit, OnDestroy } from '@angular/core';
import WaveParticles from 'js-waves-particles';
@Component({ ... })
export class AppComponent implements AfterViewInit, OnDestroy {
private animation: WaveParticles;
ngAfterViewInit() {
this.animation = new WaveParticles();
}
ngOnDestroy() {
this.animation.destroy();
}
}
Every aspect of the visual engine can be tweaked via the constructor.
const animation = new WaveParticles({
canvas: '#custom-id',
config: {
waves: [{
amplitude: 60,
frequency: 0.003,
speed: 0.006,
yOffset: 0.55,
color: 'rgba(255, 255, 255, 0.2)',
layers: 3
}],
particles: { countScale: 8000, maxCount: 180 },
colors: {
backgroundGradient: ['#fdfcfb', '#e8d5d0'],
particleColorPrefixes: ['rgba(255, 0, 0,', 'rgba(0, 255, 0,']
}
}
});