winter-fx 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +133 -2
- package/dist/frost/crystals.d.ts +17 -0
- package/dist/frost/crystals.js +145 -0
- package/dist/frost/frost.d.ts +7 -0
- package/dist/frost/frost.js +230 -0
- package/dist/garland/garland.d.ts +6 -0
- package/dist/garland/garland.js +117 -0
- package/dist/garland/layout.d.ts +9 -0
- package/dist/garland/layout.js +17 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/shared/motion.d.ts +1 -0
- package/dist/shared/motion.js +5 -0
- package/dist/shared/options.d.ts +9 -0
- package/dist/shared/options.js +82 -0
- package/dist/shared/overlay.d.ts +2 -0
- package/dist/shared/overlay.js +18 -0
- package/dist/shared/random.d.ts +2 -0
- package/dist/shared/random.js +4 -0
- package/dist/shared/stop.d.ts +1 -0
- package/dist/shared/stop.js +1 -0
- package/dist/shared/svg.d.ts +2 -0
- package/dist/shared/svg.js +13 -0
- package/dist/snow/flakes.d.ts +21 -0
- package/dist/snow/flakes.js +80 -0
- package/dist/snow/snow.d.ts +8 -0
- package/dist/snow/snow.js +101 -0
- package/package.json +60 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# winter-fx
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Falling snow, a twinkling garland and frost on the screen edges, for any web page. Each effect is one function call, works with any framework, and is 1.5 to 3 kB minified and compressed. No dependencies.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install winter-fx
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { snow, garland, frost } from "winter-fx";
|
|
15
|
+
|
|
16
|
+
const stopSnow = snow();
|
|
17
|
+
const stopGarland = garland();
|
|
18
|
+
const stopFrost = frost();
|
|
19
|
+
|
|
20
|
+
// later, to remove the snow and everything it added
|
|
21
|
+
stopSnow();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Every effect returns a function that removes it. Calling that function again does nothing. Calling an effect twice adds two separate layers, each with its own stop function.
|
|
25
|
+
|
|
26
|
+
The effects sit over the page in a fixed layer. Clicks go through to the page underneath, and screen readers skip them.
|
|
27
|
+
|
|
28
|
+
## Effects
|
|
29
|
+
|
|
30
|
+
All settings are optional. Every effect also takes `zIndex`, any integer, which defaults to `9999`.
|
|
31
|
+
|
|
32
|
+
### snow
|
|
33
|
+
|
|
34
|
+
Snowflakes falling over the page, in three depth layers.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
snow({ color: "#ffffff", amount: 2, speed: 0.5 });
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
| Setting | Default | Valid values |
|
|
41
|
+
| -------- | -------------------------------- | ---------------------------------------------------------------------- |
|
|
42
|
+
| `color` | `"light-dark(#7f9cc0, #ffffff)"` | any CSS color |
|
|
43
|
+
| `amount` | `1` | a number greater than 0 and at most 3. Multiplies the number of flakes |
|
|
44
|
+
| `speed` | `1` | a number greater than 0 and at most 5. Multiplies the falling speed |
|
|
45
|
+
|
|
46
|
+
### garland
|
|
47
|
+
|
|
48
|
+
A string of lights across the top of the page. Each bulb twinkles on its own timing.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
garland({ colors: ["#ff4d4d", "#ffcc33"] });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
| Setting | Default | Valid values |
|
|
55
|
+
| -------- | ---------------------------------------------- | -------------------------------------------------------- |
|
|
56
|
+
| `colors` | `["#ff4d4d", "#ffcc33", "#4cd964", "#4da6ff"]` | a list of at least one CSS color. Bulbs cycle through it |
|
|
57
|
+
|
|
58
|
+
### frost
|
|
59
|
+
|
|
60
|
+
Ice crystals that grow in from the edges of the screen and leave the middle clear.
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
frost({ size: 1.5 });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
| Setting | Default | Valid values |
|
|
67
|
+
| ------- | -------------------------------- | ------------------------------------------------------------------------------- |
|
|
68
|
+
| `color` | `"light-dark(#6f93bd, #e4efff)"` | any CSS color |
|
|
69
|
+
| `size` | `1` | a number greater than 0 and at most 2. Multiplies how far the crystals reach in |
|
|
70
|
+
|
|
71
|
+
### Colors
|
|
72
|
+
|
|
73
|
+
Colors can be any CSS color, including variables like `"var(--brand)"`. The effects follow changes to them right away.
|
|
74
|
+
|
|
75
|
+
The default colors use `light-dark()`, which picks the light or dark value from the page's `color-scheme`. A dark site that doesn't set `color-scheme: dark` gets the light values, so set it or pass your own color.
|
|
76
|
+
|
|
77
|
+
## Wrong settings
|
|
78
|
+
|
|
79
|
+
A bad setting throws as soon as the effect is called, with a message that names the effect, the setting and the value it got:
|
|
80
|
+
|
|
81
|
+
```text
|
|
82
|
+
winter-fx: snow() setting "amount" must be a number greater than 0 and at most 3, got -1
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
A setting of the wrong type, or one the effect doesn't know, throws a `TypeError`. A value out of range throws a `RangeError`. A setting left out, `undefined` or `null` gets its default.
|
|
86
|
+
|
|
87
|
+
## Frameworks and server rendering
|
|
88
|
+
|
|
89
|
+
Importing winter-fx never touches the page, so it's safe in server-rendered apps like Next.js, Nuxt and Astro. Call the effects only in the browser, after the page has loaded. Called on the server, they throw.
|
|
90
|
+
|
|
91
|
+
Start an effect when your component mounts, and call its stop function when it unmounts. In React, the stop function is the effect's cleanup:
|
|
92
|
+
|
|
93
|
+
```jsx
|
|
94
|
+
import { useEffect } from "react";
|
|
95
|
+
import { snow } from "winter-fx";
|
|
96
|
+
|
|
97
|
+
export function Snow() {
|
|
98
|
+
useEffect(() => snow(), []);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Sites that replace the page's `<body>` without a reload, like Turbo, remove the effects with it. Call the stop function before the swap and start the effect again after it.
|
|
104
|
+
|
|
105
|
+
## Script tag
|
|
106
|
+
|
|
107
|
+
Sites without a build step can load winter-fx from a CDN:
|
|
108
|
+
|
|
109
|
+
```html
|
|
110
|
+
<script type="module">
|
|
111
|
+
import { snow } from "https://cdn.jsdelivr.net/npm/winter-fx@0.1/+esm";
|
|
112
|
+
snow();
|
|
113
|
+
</script>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Reduced motion
|
|
117
|
+
|
|
118
|
+
Visitors can ask their system for less motion. The effects follow that setting, and change while the page is open if it changes. No setting can turn this off.
|
|
119
|
+
|
|
120
|
+
| Effect | With reduced motion on |
|
|
121
|
+
| ------- | -------------------------------------------------------- |
|
|
122
|
+
| snow | Shows nothing, and comes back when the setting turns off |
|
|
123
|
+
| garland | Bulbs stay lit without twinkling |
|
|
124
|
+
| frost | Appears at once, without growing in |
|
|
125
|
+
|
|
126
|
+
## Browser support
|
|
127
|
+
|
|
128
|
+
Chrome and Edge 123, Firefox 120 and Safari 17.5, and later versions. These are the first with `light-dark()`, which lets the default colors follow dark mode.
|
|
129
|
+
|
|
130
|
+
Older versions also worked when this release was checked, back to Chromium 112, Firefox 111 and WebKit 16.4, the engine inside Safari. There the default colors stay light on dark pages, so pass your own color if that matters.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Random } from "../shared/random.ts";
|
|
2
|
+
export interface Point {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
}
|
|
6
|
+
export interface Line {
|
|
7
|
+
from: Point;
|
|
8
|
+
to: Point;
|
|
9
|
+
level: number;
|
|
10
|
+
}
|
|
11
|
+
export type Side = "top" | "bottom" | "left" | "right";
|
|
12
|
+
export declare const LEVELS = 2;
|
|
13
|
+
export declare function seededRandom(seed: number): Random;
|
|
14
|
+
export declare function growCrystal(start: Point, angle: number, length: number, level: number, random: Random): Line[];
|
|
15
|
+
export declare function waveOf(random: Random): (share: number) => number;
|
|
16
|
+
export declare function frostLines(width: number, height: number, size: number, seed: number): Record<Side, Line[]>;
|
|
17
|
+
export declare function pathData(lines: Line[]): string;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { between } from "../shared/random.js";
|
|
2
|
+
export const LEVELS = 2;
|
|
3
|
+
const BRANCH_SPOTS = 4;
|
|
4
|
+
const BRANCH_CHANCE = 0.6;
|
|
5
|
+
const BRANCH_ANGLE = Math.PI / 3;
|
|
6
|
+
const ANGLE_SPREAD = 0.15;
|
|
7
|
+
const BRANCH_SHARE = 0.42;
|
|
8
|
+
const TAPER = 0.7;
|
|
9
|
+
const MIN_FACTOR = 0.7;
|
|
10
|
+
const MAX_FACTOR = 1.3;
|
|
11
|
+
const WAVES = [
|
|
12
|
+
{ cycles: 3, height: 0.35 },
|
|
13
|
+
{ cycles: 7, height: 0.21 },
|
|
14
|
+
{ cycles: 17, height: 0.14 },
|
|
15
|
+
];
|
|
16
|
+
const MIN_LENGTH = 4;
|
|
17
|
+
const MAX_LENGTH = 10;
|
|
18
|
+
const MIN_SPACING = 5;
|
|
19
|
+
const MAX_SPACING = 11;
|
|
20
|
+
const TILT = 0.5;
|
|
21
|
+
const CORNER_BOOST = 0.8;
|
|
22
|
+
const TOP_WEIGHT = 0.7;
|
|
23
|
+
const BOTTOM_WEIGHT = 1.5;
|
|
24
|
+
export function seededRandom(seed) {
|
|
25
|
+
let state = seed;
|
|
26
|
+
return () => {
|
|
27
|
+
state = (state + 0x6d2b79f5) | 0;
|
|
28
|
+
let mixed = Math.imul(state ^ (state >>> 15), 1 | state);
|
|
29
|
+
mixed = (mixed + Math.imul(mixed ^ (mixed >>> 7), 61 | mixed)) ^ mixed;
|
|
30
|
+
return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function sideWeight(share) {
|
|
34
|
+
return TOP_WEIGHT + (BOTTOM_WEIGHT - TOP_WEIGHT) * share;
|
|
35
|
+
}
|
|
36
|
+
function edgesOf(width, height) {
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
side: "top",
|
|
40
|
+
length: width,
|
|
41
|
+
inward: Math.PI / 2,
|
|
42
|
+
start: (d) => ({ x: d, y: 0 }),
|
|
43
|
+
weight: () => TOP_WEIGHT,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
side: "bottom",
|
|
47
|
+
length: width,
|
|
48
|
+
inward: -Math.PI / 2,
|
|
49
|
+
start: (d) => ({ x: d, y: height }),
|
|
50
|
+
weight: () => BOTTOM_WEIGHT,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
side: "left",
|
|
54
|
+
length: height,
|
|
55
|
+
inward: 0,
|
|
56
|
+
start: (d) => ({ x: 0, y: d }),
|
|
57
|
+
weight: sideWeight,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
side: "right",
|
|
61
|
+
length: height,
|
|
62
|
+
inward: Math.PI,
|
|
63
|
+
start: (d) => ({ x: width, y: d }),
|
|
64
|
+
weight: sideWeight,
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
function pointAt(start, angle, distance) {
|
|
69
|
+
const x = start.x + Math.cos(angle) * distance;
|
|
70
|
+
const y = start.y + Math.sin(angle) * distance;
|
|
71
|
+
return { x, y };
|
|
72
|
+
}
|
|
73
|
+
export function growCrystal(start, angle, length, level, random) {
|
|
74
|
+
const lines = [
|
|
75
|
+
{ from: start, to: pointAt(start, angle, length), level },
|
|
76
|
+
];
|
|
77
|
+
if (level === LEVELS - 1) {
|
|
78
|
+
return lines;
|
|
79
|
+
}
|
|
80
|
+
for (let spot = 1; spot <= BRANCH_SPOTS; spot++) {
|
|
81
|
+
const share = spot / (BRANCH_SPOTS + 1);
|
|
82
|
+
const at = pointAt(start, angle, length * share);
|
|
83
|
+
for (const side of [1, -1]) {
|
|
84
|
+
const coin = random();
|
|
85
|
+
if (coin >= BRANCH_CHANCE) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const turn = BRANCH_ANGLE + between(random, -ANGLE_SPREAD, ANGLE_SPREAD);
|
|
89
|
+
const branchLength = length *
|
|
90
|
+
BRANCH_SHARE *
|
|
91
|
+
(1 - share * TAPER) *
|
|
92
|
+
between(random, MIN_FACTOR, MAX_FACTOR);
|
|
93
|
+
lines.push(...growCrystal(at, angle + side * turn, branchLength, level + 1, random));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return lines;
|
|
97
|
+
}
|
|
98
|
+
export function waveOf(random) {
|
|
99
|
+
const waves = [];
|
|
100
|
+
for (const wave of WAVES) {
|
|
101
|
+
waves.push({ cycles: wave.cycles, height: wave.height, phase: random() });
|
|
102
|
+
}
|
|
103
|
+
return (share) => {
|
|
104
|
+
let total = 1;
|
|
105
|
+
for (const wave of waves) {
|
|
106
|
+
total +=
|
|
107
|
+
wave.height *
|
|
108
|
+
Math.sin((share * wave.cycles + wave.phase) * 2 * Math.PI);
|
|
109
|
+
}
|
|
110
|
+
return total;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export function frostLines(width, height, size, seed) {
|
|
114
|
+
const edges = {
|
|
115
|
+
top: [],
|
|
116
|
+
bottom: [],
|
|
117
|
+
left: [],
|
|
118
|
+
right: [],
|
|
119
|
+
};
|
|
120
|
+
for (const [index, edge] of edgesOf(width, height).entries()) {
|
|
121
|
+
const random = seededRandom(seed + index);
|
|
122
|
+
const wave = waveOf(random);
|
|
123
|
+
const lines = edges[edge.side];
|
|
124
|
+
for (let d = between(random, 0, MAX_SPACING); d < edge.length; d += between(random, MIN_SPACING, MAX_SPACING)) {
|
|
125
|
+
const share = d / edge.length;
|
|
126
|
+
const fromMiddle = Math.abs(share - 0.5) * 2;
|
|
127
|
+
const length = size *
|
|
128
|
+
between(random, MIN_LENGTH, MAX_LENGTH) *
|
|
129
|
+
edge.weight(share) *
|
|
130
|
+
wave(share) *
|
|
131
|
+
(1 + CORNER_BOOST * fromMiddle ** 4);
|
|
132
|
+
const angle = edge.inward + between(random, -TILT, TILT);
|
|
133
|
+
lines.push(...growCrystal(edge.start(d), angle, length, 0, random));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return edges;
|
|
137
|
+
}
|
|
138
|
+
export function pathData(lines) {
|
|
139
|
+
let data = "";
|
|
140
|
+
for (const { from, to } of lines) {
|
|
141
|
+
data += `M${from.x.toFixed(1)} ${from.y.toFixed(1)}`;
|
|
142
|
+
data += `L${to.x.toFixed(1)} ${to.y.toFixed(1)}`;
|
|
143
|
+
}
|
|
144
|
+
return data;
|
|
145
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { watchReducedMotion } from "../shared/motion.js";
|
|
2
|
+
import { checkColor, checkInteger, checkPositive, readOptions, themeColor, } from "../shared/options.js";
|
|
3
|
+
import { assertBrowser, createOverlay } from "../shared/overlay.js";
|
|
4
|
+
import { svgElement, uniqueId } from "../shared/svg.js";
|
|
5
|
+
import { frostLines, LEVELS, pathData, } from "./crystals.js";
|
|
6
|
+
const DEFAULT_LIGHT_COLOR = "#6f93bd";
|
|
7
|
+
const DEFAULT_DARK_COLOR = "#e4efff";
|
|
8
|
+
const MAX_SIZE = 2;
|
|
9
|
+
const STROKE_WIDTHS = [0.8, 0.5];
|
|
10
|
+
const OPACITIES = [0.8, 0.6];
|
|
11
|
+
const CRUST_WIDTH = 7;
|
|
12
|
+
const CRUST_BLUR = 3.6;
|
|
13
|
+
const CRUST_OPACITY = 0.11;
|
|
14
|
+
const GRAIN_OPACITY = 0.6;
|
|
15
|
+
const GRAIN_TILE = 128;
|
|
16
|
+
const GLOW_SHARE = 30;
|
|
17
|
+
const GLOW_SPILL = 300;
|
|
18
|
+
const GROW_MS = 5000;
|
|
19
|
+
const GROW_EASING = "ease-out";
|
|
20
|
+
const REACH = 70;
|
|
21
|
+
const BOTTOM_REACH = 1.4;
|
|
22
|
+
const FADE = 20;
|
|
23
|
+
const PLACEMENTS = {
|
|
24
|
+
top: { pinned: ["top", "left"], outward: [0, -1], inward: "to bottom" },
|
|
25
|
+
bottom: { pinned: ["bottom", "left"], outward: [0, 1], inward: "to top" },
|
|
26
|
+
left: { pinned: ["bottom", "left"], outward: [-1, 0], inward: "to right" },
|
|
27
|
+
right: { pinned: ["bottom", "right"], outward: [1, 0], inward: "to left" },
|
|
28
|
+
};
|
|
29
|
+
const SIDES = ["top", "bottom", "left", "right"];
|
|
30
|
+
function createStroke(paint, width, opacity) {
|
|
31
|
+
const path = svgElement("path", {});
|
|
32
|
+
path.style.fill = "none";
|
|
33
|
+
path.style.stroke = paint;
|
|
34
|
+
path.style.strokeWidth = String(width);
|
|
35
|
+
path.style.strokeLinecap = "round";
|
|
36
|
+
path.style.opacity = String(opacity);
|
|
37
|
+
path.style.pointerEvents = "none";
|
|
38
|
+
return path;
|
|
39
|
+
}
|
|
40
|
+
function createGrain(color, grainId) {
|
|
41
|
+
const noise = svgElement("filter", {
|
|
42
|
+
id: `${grainId}-noise`,
|
|
43
|
+
x: 0,
|
|
44
|
+
y: 0,
|
|
45
|
+
width: "100%",
|
|
46
|
+
height: "100%",
|
|
47
|
+
});
|
|
48
|
+
noise.append(svgElement("feTurbulence", {
|
|
49
|
+
type: "fractalNoise",
|
|
50
|
+
baseFrequency: 0.9,
|
|
51
|
+
numOctaves: 2,
|
|
52
|
+
seed: 4,
|
|
53
|
+
stitchTiles: "stitch",
|
|
54
|
+
}), svgElement("feColorMatrix", {
|
|
55
|
+
type: "matrix",
|
|
56
|
+
values: "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 -4.6",
|
|
57
|
+
}), svgElement("feComposite", { in: "SourceGraphic", operator: "in" }));
|
|
58
|
+
const specks = svgElement("rect", { width: GRAIN_TILE, height: GRAIN_TILE });
|
|
59
|
+
specks.style.fill = color;
|
|
60
|
+
specks.style.filter = `url(#${grainId}-noise)`;
|
|
61
|
+
const grain = svgElement("pattern", {
|
|
62
|
+
id: grainId,
|
|
63
|
+
width: GRAIN_TILE,
|
|
64
|
+
height: GRAIN_TILE,
|
|
65
|
+
patternUnits: "userSpaceOnUse",
|
|
66
|
+
});
|
|
67
|
+
grain.append(specks);
|
|
68
|
+
return [noise, grain];
|
|
69
|
+
}
|
|
70
|
+
function createBlock() {
|
|
71
|
+
const block = document.createElement("div");
|
|
72
|
+
block.style.position = "absolute";
|
|
73
|
+
block.style.width = "auto";
|
|
74
|
+
block.style.height = "auto";
|
|
75
|
+
block.style.pointerEvents = "none";
|
|
76
|
+
return block;
|
|
77
|
+
}
|
|
78
|
+
function createGlow(side, color) {
|
|
79
|
+
const glow = createBlock();
|
|
80
|
+
glow.style.inset = `-${GLOW_SPILL}px`;
|
|
81
|
+
glow.style[side] = "0";
|
|
82
|
+
glow.style.boxShadow = `inset 0 -12px 44px color-mix(in srgb, ${color} ${GLOW_SHARE}%, transparent)`;
|
|
83
|
+
return glow;
|
|
84
|
+
}
|
|
85
|
+
function createPiece(side, color) {
|
|
86
|
+
const blurId = uniqueId("frost-blur");
|
|
87
|
+
const grainId = uniqueId("frost-grain");
|
|
88
|
+
const blur = svgElement("filter", {
|
|
89
|
+
id: blurId,
|
|
90
|
+
filterUnits: "userSpaceOnUse",
|
|
91
|
+
});
|
|
92
|
+
blur.append(svgElement("feGaussianBlur", { stdDeviation: CRUST_BLUR }));
|
|
93
|
+
const defs = svgElement("defs", {});
|
|
94
|
+
defs.append(blur, ...createGrain(color, grainId));
|
|
95
|
+
const blurred = createStroke(color, CRUST_WIDTH, CRUST_OPACITY);
|
|
96
|
+
blurred.style.filter = `url(#${blurId})`;
|
|
97
|
+
const grainy = createStroke(`url(#${grainId})`, CRUST_WIDTH, GRAIN_OPACITY);
|
|
98
|
+
const levels = Array.from({ length: LEVELS }, (_, level) => createStroke(color, STROKE_WIDTHS[level] ?? 0.5, OPACITIES[level] ?? 0.6));
|
|
99
|
+
const svg = svgElement("svg", {});
|
|
100
|
+
svg.style.position = "absolute";
|
|
101
|
+
svg.style.top = "0";
|
|
102
|
+
svg.style.left = "0";
|
|
103
|
+
svg.style.display = "block";
|
|
104
|
+
svg.style.pointerEvents = "none";
|
|
105
|
+
svg.append(defs, blurred, grainy, ...levels);
|
|
106
|
+
const inner = createBlock();
|
|
107
|
+
inner.style.inset = "0";
|
|
108
|
+
inner.append(createGlow(side, color), svg);
|
|
109
|
+
const { pinned, inward } = PLACEMENTS[side];
|
|
110
|
+
const box = document.createElement("div");
|
|
111
|
+
box.style.position = "fixed";
|
|
112
|
+
box.style[pinned[0]] = "0";
|
|
113
|
+
box.style[pinned[1]] = "0";
|
|
114
|
+
box.style.overflow = "hidden";
|
|
115
|
+
box.style.pointerEvents = "none";
|
|
116
|
+
box.style.maskImage = `linear-gradient(${inward}, black calc(100% - ${FADE}px), transparent)`;
|
|
117
|
+
box.style.willChange = "transform";
|
|
118
|
+
box.append(inner);
|
|
119
|
+
return { side, box, inner, svg, blur, crust: [blurred, grainy], levels };
|
|
120
|
+
}
|
|
121
|
+
function thickness(side, size) {
|
|
122
|
+
const reach = REACH * Math.max(size, 1);
|
|
123
|
+
return (side === "bottom" ? reach * BOTTOM_REACH : reach) + FADE;
|
|
124
|
+
}
|
|
125
|
+
function pieceArea(side, bounds, size) {
|
|
126
|
+
const band = thickness(side, size);
|
|
127
|
+
const { width, height } = bounds;
|
|
128
|
+
if (side === "top") {
|
|
129
|
+
return { x: 0, y: 0, width, height: band };
|
|
130
|
+
}
|
|
131
|
+
if (side === "bottom") {
|
|
132
|
+
return { x: 0, y: height - band, width, height: band };
|
|
133
|
+
}
|
|
134
|
+
if (side === "left") {
|
|
135
|
+
return { x: 0, y: 0, width: band, height };
|
|
136
|
+
}
|
|
137
|
+
return { x: width - band, y: 0, width: band, height };
|
|
138
|
+
}
|
|
139
|
+
function drawPiece(piece, lines, area) {
|
|
140
|
+
const { x, y, width, height } = area;
|
|
141
|
+
piece.box.style.width = `${width}px`;
|
|
142
|
+
piece.box.style.height = `${height}px`;
|
|
143
|
+
piece.svg.style.width = `${width}px`;
|
|
144
|
+
piece.svg.style.height = `${height}px`;
|
|
145
|
+
piece.svg.setAttribute("viewBox", `${x} ${y} ${width} ${height}`);
|
|
146
|
+
piece.blur.setAttribute("x", String(x));
|
|
147
|
+
piece.blur.setAttribute("y", String(y));
|
|
148
|
+
piece.blur.setAttribute("width", String(width));
|
|
149
|
+
piece.blur.setAttribute("height", String(height));
|
|
150
|
+
const all = pathData(lines);
|
|
151
|
+
for (const path of piece.crust) {
|
|
152
|
+
path.setAttribute("d", all);
|
|
153
|
+
}
|
|
154
|
+
for (const [level, path] of piece.levels.entries()) {
|
|
155
|
+
const own = lines.filter((line) => line.level === level);
|
|
156
|
+
path.setAttribute("d", pathData(own));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function screenBounds() {
|
|
160
|
+
const { width, height } = screen;
|
|
161
|
+
if (width < height && matchMedia("(orientation: landscape)").matches) {
|
|
162
|
+
return { width: height, height: width };
|
|
163
|
+
}
|
|
164
|
+
return { width, height };
|
|
165
|
+
}
|
|
166
|
+
function drawingBounds(layer) {
|
|
167
|
+
const whole = screenBounds();
|
|
168
|
+
return {
|
|
169
|
+
width: Math.max(whole.width, layer.clientWidth),
|
|
170
|
+
height: Math.max(whole.height, layer.clientHeight),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function slide(element, x, y) {
|
|
174
|
+
const hidden = `translate(${x * 100}%, ${y * 100}%)`;
|
|
175
|
+
return element.animate([{ transform: hidden }, { transform: "none" }], {
|
|
176
|
+
duration: GROW_MS,
|
|
177
|
+
easing: GROW_EASING,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function startGrowth(piece) {
|
|
181
|
+
const [x, y] = PLACEMENTS[piece.side].outward;
|
|
182
|
+
return [slide(piece.box, x, y), slide(piece.inner, -x, -y)];
|
|
183
|
+
}
|
|
184
|
+
export function frost(options) {
|
|
185
|
+
assertBrowser("frost");
|
|
186
|
+
const settings = readOptions("frost", options, ["color", "size", "zIndex"]);
|
|
187
|
+
const color = checkColor("frost", "color", settings.color ?? themeColor(DEFAULT_LIGHT_COLOR, DEFAULT_DARK_COLOR));
|
|
188
|
+
const size = checkPositive("frost", "size", settings.size ?? 1, MAX_SIZE);
|
|
189
|
+
const zIndex = checkInteger("frost", "zIndex", settings.zIndex ?? 9999);
|
|
190
|
+
const layer = createOverlay(zIndex);
|
|
191
|
+
const pieces = SIDES.map((side) => createPiece(side, color));
|
|
192
|
+
layer.append(...pieces.map((piece) => piece.box));
|
|
193
|
+
const seed = Math.floor(Math.random() * 2 ** 32);
|
|
194
|
+
function draw(bounds) {
|
|
195
|
+
const edges = frostLines(bounds.width, bounds.height, size, seed);
|
|
196
|
+
for (const piece of pieces) {
|
|
197
|
+
const area = pieceArea(piece.side, bounds, size);
|
|
198
|
+
drawPiece(piece, edges[piece.side], area);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let drawn = drawingBounds(layer);
|
|
202
|
+
draw(drawn);
|
|
203
|
+
const controller = new AbortController();
|
|
204
|
+
const growths = pieces.flatMap(startGrowth);
|
|
205
|
+
watchReducedMotion((reduced) => {
|
|
206
|
+
if (!reduced) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
for (const growth of growths) {
|
|
210
|
+
growth.finish();
|
|
211
|
+
}
|
|
212
|
+
}, controller.signal);
|
|
213
|
+
const observer = new ResizeObserver(() => {
|
|
214
|
+
const bounds = drawingBounds(layer);
|
|
215
|
+
if (bounds.width === drawn.width && bounds.height === drawn.height) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
drawn = bounds;
|
|
219
|
+
draw(drawn);
|
|
220
|
+
});
|
|
221
|
+
observer.observe(layer);
|
|
222
|
+
return () => {
|
|
223
|
+
controller.abort();
|
|
224
|
+
observer.disconnect();
|
|
225
|
+
for (const growth of growths) {
|
|
226
|
+
growth.cancel();
|
|
227
|
+
}
|
|
228
|
+
layer.remove();
|
|
229
|
+
};
|
|
230
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { watchReducedMotion } from "../shared/motion.js";
|
|
2
|
+
import { checkColorList, checkInteger, readOptions, } from "../shared/options.js";
|
|
3
|
+
import { assertBrowser, createOverlay } from "../shared/overlay.js";
|
|
4
|
+
import { svgElement, uniqueId } from "../shared/svg.js";
|
|
5
|
+
import { garlandLayout } from "./layout.js";
|
|
6
|
+
const DEFAULT_COLORS = ["#ff4d4d", "#ffcc33", "#4cd964", "#4da6ff"];
|
|
7
|
+
const STRIP_HEIGHT = 48;
|
|
8
|
+
const WIRE_COLOR = "#2b2b2b";
|
|
9
|
+
const CAP_COLOR = "#555555";
|
|
10
|
+
const GLOW_BLUR = 4;
|
|
11
|
+
const SLOWEST_TWINKLE_MS = 1500;
|
|
12
|
+
const DIM_OPACITY = 0.35;
|
|
13
|
+
function colorAt(colors, index) {
|
|
14
|
+
const color = colors[index % colors.length];
|
|
15
|
+
if (color === undefined) {
|
|
16
|
+
throw new Error("winter-fx: garland() needs at least one color");
|
|
17
|
+
}
|
|
18
|
+
return color;
|
|
19
|
+
}
|
|
20
|
+
function createBulb(point, color, glowId) {
|
|
21
|
+
const bulb = svgElement("g", {
|
|
22
|
+
transform: `translate(${point.x} ${point.y})`,
|
|
23
|
+
});
|
|
24
|
+
const glow = svgElement("circle", { cy: 8, r: 9 });
|
|
25
|
+
const body = svgElement("ellipse", { cy: 8, rx: 4, ry: 6 });
|
|
26
|
+
const cap = svgElement("rect", { x: -2, width: 4, height: 3 });
|
|
27
|
+
glow.style.fill = color;
|
|
28
|
+
glow.style.opacity = "0.5";
|
|
29
|
+
glow.style.filter = `url(#${glowId})`;
|
|
30
|
+
body.style.fill = color;
|
|
31
|
+
cap.style.fill = CAP_COLOR;
|
|
32
|
+
bulb.append(glow, body, cap);
|
|
33
|
+
return bulb;
|
|
34
|
+
}
|
|
35
|
+
function twinkle(bulb) {
|
|
36
|
+
const duration = SLOWEST_TWINKLE_MS + Math.random() * SLOWEST_TWINKLE_MS;
|
|
37
|
+
return bulb.animate([{ opacity: 1 }, { opacity: DIM_OPACITY }], {
|
|
38
|
+
duration,
|
|
39
|
+
delay: Math.random() * duration,
|
|
40
|
+
iterations: Number.POSITIVE_INFINITY,
|
|
41
|
+
direction: "alternate",
|
|
42
|
+
easing: "ease-in-out",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function garland(options) {
|
|
46
|
+
assertBrowser("garland");
|
|
47
|
+
const settings = readOptions("garland", options, ["colors", "zIndex"]);
|
|
48
|
+
const colors = checkColorList("garland", "colors", settings.colors ?? DEFAULT_COLORS);
|
|
49
|
+
const zIndex = checkInteger("garland", "zIndex", settings.zIndex ?? 9999);
|
|
50
|
+
const layer = createOverlay(zIndex);
|
|
51
|
+
const strip = svgElement("svg", {});
|
|
52
|
+
strip.style.display = "block";
|
|
53
|
+
strip.style.width = "100%";
|
|
54
|
+
strip.style.height = `${STRIP_HEIGHT}px`;
|
|
55
|
+
const glowId = uniqueId("glow");
|
|
56
|
+
const glowFilter = svgElement("filter", {
|
|
57
|
+
id: glowId,
|
|
58
|
+
x: "-100%",
|
|
59
|
+
y: "-100%",
|
|
60
|
+
width: "300%",
|
|
61
|
+
height: "300%",
|
|
62
|
+
});
|
|
63
|
+
glowFilter.append(svgElement("feGaussianBlur", { stdDeviation: GLOW_BLUR }));
|
|
64
|
+
const defs = svgElement("defs", {});
|
|
65
|
+
defs.append(glowFilter);
|
|
66
|
+
const wire = svgElement("path", {});
|
|
67
|
+
wire.style.fill = "none";
|
|
68
|
+
wire.style.stroke = WIRE_COLOR;
|
|
69
|
+
wire.style.strokeWidth = "1.5";
|
|
70
|
+
const lights = svgElement("g", {});
|
|
71
|
+
strip.append(defs, wire, lights);
|
|
72
|
+
layer.append(strip);
|
|
73
|
+
let width = 0;
|
|
74
|
+
let bulbs = [];
|
|
75
|
+
function hang() {
|
|
76
|
+
width = layer.clientWidth;
|
|
77
|
+
const layout = garlandLayout(width);
|
|
78
|
+
wire.setAttribute("d", layout.wire);
|
|
79
|
+
bulbs = layout.bulbs.map((point, index) => createBulb(point, colorAt(colors, index), glowId));
|
|
80
|
+
lights.replaceChildren(...bulbs);
|
|
81
|
+
}
|
|
82
|
+
hang();
|
|
83
|
+
const controller = new AbortController();
|
|
84
|
+
let twinkles = [];
|
|
85
|
+
function play() {
|
|
86
|
+
twinkles = bulbs.map((bulb) => twinkle(bulb));
|
|
87
|
+
}
|
|
88
|
+
function pause() {
|
|
89
|
+
for (const t of twinkles) {
|
|
90
|
+
t.cancel();
|
|
91
|
+
}
|
|
92
|
+
twinkles = [];
|
|
93
|
+
}
|
|
94
|
+
watchReducedMotion((reduced) => {
|
|
95
|
+
if (reduced)
|
|
96
|
+
pause();
|
|
97
|
+
else
|
|
98
|
+
play();
|
|
99
|
+
}, controller.signal);
|
|
100
|
+
const observer = new ResizeObserver(() => {
|
|
101
|
+
if (layer.clientWidth === width) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const wasTwinkling = twinkles.length > 0;
|
|
105
|
+
pause();
|
|
106
|
+
hang();
|
|
107
|
+
if (wasTwinkling)
|
|
108
|
+
play();
|
|
109
|
+
});
|
|
110
|
+
observer.observe(layer);
|
|
111
|
+
return () => {
|
|
112
|
+
controller.abort();
|
|
113
|
+
observer.disconnect();
|
|
114
|
+
pause();
|
|
115
|
+
layer.remove();
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const HOOK_SPACING = 120;
|
|
2
|
+
const SAG = 16;
|
|
3
|
+
const BULB_SPOTS = [1 / 6, 1 / 2, 5 / 6];
|
|
4
|
+
export function garlandLayout(width) {
|
|
5
|
+
const swoops = Math.max(1, Math.round(width / HOOK_SPACING));
|
|
6
|
+
let wire = "M 0 0";
|
|
7
|
+
const bulbs = [];
|
|
8
|
+
for (let i = 0; i < swoops; i += 1) {
|
|
9
|
+
const start = (i * width) / swoops;
|
|
10
|
+
const end = ((i + 1) * width) / swoops;
|
|
11
|
+
wire += ` Q ${(start + end) / 2} ${2 * SAG} ${end} 0`;
|
|
12
|
+
for (const t of BULB_SPOTS) {
|
|
13
|
+
bulbs.push({ x: start + t * (end - start), y: 4 * SAG * t * (1 - t) });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return { wire, bulbs };
|
|
17
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function watchReducedMotion(onChange: (reduced: boolean) => void, signal: AbortSignal): void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function checkInteger(effect: string, name: string, value: unknown): number;
|
|
2
|
+
export declare function checkPositive(effect: string, name: string, value: unknown, max?: number): number;
|
|
3
|
+
export declare function checkColor(effect: string, name: string, value: unknown): string;
|
|
4
|
+
export declare function themeColor(light: string, dark: string): string;
|
|
5
|
+
export declare function checkColorList(effect: string, name: string, value: unknown): string[];
|
|
6
|
+
export interface CommonOptions {
|
|
7
|
+
zIndex?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function readOptions(effect: string, options: unknown, allowed: readonly string[]): Record<string, unknown>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const CSS_WIDE_KEYWORDS = [
|
|
2
|
+
"inherit",
|
|
3
|
+
"initial",
|
|
4
|
+
"unset",
|
|
5
|
+
"revert",
|
|
6
|
+
"revert-layer",
|
|
7
|
+
];
|
|
8
|
+
function show(value) {
|
|
9
|
+
if (typeof value === "string" || Array.isArray(value)) {
|
|
10
|
+
return JSON.stringify(value);
|
|
11
|
+
}
|
|
12
|
+
return String(value);
|
|
13
|
+
}
|
|
14
|
+
function settingMessage(effect, name, rule, value) {
|
|
15
|
+
return `winter-fx: ${effect}() setting "${name}" must ${rule}, got ${show(value)}`;
|
|
16
|
+
}
|
|
17
|
+
export function checkInteger(effect, name, value) {
|
|
18
|
+
if (typeof value !== "number") {
|
|
19
|
+
throw new TypeError(settingMessage(effect, name, "be a number", value));
|
|
20
|
+
}
|
|
21
|
+
if (!Number.isInteger(value)) {
|
|
22
|
+
throw new RangeError(settingMessage(effect, name, "be an integer", value));
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
export function checkPositive(effect, name, value, max) {
|
|
27
|
+
if (typeof value !== "number") {
|
|
28
|
+
throw new TypeError(settingMessage(effect, name, "be a number", value));
|
|
29
|
+
}
|
|
30
|
+
let rule = "be a number greater than 0";
|
|
31
|
+
if (max !== undefined) {
|
|
32
|
+
rule += ` and at most ${max}`;
|
|
33
|
+
}
|
|
34
|
+
if (!Number.isFinite(value) ||
|
|
35
|
+
value <= 0 ||
|
|
36
|
+
(max !== undefined && value > max)) {
|
|
37
|
+
throw new RangeError(settingMessage(effect, name, rule, value));
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
export function checkColor(effect, name, value) {
|
|
42
|
+
if (typeof value !== "string") {
|
|
43
|
+
throw new TypeError(settingMessage(effect, name, "be a string", value));
|
|
44
|
+
}
|
|
45
|
+
const cleaned = value.trim().toLowerCase();
|
|
46
|
+
if (CSS_WIDE_KEYWORDS.includes(cleaned)) {
|
|
47
|
+
throw new RangeError(settingMessage(effect, name, "be a CSS color", value));
|
|
48
|
+
}
|
|
49
|
+
if (!CSS.supports("color", value)) {
|
|
50
|
+
throw new RangeError(settingMessage(effect, name, "be a CSS color", value));
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
export function themeColor(light, dark) {
|
|
55
|
+
const both = `light-dark(${light}, ${dark})`;
|
|
56
|
+
return CSS.supports("color", both) ? both : light;
|
|
57
|
+
}
|
|
58
|
+
export function checkColorList(effect, name, value) {
|
|
59
|
+
if (!Array.isArray(value)) {
|
|
60
|
+
throw new TypeError(settingMessage(effect, name, "be an array", value));
|
|
61
|
+
}
|
|
62
|
+
if (value.length === 0) {
|
|
63
|
+
throw new RangeError(settingMessage(effect, name, "have at least one color", value));
|
|
64
|
+
}
|
|
65
|
+
return Array.from(value, (color, index) => checkColor(effect, `${name}[${index}]`, color));
|
|
66
|
+
}
|
|
67
|
+
export function readOptions(effect, options, allowed) {
|
|
68
|
+
if (options === undefined) {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
if (typeof options !== "object" ||
|
|
72
|
+
options === null ||
|
|
73
|
+
Array.isArray(options)) {
|
|
74
|
+
throw new TypeError(`winter-fx: ${effect}() settings must be an object, got ${show(options)}`);
|
|
75
|
+
}
|
|
76
|
+
for (const key of Object.keys(options)) {
|
|
77
|
+
if (!allowed.includes(key)) {
|
|
78
|
+
throw new TypeError(`winter-fx: ${effect}() has no setting "${key}"`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return options;
|
|
82
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function assertBrowser(effect) {
|
|
2
|
+
if (typeof document === "undefined") {
|
|
3
|
+
throw new Error(`winter-fx: ${effect}() must run in a browser`);
|
|
4
|
+
}
|
|
5
|
+
if (document.body === null) {
|
|
6
|
+
throw new Error(`winter-fx: ${effect}() needs document.body, call it after the page has loaded`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function createOverlay(zIndex) {
|
|
10
|
+
const overlay = document.createElement("div");
|
|
11
|
+
overlay.style.position = "fixed";
|
|
12
|
+
overlay.style.inset = "0";
|
|
13
|
+
overlay.style.pointerEvents = "none";
|
|
14
|
+
overlay.style.zIndex = String(zIndex);
|
|
15
|
+
overlay.setAttribute("aria-hidden", "true");
|
|
16
|
+
document.body.append(overlay);
|
|
17
|
+
return overlay;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Stop = () => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
2
|
+
export function svgElement(tag, attributes) {
|
|
3
|
+
const element = document.createElementNS(SVG_NAMESPACE, tag);
|
|
4
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
5
|
+
element.setAttribute(name, String(value));
|
|
6
|
+
}
|
|
7
|
+
return element;
|
|
8
|
+
}
|
|
9
|
+
let lastId = 0;
|
|
10
|
+
export function uniqueId(name) {
|
|
11
|
+
lastId += 1;
|
|
12
|
+
return `winter-fx-${name}-${lastId}`;
|
|
13
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Random } from "../shared/random.ts";
|
|
2
|
+
export interface Bounds {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function flakeCount(bounds: Bounds, amount: number): number;
|
|
7
|
+
export interface Flake {
|
|
8
|
+
readonly x: number;
|
|
9
|
+
readonly y: number;
|
|
10
|
+
readonly radius: number;
|
|
11
|
+
readonly speed: number;
|
|
12
|
+
readonly opacity: number;
|
|
13
|
+
readonly phase: number;
|
|
14
|
+
readonly swaySpeed: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function createFlake(bounds: Bounds, random: Random): Flake;
|
|
17
|
+
export declare function createFlakes(count: number, bounds: Bounds, random: Random): Flake[];
|
|
18
|
+
export declare function swayOffset(flake: Flake): number;
|
|
19
|
+
export declare function elapsedSeconds(previous: number | undefined, now: number): number;
|
|
20
|
+
export declare function moveFlake(flake: Flake, seconds: number, speedFactor: number, bounds: Bounds, random: Random): Flake;
|
|
21
|
+
export declare function resizeFlakes(flakes: Flake[], bounds: Bounds, amount: number, random: Random): Flake[];
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { between } from "../shared/random.js";
|
|
2
|
+
const AREA_PER_FLAKE = 5300;
|
|
3
|
+
export function flakeCount(bounds, amount) {
|
|
4
|
+
return Math.round(((bounds.width * bounds.height) / AREA_PER_FLAKE) * amount);
|
|
5
|
+
}
|
|
6
|
+
const BACK = {
|
|
7
|
+
minRadius: 1,
|
|
8
|
+
maxRadius: 1.5,
|
|
9
|
+
minSpeed: 20,
|
|
10
|
+
maxSpeed: 40,
|
|
11
|
+
opacity: 0.4,
|
|
12
|
+
};
|
|
13
|
+
const MIDDLE = {
|
|
14
|
+
minRadius: 1.5,
|
|
15
|
+
maxRadius: 2.5,
|
|
16
|
+
minSpeed: 40,
|
|
17
|
+
maxSpeed: 70,
|
|
18
|
+
opacity: 0.7,
|
|
19
|
+
};
|
|
20
|
+
const FRONT = {
|
|
21
|
+
minRadius: 2.5,
|
|
22
|
+
maxRadius: 4,
|
|
23
|
+
minSpeed: 70,
|
|
24
|
+
maxSpeed: 110,
|
|
25
|
+
opacity: 0.95,
|
|
26
|
+
};
|
|
27
|
+
function pickLayer(pick) {
|
|
28
|
+
if (pick < 0.5) {
|
|
29
|
+
return BACK;
|
|
30
|
+
}
|
|
31
|
+
else if (pick < 0.8) {
|
|
32
|
+
return MIDDLE;
|
|
33
|
+
}
|
|
34
|
+
return FRONT;
|
|
35
|
+
}
|
|
36
|
+
export function createFlake(bounds, random) {
|
|
37
|
+
const layer = pickLayer(random());
|
|
38
|
+
return {
|
|
39
|
+
x: random() * bounds.width,
|
|
40
|
+
y: random() * bounds.height,
|
|
41
|
+
radius: between(random, layer.minRadius, layer.maxRadius),
|
|
42
|
+
speed: between(random, layer.minSpeed, layer.maxSpeed),
|
|
43
|
+
opacity: layer.opacity,
|
|
44
|
+
phase: random() * 2 * Math.PI,
|
|
45
|
+
swaySpeed: 0.5 + random(),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function createFlakes(count, bounds, random) {
|
|
49
|
+
return Array.from({ length: count }, () => createFlake(bounds, random));
|
|
50
|
+
}
|
|
51
|
+
const SWAY_PX = 8;
|
|
52
|
+
export function swayOffset(flake) {
|
|
53
|
+
return Math.sin(flake.phase) * SWAY_PX;
|
|
54
|
+
}
|
|
55
|
+
const MAX_STEP_SECONDS = 0.1;
|
|
56
|
+
export function elapsedSeconds(previous, now) {
|
|
57
|
+
if (previous === undefined)
|
|
58
|
+
return 0;
|
|
59
|
+
return Math.min((now - previous) / 1000, MAX_STEP_SECONDS);
|
|
60
|
+
}
|
|
61
|
+
export function moveFlake(flake, seconds, speedFactor, bounds, random) {
|
|
62
|
+
const newY = flake.y + speedFactor * seconds * flake.speed;
|
|
63
|
+
if (newY - flake.radius > bounds.height) {
|
|
64
|
+
const fresh = createFlake(bounds, random);
|
|
65
|
+
return { ...fresh, y: -fresh.radius };
|
|
66
|
+
}
|
|
67
|
+
return { ...flake, y: newY, phase: flake.phase + flake.swaySpeed * seconds };
|
|
68
|
+
}
|
|
69
|
+
export function resizeFlakes(flakes, bounds, amount, random) {
|
|
70
|
+
const count = flakeCount(bounds, amount);
|
|
71
|
+
const firstFlakes = flakes.slice(0, count);
|
|
72
|
+
const keptFlakes = firstFlakes.map((flake) => {
|
|
73
|
+
if (flake.x >= bounds.width) {
|
|
74
|
+
return { ...flake, x: random() * bounds.width };
|
|
75
|
+
}
|
|
76
|
+
return flake;
|
|
77
|
+
});
|
|
78
|
+
const newFlakes = createFlakes(count - keptFlakes.length, bounds, random);
|
|
79
|
+
return [...keptFlakes, ...newFlakes];
|
|
80
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type CommonOptions } from "../shared/options.ts";
|
|
2
|
+
import type { Stop } from "../shared/stop.ts";
|
|
3
|
+
export interface SnowOptions extends CommonOptions {
|
|
4
|
+
color?: string;
|
|
5
|
+
amount?: number;
|
|
6
|
+
speed?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function snow(options?: SnowOptions): Stop;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { watchReducedMotion } from "../shared/motion.js";
|
|
2
|
+
import { checkColor, checkInteger, checkPositive, readOptions, themeColor, } from "../shared/options.js";
|
|
3
|
+
import { assertBrowser, createOverlay } from "../shared/overlay.js";
|
|
4
|
+
import { createFlakes, elapsedSeconds, flakeCount, moveFlake, resizeFlakes, swayOffset, } from "./flakes.js";
|
|
5
|
+
const DEFAULT_LIGHT_COLOR = "#7f9cc0";
|
|
6
|
+
const DEFAULT_DARK_COLOR = "#ffffff";
|
|
7
|
+
const MAX_PIXEL_RATIO = 2;
|
|
8
|
+
const FADE_IN_MS = 1000;
|
|
9
|
+
const MAX_AMOUNT = 3;
|
|
10
|
+
const MAX_SPEED = 5;
|
|
11
|
+
function getContext2d(canvas) {
|
|
12
|
+
const context = canvas.getContext("2d");
|
|
13
|
+
if (context === null) {
|
|
14
|
+
throw new Error("winter-fx: snow() needs a browser that can draw on a canvas");
|
|
15
|
+
}
|
|
16
|
+
return context;
|
|
17
|
+
}
|
|
18
|
+
function fitCanvas(canvas, context) {
|
|
19
|
+
const ratio = Math.min(devicePixelRatio, MAX_PIXEL_RATIO);
|
|
20
|
+
const bounds = { width: canvas.clientWidth, height: canvas.clientHeight };
|
|
21
|
+
canvas.width = Math.round(bounds.width * ratio);
|
|
22
|
+
canvas.height = Math.round(bounds.height * ratio);
|
|
23
|
+
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
24
|
+
return bounds;
|
|
25
|
+
}
|
|
26
|
+
function drawFlakes(context, flakes, bounds, color) {
|
|
27
|
+
context.clearRect(0, 0, bounds.width, bounds.height);
|
|
28
|
+
context.fillStyle = color;
|
|
29
|
+
for (const flake of flakes) {
|
|
30
|
+
context.globalAlpha = flake.opacity;
|
|
31
|
+
context.beginPath();
|
|
32
|
+
context.arc(flake.x + swayOffset(flake), flake.y, flake.radius, 0, 2 * Math.PI);
|
|
33
|
+
context.fill();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function snow(options) {
|
|
37
|
+
assertBrowser("snow");
|
|
38
|
+
const settings = readOptions("snow", options, [
|
|
39
|
+
"color",
|
|
40
|
+
"amount",
|
|
41
|
+
"speed",
|
|
42
|
+
"zIndex",
|
|
43
|
+
]);
|
|
44
|
+
const color = checkColor("snow", "color", settings.color ?? themeColor(DEFAULT_LIGHT_COLOR, DEFAULT_DARK_COLOR));
|
|
45
|
+
const amount = checkPositive("snow", "amount", settings.amount ?? 1, MAX_AMOUNT);
|
|
46
|
+
const speed = checkPositive("snow", "speed", settings.speed ?? 1, MAX_SPEED);
|
|
47
|
+
const zIndex = checkInteger("snow", "zIndex", settings.zIndex ?? 9999);
|
|
48
|
+
const canvas = document.createElement("canvas");
|
|
49
|
+
const context = getContext2d(canvas);
|
|
50
|
+
canvas.style.display = "block";
|
|
51
|
+
canvas.style.width = "100%";
|
|
52
|
+
canvas.style.height = "100%";
|
|
53
|
+
canvas.style.color = color;
|
|
54
|
+
const layer = createOverlay(zIndex);
|
|
55
|
+
layer.append(canvas);
|
|
56
|
+
let bounds = fitCanvas(canvas, context);
|
|
57
|
+
const controller = new AbortController();
|
|
58
|
+
let flakes = createFlakes(flakeCount(bounds, amount), bounds, Math.random);
|
|
59
|
+
let last;
|
|
60
|
+
let frame;
|
|
61
|
+
function tick(now) {
|
|
62
|
+
const scheduled = frame;
|
|
63
|
+
// firefox 111 can run the reduced motion listener inside this read, so the checks come after it
|
|
64
|
+
const color = getComputedStyle(canvas).color;
|
|
65
|
+
if (frame !== scheduled || frame === undefined || !canvas.isConnected) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const seconds = elapsedSeconds(last, now);
|
|
69
|
+
last = now;
|
|
70
|
+
flakes = flakes.map((flake) => moveFlake(flake, seconds, speed, bounds, Math.random));
|
|
71
|
+
drawFlakes(context, flakes, bounds, color);
|
|
72
|
+
frame = requestAnimationFrame(tick);
|
|
73
|
+
}
|
|
74
|
+
function play() {
|
|
75
|
+
last = undefined;
|
|
76
|
+
layer.animate([{ opacity: 0 }, { opacity: 1 }], FADE_IN_MS);
|
|
77
|
+
frame = requestAnimationFrame(tick);
|
|
78
|
+
}
|
|
79
|
+
function pause() {
|
|
80
|
+
if (frame !== undefined) {
|
|
81
|
+
cancelAnimationFrame(frame);
|
|
82
|
+
frame = undefined;
|
|
83
|
+
}
|
|
84
|
+
context.clearRect(0, 0, bounds.width, bounds.height);
|
|
85
|
+
}
|
|
86
|
+
watchReducedMotion((reduced) => {
|
|
87
|
+
if (reduced === true)
|
|
88
|
+
pause();
|
|
89
|
+
else
|
|
90
|
+
play();
|
|
91
|
+
}, controller.signal);
|
|
92
|
+
addEventListener("resize", () => {
|
|
93
|
+
bounds = fitCanvas(canvas, context);
|
|
94
|
+
flakes = resizeFlakes(flakes, bounds, amount, Math.random);
|
|
95
|
+
}, { signal: controller.signal });
|
|
96
|
+
return () => {
|
|
97
|
+
controller.abort();
|
|
98
|
+
pause();
|
|
99
|
+
layer.remove();
|
|
100
|
+
};
|
|
101
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "winter-fx",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Falling snow, a twinkling garland and frost on the edges for any web page, with no dependencies",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "vite demo",
|
|
7
|
+
"typecheck": "tsc --noEmit",
|
|
8
|
+
"lint": "biome check .",
|
|
9
|
+
"format": "biome check --write .",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"build": "tsc -p tsconfig.build.json",
|
|
12
|
+
"prepack": "npm run build",
|
|
13
|
+
"size": "npm run build && size-limit",
|
|
14
|
+
"check:package": "publint && attw --pack . --profile esm-only"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/y3rsss/winter-fx.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"snow",
|
|
22
|
+
"snowfall",
|
|
23
|
+
"garland",
|
|
24
|
+
"frost",
|
|
25
|
+
"winter",
|
|
26
|
+
"christmas",
|
|
27
|
+
"holiday",
|
|
28
|
+
"effects",
|
|
29
|
+
"canvas",
|
|
30
|
+
"svg"
|
|
31
|
+
],
|
|
32
|
+
"author": "",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"type": "module",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist"
|
|
43
|
+
],
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/y3rsss/winter-fx/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/y3rsss/winter-fx#readme",
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
51
|
+
"@biomejs/biome": "2.6.3",
|
|
52
|
+
"@size-limit/preset-small-lib": "14.3.0",
|
|
53
|
+
"@vitest/browser-playwright": "5.0.5",
|
|
54
|
+
"playwright": "1.64.1",
|
|
55
|
+
"publint": "0.3.25",
|
|
56
|
+
"size-limit": "14.3.0",
|
|
57
|
+
"typescript": "7.0.2",
|
|
58
|
+
"vite": "8.3.5",
|
|
59
|
+
"vitest": "5.0.5"
|
|
60
|
+
}
|
|
61
|
+
}
|