ambient-snow
v0.2.0
Published
An idle snowfall overlay for React and Next.js: snow settles, a snowman and a pine grow, then a short gust clears the scene.
Maintainers
Readme
ambient-snow
An idle snowfall overlay for React and Next.js. After a quiet period the snow starts falling, settles into a bank, and a snowman, a winter pine and three wrapped gifts grow out of it. When you come back to the page, a short gust carries the whole scene away.
- Canvas 2D. No images, no CSS import, no runtime dependencies.
- About 16 kB gzipped.
- Flake size, count, speed, scene size, delay and exit can be changed with props — live, while the overlay is mounted.
- Four built-in flake silhouettes, or draw your own with
renderFlake. - A subtle, adjustable backdrop scrim keeps the snow readable on light themes.
- Respects
prefers-reduced-motion(renders a static scene instead of animating). - Fixed,
pointer-events: noneoverlay. It never blocks the page.
Install
npm install ambient-snow
# or
pnpm add ambient-snow
# or
bun add ambient-snowReact 18 or newer is a peer dependency.
Next.js
The package ships its own "use client" directive, so it can be rendered from a Server Component layout. Add it once, near the end of <body>:
// app/layout.tsx
import { AmbientSnow } from "ambient-snow"
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<AmbientSnow />
</body>
</html>
)
}React
import { AmbientSnow } from "ambient-snow"
export function App() {
return (
<main>
<h1>Hello</h1>
<AmbientSnow idleDelay={12_000} flakeShape="crystal" sceneScale={1.1} />
</main>
)
}Props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| idleDelay | number | 30000 | Milliseconds without activity before the scene appears. 0 shows it right away. |
| exitDuration | number | 0.5 | How long the exit gust lasts, in seconds (0.1–3). |
| flakeCount | number | 144 | Maximum flakes on screen (1–1200). |
| flakeSize | [number, number] | [1.8, 4] | Min and max flake radius in CSS pixels (0.5–12). |
| flakeColor | string | "rgba(176, 207, 231, 0.96)" | Fill and stroke color of the built-in flakes. |
| flakeShape | "soft" \| "crystal" \| "star" \| "hexagon" | "soft" | Built-in flake silhouette. Ignored when renderFlake is set. |
| renderFlake | (context, flake) => void | — | Draw a custom flake. See below. |
| speed | number | 1 | Motion multiplier for falling, settling and the scene reveal (0.2–4). |
| sceneScale | number | 1 | Size of the lower scene: snowman, pine and gifts (0.4–2.2). The snowbank keeps its own size. |
| backdropOpacity | number | 0.32 | Opacity (0–1) of a translucent scrim painted behind the snow, so flakes stay readable on bright pages. Set 0 to disable. |
| backdropColor | string | "rgb(12, 24, 38)" | Color of that backdrop. Ignored when backdropOpacity is 0. |
| zIndex | number | 20 | Stacking order of the fixed overlay. |
| className | string | — | Extra class for the overlay, if you want to target it in CSS. |
| style | CSSProperties | — | Inline styles merged onto the overlay. |
| disabled | boolean | false | Keeps the component mounted but turns the effect off. |
All numeric props are clamped to the ranges above, so out-of-range values cannot break the canvas.
Custom flakes
renderFlake is called for every flake on every frame. The context is already scaled to CSS pixels and globalAlpha is set to the flake opacity, but the origin is not moved for you: use flake.x and flake.y directly. Wrap state changes in save() / restore().
import { AmbientSnow, type SnowflakeRenderInfo } from "ambient-snow"
function drawDiamond(context: CanvasRenderingContext2D, flake: SnowflakeRenderInfo) {
context.fillStyle = "#dbeafe"
context.beginPath()
context.moveTo(flake.x, flake.y - flake.radius)
context.lineTo(flake.x + flake.radius * 0.7, flake.y)
context.lineTo(flake.x, flake.y + flake.radius)
context.lineTo(flake.x - flake.radius * 0.7, flake.y)
context.closePath()
context.fill()
}
<AmbientSnow renderFlake={drawDiamond} flakeSize={[2, 6]} />Keep custom renderers cheap: they run up to flakeCount times per frame.
Behavior
- Activity is detected on the document (
pointermove,pointerdown,click,wheel,scroll,keydown,input,change, touch events). Any activity while the scene is showing starts the exit gust. - Pressing a key or scrolling does not get blocked — listeners are passive and capture-only.
- The component stops animating when the tab is hidden, and restarts its idle timer when it becomes visible again.
- A new idle delay starts counting from the last activity. Changing props does not require a remount.
- The snowbank, snowman, pine and gifts are drawn procedurally on the same canvas, so there are no assets to host.
- Flakes are pre-rendered sprites (with their glow baked in) and the canvas renders at up to 1.5× device pixel ratio. In the demo, 400 flakes between 8 and 12 px hold 60 fps at that resolution. Very high counts still depend on the device, so leave
speedat1and reduceflakeCounton low-end phones.
Build and publish
pnpm --filter ambient-snow build
cd packages/ambient-snow
npm publishThe build emits ESM, CommonJS and .d.ts files into dist/. npm publish runs no build step by itself, so build first. The package name ambient-snow was available on npm when this was written; check with npm view ambient-snow before publishing if that may have changed.
License
MIT
