winter-fx 0.0.0-stage → 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.
@@ -0,0 +1,136 @@
1
+ import { watchReducedMotion } from "../shared/motion.js";
2
+ import { checkChoice, checkColor, checkInteger, checkPositive, readOptions, themeColor, } from "../shared/options.js";
3
+ import { assertBrowser, createOverlay } from "../shared/overlay.js";
4
+ import { addCrystal, crystalMatrix } from "./crystals.js";
5
+ import { createFlakes, elapsedSeconds, flakeCount, LAYERS, moveFlake, resizeFlakes, SHAPES, swayOffset, } from "./flakes.js";
6
+ const DEFAULT_LIGHT_COLOR = "#7f9cc0";
7
+ const DEFAULT_DARK_COLOR = "#ffffff";
8
+ const MAX_PIXEL_RATIO = 2;
9
+ const DETAIL_FROM_DEVICE_PX = 8;
10
+ const FADE_IN_MS = 1000;
11
+ const MAX_AMOUNT = 3;
12
+ const MAX_SPEED = 5;
13
+ const MAX_SIZE = 3;
14
+ function getContext2d(canvas) {
15
+ const context = canvas.getContext("2d");
16
+ if (context === null) {
17
+ throw new Error("winter-fx: snow() needs a browser that can draw on a canvas");
18
+ }
19
+ return context;
20
+ }
21
+ function pixelRatio() {
22
+ return Math.min(devicePixelRatio, MAX_PIXEL_RATIO);
23
+ }
24
+ function fitCanvas(canvas, context) {
25
+ const ratio = pixelRatio();
26
+ const bounds = { width: canvas.clientWidth, height: canvas.clientHeight };
27
+ canvas.width = Math.round(bounds.width * ratio);
28
+ canvas.height = Math.round(bounds.height * ratio);
29
+ context.setTransform(ratio, 0, 0, ratio, 0, 0);
30
+ return bounds;
31
+ }
32
+ function drawFlakes(context, flakes, bounds, color) {
33
+ context.clearRect(0, 0, bounds.width, bounds.height);
34
+ context.fillStyle = color;
35
+ for (const flake of flakes) {
36
+ context.globalAlpha = flake.layer.opacity;
37
+ context.beginPath();
38
+ context.arc(flake.x + swayOffset(flake), flake.y, flake.radius, 0, 2 * Math.PI);
39
+ context.fill();
40
+ }
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
+ }
61
+ export function snow(options) {
62
+ assertBrowser("snow");
63
+ const settings = readOptions("snow", options, [
64
+ "color",
65
+ "amount",
66
+ "speed",
67
+ "shape",
68
+ "size",
69
+ "zIndex",
70
+ ]);
71
+ const color = checkColor("snow", "color", settings.color ?? themeColor(DEFAULT_LIGHT_COLOR, DEFAULT_DARK_COLOR));
72
+ const amount = checkPositive("snow", "amount", settings.amount ?? 1, MAX_AMOUNT);
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);
76
+ const zIndex = checkInteger("snow", "zIndex", settings.zIndex ?? 9999);
77
+ const look = { shape, size };
78
+ const canvas = document.createElement("canvas");
79
+ const context = getContext2d(canvas);
80
+ canvas.style.display = "block";
81
+ canvas.style.width = "100%";
82
+ canvas.style.height = "100%";
83
+ canvas.style.color = color;
84
+ const layer = createOverlay(zIndex);
85
+ layer.append(canvas);
86
+ let bounds = fitCanvas(canvas, context);
87
+ const controller = new AbortController();
88
+ let flakes = createFlakes(flakeCount(bounds, amount), bounds, look, Math.random);
89
+ let last;
90
+ let frame;
91
+ function tick(now) {
92
+ const scheduled = frame;
93
+ // firefox 111 can run the reduced motion listener inside this read, so the checks come after it
94
+ const color = getComputedStyle(canvas).color;
95
+ if (frame !== scheduled || frame === undefined || !canvas.isConnected) {
96
+ return;
97
+ }
98
+ const seconds = elapsedSeconds(last, now);
99
+ last = now;
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
+ }
107
+ frame = requestAnimationFrame(tick);
108
+ }
109
+ function play() {
110
+ last = undefined;
111
+ layer.animate([{ opacity: 0 }, { opacity: 1 }], FADE_IN_MS);
112
+ frame = requestAnimationFrame(tick);
113
+ }
114
+ function pause() {
115
+ if (frame !== undefined) {
116
+ cancelAnimationFrame(frame);
117
+ frame = undefined;
118
+ }
119
+ context.clearRect(0, 0, bounds.width, bounds.height);
120
+ }
121
+ watchReducedMotion((reduced) => {
122
+ if (reduced === true)
123
+ pause();
124
+ else
125
+ play();
126
+ }, controller.signal);
127
+ addEventListener("resize", () => {
128
+ bounds = fitCanvas(canvas, context);
129
+ flakes = resizeFlakes(flakes, bounds, amount, look, Math.random);
130
+ }, { signal: controller.signal });
131
+ return () => {
132
+ controller.abort();
133
+ pause();
134
+ layer.remove();
135
+ };
136
+ }
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
- "name": "winter-fx",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "winter-fx",
3
+ "version": "0.2.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
+ "build:demo": "vite build demo",
8
+ "typecheck": "tsc --noEmit",
9
+ "lint": "biome check .",
10
+ "format": "biome check --write .",
11
+ "test": "vitest run",
12
+ "build": "tsc -p tsconfig.build.json",
13
+ "prepack": "npm run build",
14
+ "size": "npm run build && size-limit",
15
+ "check:package": "publint && attw --pack . --profile esm-only"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/y3rsss/winter-fx.git"
20
+ },
21
+ "keywords": [
22
+ "snow",
23
+ "snowfall",
24
+ "garland",
25
+ "frost",
26
+ "winter",
27
+ "christmas",
28
+ "holiday",
29
+ "effects",
30
+ "canvas",
31
+ "svg"
32
+ ],
33
+ "author": "",
34
+ "license": "MIT",
35
+ "type": "module",
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/index.d.ts",
39
+ "default": "./dist/index.js"
40
+ }
41
+ },
42
+ "files": [
43
+ "dist"
44
+ ],
45
+ "sideEffects": false,
46
+ "bugs": {
47
+ "url": "https://github.com/y3rsss/winter-fx/issues"
48
+ },
49
+ "homepage": "https://github.com/y3rsss/winter-fx#readme",
50
+ "devDependencies": {
51
+ "@arethetypeswrong/cli": "0.18.5",
52
+ "@biomejs/biome": "2.6.3",
53
+ "@fontsource-variable/bricolage-grotesque": "5.3.0",
54
+ "@fontsource-variable/geist-mono": "5.3.0",
55
+ "@size-limit/preset-small-lib": "14.3.0",
56
+ "@vitest/browser-playwright": "5.0.5",
57
+ "playwright": "1.64.1",
58
+ "publint": "0.3.25",
59
+ "size-limit": "14.3.0",
60
+ "typescript": "7.0.2",
61
+ "vite": "8.3.5",
62
+ "vitest": "5.0.5"
63
+ }
64
+ }