Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-snowfall

cahilfoley157.8kMIT2.2.0TypeScript support: included

A react component that creates a snowfall effect

react, component, snowfall, winter, christmas, canvas

readme

Snowfall Demo

React Snowfall

npm GitHub stars Conventional Commits

A react component that creates a snowfall effect

Installation

With npm

npm i react-snowfall

Or with yarn

yarn add react-snowfall

Usage

Basic usage requires no properties - it will grow to fill the nearest relative positioned parent element.

import React from 'react'
import ReactDOM from 'react-dom'
import Snowfall from 'react-snowfall'

ReactDOM.render(
  <div style={{ height: 400, width: 400, background: '#282c34', position: 'relative' }}>
    <Snowfall />
  </div>,
  document.querySelector('#app'),
)

Configuration

An optional color, style, and snowflakeCount property can be passed in to the component.

<Snowfall
  // Changes the snowflake color
  color="red"
  // Applied to the canvas element
  style={{ background: '#fff' }}
  // Controls the number of snowflakes that are created (default 150)
  snowflakeCount={200}
/>

Using Images

Instead of rendering colored circles you can instead pass in an array of image elements that will be randomly selected and used as the snowflake instead.

NOTE: If the images provided are not square they will be stretched into a 1:1 aspect ratio.

const snowflake1 = document.createElement('img')
snowflake1.src = '/assets/snowflake-1.png'
const snowflake2 = document.createElement('img')
snowflake2.src = '/assets/snowflake-2.jpg'

const images = [snowflake1, snowflake2]

const Demo = () => {
  return (
    <Snowfall
      // Applied to the canvas element
      style={{ background: '#fff' }}
      // Controls the number of snowflakes that are created (default 150)
      snowflakeCount={200}
      // Pass in the images to be used
      images={images}
    >
  )
}

Positioning

The snowfall container is absolute positioned and has the following default styles (see the definition):

position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;

If you want the component to cover the entire screen then you can change the position to fixed and using vw/vh units by passing in an overriding styles object:

<Snowfall
  style={{
    position: 'fixed',
    width: '100vw',
    height: '100vh',
  }}
/>

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.2.0 (2024-11-22)

Features

  • Snowflakes: Add support using opacity for image snowflakes (dbb6d51)

2.1.2 (2024-11-22)

Bug Fixes

  • Snowflake: Update the snowflake initialization to use the correct speed and wind values for the target speed and wind values (8db1a66)

2.1.1 (2024-09-06)

Bug Fixes

  • ESM: Update the published library to include file extensions for proper ESM compatibility (a5d6a8f), closes #76 #73

2.1.0 (2024-02-07)

Bug Fixes

  • Add module type to package.json to properly indicate the ESM bundle (ce6b83d)
  • Switch from useLayoutEffect to useEffect for better SSR compatibility (400c0e6)

2.0.0 (2024-01-25)

⚠ BREAKING CHANGES

  • Snowfall: Lots of internals have moved around - if you are only using the <Snowfall /> component then you shouldn't have to make any changes. If you were importing any of the library internals such as the custom hooks or some utilities then these have been reorganized and are now not exported from the root of the package - you will need to import them from the specific files.

Features

  • Snowfall: Abstract the snowflake rendering logic into the SnowfallCanvas class that is independent of React. (7cccc63)

1.2.1 (2022-08-25)

1.2.0 (2022-08-25)

Features

  • Add support for rendering images instead of circles for snowflake contents (d0d99e4)

1.1.2 (2022-01-20)

Bug Fixes

  • Types: Remove labeled tuple elements (30dbfa7), closes #33

1.1.1 (2021-11-29)

Bug Fixes

  • next: remove unused config variable to prevent NextJS crashing on window being undefined (e4cb1f2)

1.1.0 (2021-02-23)

Features

  • SnowflakeConfig: allow additional snowflake properties to be overridden via the Snowfall props (5d7b3f5)

1.0.2 (2019-06-09)

1.0.1 (2019-06-09)

Bug Fixes

  • color: read color from snowflake config (60676a9)

Build System

  • map: publish source maps with build (74f9162)
  • publish src with npm package (7c49bd2)

1.0.0 (2019-06-09)

refactor

  • convert library to use hooks (0a45f70)

BREAKING CHANGES

  • now has a peer dependency on react and react-dom >16.8

0.3.0 (2018-12-12)

Features

  • add wrapping in translate calculations (62a16dd)

0.2.0 (2018-12-11)

Features

  • update snowflake location based on time between frames (49c2916)

0.1.3 (2018-12-09)

Features

  • snowflake: update speed interpolation smoothing (816d4d7)
  • add linear interpolation to speed and random speed shuffling (47a1cd6)