winter-fx 0.1.0 → 0.2.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 -21
- package/README.md +8 -2
- package/dist/frost/frost.js +3 -0
- package/dist/garland/garland.js +5 -0
- package/dist/shared/options.d.ts +1 -0
- package/dist/shared/options.js +11 -0
- package/dist/snow/crystals.d.ts +31 -0
- package/dist/snow/crystals.js +115 -0
- package/dist/snow/flakes.d.ts +25 -5
- package/dist/snow/flakes.js +30 -14
- package/dist/snow/snow.d.ts +3 -0
- package/dist/snow/snow.js +43 -8
- package/package.json +4 -1
package/LICENSE
CHANGED
|
@@ -1,21 +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.
|
|
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
|
@@ -2,6 +2,8 @@
|
|
|
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
4
|
|
|
5
|
+
Try every setting on the [demo page](https://y3rsss.github.io/winter-fx/).
|
|
6
|
+
|
|
5
7
|
## Install
|
|
6
8
|
|
|
7
9
|
```sh
|
|
@@ -34,7 +36,7 @@ All settings are optional. Every effect also takes `zIndex`, any integer, which
|
|
|
34
36
|
Snowflakes falling over the page, in three depth layers.
|
|
35
37
|
|
|
36
38
|
```js
|
|
37
|
-
snow({ color: "#ffffff", amount: 2, speed: 0.5 });
|
|
39
|
+
snow({ color: "#ffffff", amount: 2, speed: 0.5, shape: "crystal", size: 2 });
|
|
38
40
|
```
|
|
39
41
|
|
|
40
42
|
| Setting | Default | Valid values |
|
|
@@ -42,6 +44,10 @@ snow({ color: "#ffffff", amount: 2, speed: 0.5 });
|
|
|
42
44
|
| `color` | `"light-dark(#7f9cc0, #ffffff)"` | any CSS color |
|
|
43
45
|
| `amount` | `1` | a number greater than 0 and at most 3. Multiplies the number of flakes |
|
|
44
46
|
| `speed` | `1` | a number greater than 0 and at most 5. Multiplies the falling speed |
|
|
47
|
+
| `shape` | `"dot"` | `"dot"` or `"crystal"` |
|
|
48
|
+
| `size` | `1` | a number greater than 0 and at most 3. Multiplies how big flakes are |
|
|
49
|
+
|
|
50
|
+
With `shape: "crystal"`, each flake is a thin six-armed crystal or a small clump, as wide as the dot it replaces, that sways and turns as it falls. Bigger crystals sway wider. Crystals take 2 to 3 times as long to draw as dots, and about 4 times at size 3, where more of them are big enough to show their details.
|
|
45
51
|
|
|
46
52
|
### garland
|
|
47
53
|
|
|
@@ -108,7 +114,7 @@ Sites without a build step can load winter-fx from a CDN:
|
|
|
108
114
|
|
|
109
115
|
```html
|
|
110
116
|
<script type="module">
|
|
111
|
-
import { snow } from "https://cdn.jsdelivr.net/npm/winter-fx@0.
|
|
117
|
+
import { snow } from "https://cdn.jsdelivr.net/npm/winter-fx@0.2/+esm";
|
|
112
118
|
snow();
|
|
113
119
|
</script>
|
|
114
120
|
```
|
package/dist/frost/frost.js
CHANGED
|
@@ -33,6 +33,8 @@ function createStroke(paint, width, opacity) {
|
|
|
33
33
|
path.style.stroke = paint;
|
|
34
34
|
path.style.strokeWidth = String(width);
|
|
35
35
|
path.style.strokeLinecap = "round";
|
|
36
|
+
path.style.strokeDasharray = "none";
|
|
37
|
+
path.style.strokeOpacity = "1";
|
|
36
38
|
path.style.opacity = String(opacity);
|
|
37
39
|
path.style.pointerEvents = "none";
|
|
38
40
|
return path;
|
|
@@ -57,6 +59,7 @@ function createGrain(color, grainId) {
|
|
|
57
59
|
}), svgElement("feComposite", { in: "SourceGraphic", operator: "in" }));
|
|
58
60
|
const specks = svgElement("rect", { width: GRAIN_TILE, height: GRAIN_TILE });
|
|
59
61
|
specks.style.fill = color;
|
|
62
|
+
specks.style.stroke = "none";
|
|
60
63
|
specks.style.filter = `url(#${grainId}-noise)`;
|
|
61
64
|
const grain = svgElement("pattern", {
|
|
62
65
|
id: grainId,
|
package/dist/garland/garland.js
CHANGED
|
@@ -25,10 +25,13 @@ function createBulb(point, color, glowId) {
|
|
|
25
25
|
const body = svgElement("ellipse", { cy: 8, rx: 4, ry: 6 });
|
|
26
26
|
const cap = svgElement("rect", { x: -2, width: 4, height: 3 });
|
|
27
27
|
glow.style.fill = color;
|
|
28
|
+
glow.style.stroke = "none";
|
|
28
29
|
glow.style.opacity = "0.5";
|
|
29
30
|
glow.style.filter = `url(#${glowId})`;
|
|
30
31
|
body.style.fill = color;
|
|
32
|
+
body.style.stroke = "none";
|
|
31
33
|
cap.style.fill = CAP_COLOR;
|
|
34
|
+
cap.style.stroke = "none";
|
|
32
35
|
bulb.append(glow, body, cap);
|
|
33
36
|
return bulb;
|
|
34
37
|
}
|
|
@@ -67,6 +70,8 @@ export function garland(options) {
|
|
|
67
70
|
wire.style.fill = "none";
|
|
68
71
|
wire.style.stroke = WIRE_COLOR;
|
|
69
72
|
wire.style.strokeWidth = "1.5";
|
|
73
|
+
wire.style.strokeDasharray = "none";
|
|
74
|
+
wire.style.strokeOpacity = "1";
|
|
70
75
|
const lights = svgElement("g", {});
|
|
71
76
|
strip.append(defs, wire, lights);
|
|
72
77
|
layer.append(strip);
|
package/dist/shared/options.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export declare function checkPositive(effect: string, name: string, value: unkno
|
|
|
3
3
|
export declare function checkColor(effect: string, name: string, value: unknown): string;
|
|
4
4
|
export declare function themeColor(light: string, dark: string): string;
|
|
5
5
|
export declare function checkColorList(effect: string, name: string, value: unknown): string[];
|
|
6
|
+
export declare function checkChoice<Choice extends string>(effect: string, name: string, value: unknown, choices: readonly Choice[]): Choice;
|
|
6
7
|
export interface CommonOptions {
|
|
7
8
|
zIndex?: number;
|
|
8
9
|
}
|
package/dist/shared/options.js
CHANGED
|
@@ -64,6 +64,17 @@ export function checkColorList(effect, name, value) {
|
|
|
64
64
|
}
|
|
65
65
|
return Array.from(value, (color, index) => checkColor(effect, `${name}[${index}]`, color));
|
|
66
66
|
}
|
|
67
|
+
export function checkChoice(effect, name, value, choices) {
|
|
68
|
+
const rule = `be one of ${choices.map(show).join(", ")}`;
|
|
69
|
+
if (typeof value !== "string") {
|
|
70
|
+
throw new TypeError(settingMessage(effect, name, rule, value));
|
|
71
|
+
}
|
|
72
|
+
const choice = choices.find((item) => item === value);
|
|
73
|
+
if (choice === undefined) {
|
|
74
|
+
throw new RangeError(settingMessage(effect, name, rule, value));
|
|
75
|
+
}
|
|
76
|
+
return choice;
|
|
77
|
+
}
|
|
67
78
|
export function readOptions(effect, options, allowed) {
|
|
68
79
|
if (options === undefined) {
|
|
69
80
|
return {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Random } from "../shared/random.ts";
|
|
2
|
+
export type Segment = readonly [x1: number, y1: number, x2: number, y2: number];
|
|
3
|
+
export declare function rotatedCopies(segments: readonly Segment[], count: number): Segment[];
|
|
4
|
+
export interface CrystalKind {
|
|
5
|
+
readonly lines: readonly Segment[];
|
|
6
|
+
readonly smallLines: readonly Segment[];
|
|
7
|
+
}
|
|
8
|
+
export declare const STELLAR_DENDRITE: CrystalKind;
|
|
9
|
+
export declare const STELLAR_PLATE: CrystalKind;
|
|
10
|
+
export declare const SECTORED_PLATE: CrystalKind;
|
|
11
|
+
export declare const CLUMP_A: CrystalKind;
|
|
12
|
+
export declare const CLUMP_B: CrystalKind;
|
|
13
|
+
export declare function pickKind(pick: number): CrystalKind;
|
|
14
|
+
export type Motion = "swing" | "spiral";
|
|
15
|
+
export interface Crystal {
|
|
16
|
+
readonly kind: CrystalKind;
|
|
17
|
+
readonly motion: Motion;
|
|
18
|
+
readonly turn: number;
|
|
19
|
+
readonly spin: number;
|
|
20
|
+
readonly lean: number;
|
|
21
|
+
readonly sway: number;
|
|
22
|
+
}
|
|
23
|
+
export declare function createCrystal(radius: number, random: Random): Crystal;
|
|
24
|
+
export interface Matrix {
|
|
25
|
+
readonly a: number;
|
|
26
|
+
readonly b: number;
|
|
27
|
+
readonly c: number;
|
|
28
|
+
readonly d: number;
|
|
29
|
+
}
|
|
30
|
+
export declare function crystalMatrix(crystal: Crystal, phase: number): Matrix;
|
|
31
|
+
export declare function addCrystal(path: Pick<CanvasPath, "moveTo" | "lineTo">, lines: readonly Segment[], matrix: Matrix, x: number, y: number, radius: number): void;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { between } from "../shared/random.js";
|
|
2
|
+
const ARM_STEP = Math.PI / 3;
|
|
3
|
+
export function rotatedCopies(segments, count) {
|
|
4
|
+
const copies = [];
|
|
5
|
+
for (let k = 0; k < count; k += 1) {
|
|
6
|
+
const angle = k * ARM_STEP;
|
|
7
|
+
const cos = Math.cos(angle);
|
|
8
|
+
const sin = Math.sin(angle);
|
|
9
|
+
for (const [x1, y1, x2, y2] of segments) {
|
|
10
|
+
copies.push([
|
|
11
|
+
x1 * cos - y1 * sin,
|
|
12
|
+
x1 * sin + y1 * cos,
|
|
13
|
+
x2 * cos - y2 * sin,
|
|
14
|
+
x2 * sin + y2 * cos,
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return copies;
|
|
19
|
+
}
|
|
20
|
+
const PLAIN = rotatedCopies([[0, -1, 0, 1]], 3);
|
|
21
|
+
function sixArmed(arm) {
|
|
22
|
+
return { lines: [...PLAIN, ...rotatedCopies(arm, 6)], smallLines: PLAIN };
|
|
23
|
+
}
|
|
24
|
+
export const STELLAR_DENDRITE = sixArmed([
|
|
25
|
+
[0, -0.42, -0.294, -0.59],
|
|
26
|
+
[0, -0.42, 0.294, -0.59],
|
|
27
|
+
[0, -0.7, -0.208, -0.82],
|
|
28
|
+
[0, -0.7, 0.208, -0.82],
|
|
29
|
+
]);
|
|
30
|
+
export const STELLAR_PLATE = sixArmed([[0, -0.4, 0.346, -0.2]]);
|
|
31
|
+
export const SECTORED_PLATE = sixArmed([[0, -0.9, 0.779, -0.45]]);
|
|
32
|
+
const CLUMP_A_LINES = [
|
|
33
|
+
[-0.1, 0.05, 0.75, -0.2],
|
|
34
|
+
[0.05, 0.1, -0.3, -0.7],
|
|
35
|
+
[0, 0, -0.8, 0.15],
|
|
36
|
+
[0.1, -0.05, 0.35, 0.7],
|
|
37
|
+
[-0.05, 0, -0.25, 0.6],
|
|
38
|
+
];
|
|
39
|
+
export const CLUMP_A = {
|
|
40
|
+
lines: CLUMP_A_LINES,
|
|
41
|
+
smallLines: CLUMP_A_LINES,
|
|
42
|
+
};
|
|
43
|
+
const CLUMP_B_LINES = [
|
|
44
|
+
[0, 0.1, 0.6, -0.55],
|
|
45
|
+
[0.05, 0, -0.7, -0.35],
|
|
46
|
+
[-0.1, 0, 0.1, 0.85],
|
|
47
|
+
[0.1, 0.05, 0.85, 0.3],
|
|
48
|
+
];
|
|
49
|
+
export const CLUMP_B = {
|
|
50
|
+
lines: CLUMP_B_LINES,
|
|
51
|
+
smallLines: CLUMP_B_LINES,
|
|
52
|
+
};
|
|
53
|
+
export function pickKind(pick) {
|
|
54
|
+
if (pick < 0.45) {
|
|
55
|
+
return STELLAR_DENDRITE;
|
|
56
|
+
}
|
|
57
|
+
else if (pick < 0.7) {
|
|
58
|
+
return STELLAR_PLATE;
|
|
59
|
+
}
|
|
60
|
+
else if (pick < 0.8) {
|
|
61
|
+
return SECTORED_PLATE;
|
|
62
|
+
}
|
|
63
|
+
else if (pick < 0.9) {
|
|
64
|
+
return CLUMP_A;
|
|
65
|
+
}
|
|
66
|
+
return CLUMP_B;
|
|
67
|
+
}
|
|
68
|
+
const FULL_TURN = 2 * Math.PI;
|
|
69
|
+
const DEGREE = Math.PI / 180;
|
|
70
|
+
const MAX_SPIN = 0.25;
|
|
71
|
+
const MIN_LEAN = 10 * DEGREE;
|
|
72
|
+
const MAX_LEAN = 30 * DEGREE;
|
|
73
|
+
const MIN_SWAY_RADII = 2;
|
|
74
|
+
const MAX_SWAY_RADII = 5;
|
|
75
|
+
export function createCrystal(radius, random) {
|
|
76
|
+
return {
|
|
77
|
+
kind: pickKind(random()),
|
|
78
|
+
motion: random() < 0.5 ? "swing" : "spiral",
|
|
79
|
+
turn: random() * FULL_TURN,
|
|
80
|
+
spin: between(random, -MAX_SPIN, MAX_SPIN),
|
|
81
|
+
lean: between(random, MIN_LEAN, MAX_LEAN),
|
|
82
|
+
sway: radius * between(random, MIN_SWAY_RADII, MAX_SWAY_RADII),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const SWING_AXIS = Math.PI / 2;
|
|
86
|
+
export function crystalMatrix(crystal, phase) {
|
|
87
|
+
const turn = crystal.turn + crystal.spin * phase;
|
|
88
|
+
let axis = SWING_AXIS;
|
|
89
|
+
let tip = crystal.lean * Math.sin(phase);
|
|
90
|
+
if (crystal.motion === "spiral") {
|
|
91
|
+
axis = phase;
|
|
92
|
+
tip = crystal.lean;
|
|
93
|
+
}
|
|
94
|
+
const squash = Math.cos(tip);
|
|
95
|
+
const ca = Math.cos(axis);
|
|
96
|
+
const sa = Math.sin(axis);
|
|
97
|
+
const ct = Math.cos(turn - axis);
|
|
98
|
+
const st = Math.sin(turn - axis);
|
|
99
|
+
return {
|
|
100
|
+
a: ca * ct - sa * squash * st,
|
|
101
|
+
b: sa * ct + ca * squash * st,
|
|
102
|
+
c: -ca * st - sa * squash * ct,
|
|
103
|
+
d: -sa * st + ca * squash * ct,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export function addCrystal(path, lines, matrix, x, y, radius) {
|
|
107
|
+
const a = matrix.a * radius;
|
|
108
|
+
const b = matrix.b * radius;
|
|
109
|
+
const c = matrix.c * radius;
|
|
110
|
+
const d = matrix.d * radius;
|
|
111
|
+
for (const [x1, y1, x2, y2] of lines) {
|
|
112
|
+
path.moveTo(x + a * x1 + c * y1, y + b * x1 + d * y1);
|
|
113
|
+
path.lineTo(x + a * x2 + c * y2, y + b * x2 + d * y2);
|
|
114
|
+
}
|
|
115
|
+
}
|
package/dist/snow/flakes.d.ts
CHANGED
|
@@ -1,21 +1,41 @@
|
|
|
1
1
|
import { type Random } from "../shared/random.ts";
|
|
2
|
+
import { type Crystal } from "./crystals.ts";
|
|
3
|
+
export declare const SHAPES: readonly ["dot", "crystal"];
|
|
4
|
+
export type Shape = (typeof SHAPES)[number];
|
|
5
|
+
export interface Look {
|
|
6
|
+
readonly shape: Shape;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
}
|
|
2
9
|
export interface Bounds {
|
|
3
10
|
width: number;
|
|
4
11
|
height: number;
|
|
5
12
|
}
|
|
6
13
|
export declare function flakeCount(bounds: Bounds, amount: number): number;
|
|
14
|
+
export interface Layer {
|
|
15
|
+
readonly minRadius: number;
|
|
16
|
+
readonly maxRadius: number;
|
|
17
|
+
readonly minSpeed: number;
|
|
18
|
+
readonly maxSpeed: number;
|
|
19
|
+
readonly opacity: number;
|
|
20
|
+
readonly lineWidth: number;
|
|
21
|
+
}
|
|
7
22
|
export interface Flake {
|
|
8
23
|
readonly x: number;
|
|
9
24
|
readonly y: number;
|
|
10
25
|
readonly radius: number;
|
|
11
26
|
readonly speed: number;
|
|
12
|
-
readonly
|
|
27
|
+
readonly layer: Layer;
|
|
13
28
|
readonly phase: number;
|
|
14
29
|
readonly swaySpeed: number;
|
|
30
|
+
readonly crystal?: Crystal;
|
|
15
31
|
}
|
|
16
|
-
export declare
|
|
17
|
-
export declare
|
|
32
|
+
export declare const BACK: Layer;
|
|
33
|
+
export declare const MIDDLE: Layer;
|
|
34
|
+
export declare const FRONT: Layer;
|
|
35
|
+
export declare const LAYERS: readonly Layer[];
|
|
36
|
+
export declare function createFlake(bounds: Bounds, look: Look, random: Random): Flake;
|
|
37
|
+
export declare function createFlakes(count: number, bounds: Bounds, look: Look, random: Random): Flake[];
|
|
18
38
|
export declare function swayOffset(flake: Flake): number;
|
|
19
39
|
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[];
|
|
40
|
+
export declare function moveFlake(flake: Flake, seconds: number, speedFactor: number, bounds: Bounds, look: Look, random: Random): Flake;
|
|
41
|
+
export declare function resizeFlakes(flakes: Flake[], bounds: Bounds, amount: number, look: Look, random: Random): Flake[];
|
package/dist/snow/flakes.js
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
1
|
import { between } from "../shared/random.js";
|
|
2
|
+
import { createCrystal } from "./crystals.js";
|
|
3
|
+
export const SHAPES = ["dot", "crystal"];
|
|
2
4
|
const AREA_PER_FLAKE = 5300;
|
|
3
5
|
export function flakeCount(bounds, amount) {
|
|
4
6
|
return Math.round(((bounds.width * bounds.height) / AREA_PER_FLAKE) * amount);
|
|
5
7
|
}
|
|
6
|
-
const BACK = {
|
|
8
|
+
export const BACK = {
|
|
7
9
|
minRadius: 1,
|
|
8
10
|
maxRadius: 1.5,
|
|
9
11
|
minSpeed: 20,
|
|
10
12
|
maxSpeed: 40,
|
|
11
13
|
opacity: 0.4,
|
|
14
|
+
lineWidth: 0.75,
|
|
12
15
|
};
|
|
13
|
-
const MIDDLE = {
|
|
16
|
+
export const MIDDLE = {
|
|
14
17
|
minRadius: 1.5,
|
|
15
18
|
maxRadius: 2.5,
|
|
16
19
|
minSpeed: 40,
|
|
17
20
|
maxSpeed: 70,
|
|
18
21
|
opacity: 0.7,
|
|
22
|
+
lineWidth: 0.85,
|
|
19
23
|
};
|
|
20
|
-
const FRONT = {
|
|
24
|
+
export const FRONT = {
|
|
21
25
|
minRadius: 2.5,
|
|
22
26
|
maxRadius: 4,
|
|
23
27
|
minSpeed: 70,
|
|
24
28
|
maxSpeed: 110,
|
|
25
29
|
opacity: 0.95,
|
|
30
|
+
lineWidth: 1,
|
|
26
31
|
};
|
|
32
|
+
export const LAYERS = [BACK, MIDDLE, FRONT];
|
|
27
33
|
function pickLayer(pick) {
|
|
28
34
|
if (pick < 0.5) {
|
|
29
35
|
return BACK;
|
|
@@ -33,24 +39,34 @@ function pickLayer(pick) {
|
|
|
33
39
|
}
|
|
34
40
|
return FRONT;
|
|
35
41
|
}
|
|
36
|
-
|
|
42
|
+
const MIN_CRYSTAL_SWAY_SPEED = 1.25;
|
|
43
|
+
const MAX_CRYSTAL_SWAY_SPEED = 2.5;
|
|
44
|
+
export function createFlake(bounds, look, random) {
|
|
37
45
|
const layer = pickLayer(random());
|
|
38
|
-
|
|
46
|
+
const flake = {
|
|
39
47
|
x: random() * bounds.width,
|
|
40
48
|
y: random() * bounds.height,
|
|
41
|
-
radius: between(random, layer.minRadius, layer.maxRadius),
|
|
49
|
+
radius: between(random, layer.minRadius, layer.maxRadius) * look.size,
|
|
42
50
|
speed: between(random, layer.minSpeed, layer.maxSpeed),
|
|
43
|
-
|
|
51
|
+
layer,
|
|
44
52
|
phase: random() * 2 * Math.PI,
|
|
45
53
|
swaySpeed: 0.5 + random(),
|
|
46
54
|
};
|
|
55
|
+
if (look.shape === "dot") {
|
|
56
|
+
return flake;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
...flake,
|
|
60
|
+
swaySpeed: between(random, MIN_CRYSTAL_SWAY_SPEED, MAX_CRYSTAL_SWAY_SPEED),
|
|
61
|
+
crystal: createCrystal(flake.radius, random),
|
|
62
|
+
};
|
|
47
63
|
}
|
|
48
|
-
export function createFlakes(count, bounds, random) {
|
|
49
|
-
return Array.from({ length: count }, () => createFlake(bounds, random));
|
|
64
|
+
export function createFlakes(count, bounds, look, random) {
|
|
65
|
+
return Array.from({ length: count }, () => createFlake(bounds, look, random));
|
|
50
66
|
}
|
|
51
67
|
const SWAY_PX = 8;
|
|
52
68
|
export function swayOffset(flake) {
|
|
53
|
-
return Math.sin(flake.phase) * SWAY_PX;
|
|
69
|
+
return Math.sin(flake.phase) * (flake.crystal?.sway ?? SWAY_PX);
|
|
54
70
|
}
|
|
55
71
|
const MAX_STEP_SECONDS = 0.1;
|
|
56
72
|
export function elapsedSeconds(previous, now) {
|
|
@@ -58,15 +74,15 @@ export function elapsedSeconds(previous, now) {
|
|
|
58
74
|
return 0;
|
|
59
75
|
return Math.min((now - previous) / 1000, MAX_STEP_SECONDS);
|
|
60
76
|
}
|
|
61
|
-
export function moveFlake(flake, seconds, speedFactor, bounds, random) {
|
|
77
|
+
export function moveFlake(flake, seconds, speedFactor, bounds, look, random) {
|
|
62
78
|
const newY = flake.y + speedFactor * seconds * flake.speed;
|
|
63
79
|
if (newY - flake.radius > bounds.height) {
|
|
64
|
-
const fresh = createFlake(bounds, random);
|
|
80
|
+
const fresh = createFlake(bounds, look, random);
|
|
65
81
|
return { ...fresh, y: -fresh.radius };
|
|
66
82
|
}
|
|
67
83
|
return { ...flake, y: newY, phase: flake.phase + flake.swaySpeed * seconds };
|
|
68
84
|
}
|
|
69
|
-
export function resizeFlakes(flakes, bounds, amount, random) {
|
|
85
|
+
export function resizeFlakes(flakes, bounds, amount, look, random) {
|
|
70
86
|
const count = flakeCount(bounds, amount);
|
|
71
87
|
const firstFlakes = flakes.slice(0, count);
|
|
72
88
|
const keptFlakes = firstFlakes.map((flake) => {
|
|
@@ -75,6 +91,6 @@ export function resizeFlakes(flakes, bounds, amount, random) {
|
|
|
75
91
|
}
|
|
76
92
|
return flake;
|
|
77
93
|
});
|
|
78
|
-
const newFlakes = createFlakes(count - keptFlakes.length, bounds, random);
|
|
94
|
+
const newFlakes = createFlakes(count - keptFlakes.length, bounds, look, random);
|
|
79
95
|
return [...keptFlakes, ...newFlakes];
|
|
80
96
|
}
|
package/dist/snow/snow.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { type CommonOptions } from "../shared/options.ts";
|
|
2
2
|
import type { Stop } from "../shared/stop.ts";
|
|
3
|
+
import { type Shape } from "./flakes.ts";
|
|
3
4
|
export interface SnowOptions extends CommonOptions {
|
|
4
5
|
color?: string;
|
|
5
6
|
amount?: number;
|
|
6
7
|
speed?: number;
|
|
8
|
+
shape?: Shape;
|
|
9
|
+
size?: number;
|
|
7
10
|
}
|
|
8
11
|
export declare function snow(options?: SnowOptions): Stop;
|
package/dist/snow/snow.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { watchReducedMotion } from "../shared/motion.js";
|
|
2
|
-
import { checkColor, checkInteger, checkPositive, readOptions, themeColor, } from "../shared/options.js";
|
|
2
|
+
import { checkChoice, checkColor, checkInteger, checkPositive, readOptions, themeColor, } from "../shared/options.js";
|
|
3
3
|
import { assertBrowser, createOverlay } from "../shared/overlay.js";
|
|
4
|
-
import {
|
|
4
|
+
import { addCrystal, crystalMatrix } from "./crystals.js";
|
|
5
|
+
import { createFlakes, elapsedSeconds, flakeCount, LAYERS, moveFlake, resizeFlakes, SHAPES, swayOffset, } from "./flakes.js";
|
|
5
6
|
const DEFAULT_LIGHT_COLOR = "#7f9cc0";
|
|
6
7
|
const DEFAULT_DARK_COLOR = "#ffffff";
|
|
7
8
|
const MAX_PIXEL_RATIO = 2;
|
|
9
|
+
const DETAIL_FROM_DEVICE_PX = 8;
|
|
8
10
|
const FADE_IN_MS = 1000;
|
|
9
11
|
const MAX_AMOUNT = 3;
|
|
10
12
|
const MAX_SPEED = 5;
|
|
13
|
+
const MAX_SIZE = 3;
|
|
11
14
|
function getContext2d(canvas) {
|
|
12
15
|
const context = canvas.getContext("2d");
|
|
13
16
|
if (context === null) {
|
|
@@ -15,8 +18,11 @@ function getContext2d(canvas) {
|
|
|
15
18
|
}
|
|
16
19
|
return context;
|
|
17
20
|
}
|
|
21
|
+
function pixelRatio() {
|
|
22
|
+
return Math.min(devicePixelRatio, MAX_PIXEL_RATIO);
|
|
23
|
+
}
|
|
18
24
|
function fitCanvas(canvas, context) {
|
|
19
|
-
const ratio =
|
|
25
|
+
const ratio = pixelRatio();
|
|
20
26
|
const bounds = { width: canvas.clientWidth, height: canvas.clientHeight };
|
|
21
27
|
canvas.width = Math.round(bounds.width * ratio);
|
|
22
28
|
canvas.height = Math.round(bounds.height * ratio);
|
|
@@ -27,24 +33,48 @@ function drawFlakes(context, flakes, bounds, color) {
|
|
|
27
33
|
context.clearRect(0, 0, bounds.width, bounds.height);
|
|
28
34
|
context.fillStyle = color;
|
|
29
35
|
for (const flake of flakes) {
|
|
30
|
-
context.globalAlpha = flake.opacity;
|
|
36
|
+
context.globalAlpha = flake.layer.opacity;
|
|
31
37
|
context.beginPath();
|
|
32
38
|
context.arc(flake.x + swayOffset(flake), flake.y, flake.radius, 0, 2 * Math.PI);
|
|
33
39
|
context.fill();
|
|
34
40
|
}
|
|
35
41
|
}
|
|
42
|
+
function drawCrystals(context, flakes, bounds, color, ratio) {
|
|
43
|
+
context.clearRect(0, 0, bounds.width, bounds.height);
|
|
44
|
+
context.strokeStyle = color;
|
|
45
|
+
context.lineCap = "round";
|
|
46
|
+
for (const layer of LAYERS) {
|
|
47
|
+
context.globalAlpha = layer.opacity;
|
|
48
|
+
context.lineWidth = layer.lineWidth;
|
|
49
|
+
context.beginPath();
|
|
50
|
+
for (const flake of flakes) {
|
|
51
|
+
const crystal = flake.crystal;
|
|
52
|
+
if (flake.layer === layer && crystal !== undefined) {
|
|
53
|
+
const detailed = flake.radius * 2 * ratio >= DETAIL_FROM_DEVICE_PX;
|
|
54
|
+
const lines = detailed ? crystal.kind.lines : crystal.kind.smallLines;
|
|
55
|
+
addCrystal(context, lines, crystalMatrix(crystal, flake.phase), flake.x + swayOffset(flake), flake.y, flake.radius);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
context.stroke();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
36
61
|
export function snow(options) {
|
|
37
62
|
assertBrowser("snow");
|
|
38
63
|
const settings = readOptions("snow", options, [
|
|
39
64
|
"color",
|
|
40
65
|
"amount",
|
|
41
66
|
"speed",
|
|
67
|
+
"shape",
|
|
68
|
+
"size",
|
|
42
69
|
"zIndex",
|
|
43
70
|
]);
|
|
44
71
|
const color = checkColor("snow", "color", settings.color ?? themeColor(DEFAULT_LIGHT_COLOR, DEFAULT_DARK_COLOR));
|
|
45
72
|
const amount = checkPositive("snow", "amount", settings.amount ?? 1, MAX_AMOUNT);
|
|
46
73
|
const speed = checkPositive("snow", "speed", settings.speed ?? 1, MAX_SPEED);
|
|
74
|
+
const shape = checkChoice("snow", "shape", settings.shape ?? "dot", SHAPES);
|
|
75
|
+
const size = checkPositive("snow", "size", settings.size ?? 1, MAX_SIZE);
|
|
47
76
|
const zIndex = checkInteger("snow", "zIndex", settings.zIndex ?? 9999);
|
|
77
|
+
const look = { shape, size };
|
|
48
78
|
const canvas = document.createElement("canvas");
|
|
49
79
|
const context = getContext2d(canvas);
|
|
50
80
|
canvas.style.display = "block";
|
|
@@ -55,7 +85,7 @@ export function snow(options) {
|
|
|
55
85
|
layer.append(canvas);
|
|
56
86
|
let bounds = fitCanvas(canvas, context);
|
|
57
87
|
const controller = new AbortController();
|
|
58
|
-
let flakes = createFlakes(flakeCount(bounds, amount), bounds, Math.random);
|
|
88
|
+
let flakes = createFlakes(flakeCount(bounds, amount), bounds, look, Math.random);
|
|
59
89
|
let last;
|
|
60
90
|
let frame;
|
|
61
91
|
function tick(now) {
|
|
@@ -67,8 +97,13 @@ export function snow(options) {
|
|
|
67
97
|
}
|
|
68
98
|
const seconds = elapsedSeconds(last, now);
|
|
69
99
|
last = now;
|
|
70
|
-
flakes = flakes.map((flake) => moveFlake(flake, seconds, speed, bounds, Math.random));
|
|
71
|
-
|
|
100
|
+
flakes = flakes.map((flake) => moveFlake(flake, seconds, speed, bounds, look, Math.random));
|
|
101
|
+
if (shape === "dot") {
|
|
102
|
+
drawFlakes(context, flakes, bounds, color);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
drawCrystals(context, flakes, bounds, color, pixelRatio());
|
|
106
|
+
}
|
|
72
107
|
frame = requestAnimationFrame(tick);
|
|
73
108
|
}
|
|
74
109
|
function play() {
|
|
@@ -91,7 +126,7 @@ export function snow(options) {
|
|
|
91
126
|
}, controller.signal);
|
|
92
127
|
addEventListener("resize", () => {
|
|
93
128
|
bounds = fitCanvas(canvas, context);
|
|
94
|
-
flakes = resizeFlakes(flakes, bounds, amount, Math.random);
|
|
129
|
+
flakes = resizeFlakes(flakes, bounds, amount, look, Math.random);
|
|
95
130
|
}, { signal: controller.signal });
|
|
96
131
|
return () => {
|
|
97
132
|
controller.abort();
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "winter-fx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Falling snow, a twinkling garland and frost on the edges for any web page, with no dependencies",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "vite demo",
|
|
7
|
+
"build:demo": "vite build demo",
|
|
7
8
|
"typecheck": "tsc --noEmit",
|
|
8
9
|
"lint": "biome check .",
|
|
9
10
|
"format": "biome check --write .",
|
|
@@ -49,6 +50,8 @@
|
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"@arethetypeswrong/cli": "0.18.5",
|
|
51
52
|
"@biomejs/biome": "2.6.3",
|
|
53
|
+
"@fontsource-variable/bricolage-grotesque": "5.3.0",
|
|
54
|
+
"@fontsource-variable/geist-mono": "5.3.0",
|
|
52
55
|
"@size-limit/preset-small-lib": "14.3.0",
|
|
53
56
|
"@vitest/browser-playwright": "5.0.5",
|
|
54
57
|
"playwright": "1.64.1",
|