A customizable and performant Vue 3 composable for creating beautiful snowfall effects in your web applications.
- π¨ Highly Customizable - Configure speed, size, color, interval, chaos level, and more
- β‘ Performant - Optimized with efficient DOM management, shared CSS animations, and automatic cleanup
- π― Flexible - Choose any container element or use the default body
- π§ Easy to Use - Simple API with sensible defaults
- π¦ Lightweight - Small bundle size, no external dependencies
- π Vue 3 Compatible - Built for Vue 3 Composition API
npm install vue-snowfall
# or
pnpm add vue-snowfall
# or
yarn add vue-snowfallimport { useSnowfall } from 'vue-snowfall'
import { onMounted, onBeforeUnmount } from 'vue'
export default {
setup() {
const { startSnowflakes, stopSnowflakes } = useSnowfall()
onMounted(() => {
startSnowflakes()
})
onBeforeUnmount(() => {
stopSnowflakes()
})
}
}import { useSnowfall } from 'vue-snowfall'
const { startSnowflakes, stopSnowflakes } = useSnowfall({
interval: 300, // Create snowflake every 300ms
minSpeed: 15, // Minimum falling speed (seconds)
maxSpeed: 25, // Maximum falling speed (seconds)
minSize: 10, // Minimum size in pixels
maxSize: 30, // Maximum size in pixels
color: '#ffffff', // Snowflake color
zIndex: 999, // Z-index for snowflakes
maxFlakes: 100, // Maximum number of snowflakes (optional)
chaos: 50, // Chaos level for random movement (0-100)
container: document.body // Container element or selector
})
startSnowflakes()Creates a snowfall effect instance.
| Option | Type | Default | Description |
|---|---|---|---|
container |
HTMLElement | string |
document.body |
Container element or CSS selector |
interval |
number |
500 |
Interval between creating snowflakes (ms) |
minSpeed |
number |
20 |
Minimum falling speed (seconds) |
maxSpeed |
number |
30 |
Maximum falling speed (seconds) |
minSize |
number |
10 |
Minimum snowflake size in pixels |
maxSize |
number |
30 |
Maximum snowflake size in pixels |
color |
string |
'#fff' |
Snowflake color (hex, rgb, etc.) |
zIndex |
number |
999 |
Z-index for snowflakes |
maxFlakes |
number | null |
null |
Maximum number of snowflakes (null = unlimited) |
chaos |
number |
50 |
Chaos level for random movement (0-100, higher = more chaotic) |
An object with the following methods:
startSnowflakes()- Start the snowfall animationstopSnowflakes()- Stop the animation and remove all snowflakes
import { useSnowfall } from 'vue-snowfall'
const { startSnowflakes, stopSnowflakes } = useSnowfall({
container: '#snow-container', // CSS selector
// or
container: document.querySelector('.my-container') // Element
})
startSnowflakes()const { startSnowflakes } = useSnowfall({
interval: 200,
minSpeed: 5,
maxSpeed: 10
})
startSnowflakes()const { startSnowflakes } = useSnowfall({
interval: 1000,
minSpeed: 30,
maxSpeed: 50,
minSize: 20,
maxSize: 50
})
startSnowflakes()const { startSnowflakes } = useSnowfall({
color: '#ff6b9d' // Pink snowflakes!
})
startSnowflakes()const { startSnowflakes } = useSnowfall({
chaos: 80 // Higher chaos = more random horizontal movement (0-100)
})
startSnowflakes()const { startSnowflakes } = useSnowfall({
maxFlakes: 50, // Limit to 50 snowflakes for better performance
interval: 400
})
startSnowflakes()import { ref, watch } from 'vue'
import { useSnowfall } from 'vue-snowfall'
const speed = ref(20)
let snowfall = null
watch(speed, (newSpeed) => {
if (snowfall) {
snowfall.stopSnowflakes()
}
snowfall = useSnowfall({ minSpeed: newSpeed, maxSpeed: newSpeed + 10 })
snowfall.startSnowflakes()
}, { immediate: true })const snowfall1 = useSnowfall({ container: '#container1' })
const snowfall2 = useSnowfall({ container: '#container2', color: '#ff0000' })
snowfall1.startSnowflakes()
snowfall2.startSnowflakes()- Use
maxFlakesto limit the number of snowflakes for better performance on low-end devices - Increase
intervalto create snowflakes less frequently - Use a specific container instead of
document.bodywhen possible - Stop snowfall when not visible (e.g., when component is unmounted or tab is hidden)
- Optimized rendering - The library uses a single shared CSS animation with CSS variables for optimal performance
- Automatic cleanup - Snowflakes are automatically removed when animation completes, reducing memory usage
- Make sure the container has a defined height
- Check that the container is visible (not
display: none) - Verify that
startSnowflakes()is called after the container is mounted
- Reduce
maxFlakesor increaseinterval - Use a smaller container instead of the entire body
- Consider stopping snowfall when the page is not visible
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
Made with βοΈ by Erlan Kaparov