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.
- package/LICENSE +21 -0
- package/README.md +139 -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 +233 -0
- package/dist/garland/garland.d.ts +6 -0
- package/dist/garland/garland.js +122 -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 +10 -0
- package/dist/shared/options.js +93 -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/crystals.d.ts +31 -0
- package/dist/snow/crystals.js +115 -0
- package/dist/snow/flakes.d.ts +41 -0
- package/dist/snow/flakes.js +96 -0
- package/dist/snow/snow.d.ts +11 -0
- package/dist/snow/snow.js +136 -0
- package/package.json +63 -5
|
@@ -0,0 +1,122 @@
|
|
|
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.stroke = "none";
|
|
29
|
+
glow.style.opacity = "0.5";
|
|
30
|
+
glow.style.filter = `url(#${glowId})`;
|
|
31
|
+
body.style.fill = color;
|
|
32
|
+
body.style.stroke = "none";
|
|
33
|
+
cap.style.fill = CAP_COLOR;
|
|
34
|
+
cap.style.stroke = "none";
|
|
35
|
+
bulb.append(glow, body, cap);
|
|
36
|
+
return bulb;
|
|
37
|
+
}
|
|
38
|
+
function twinkle(bulb) {
|
|
39
|
+
const duration = SLOWEST_TWINKLE_MS + Math.random() * SLOWEST_TWINKLE_MS;
|
|
40
|
+
return bulb.animate([{ opacity: 1 }, { opacity: DIM_OPACITY }], {
|
|
41
|
+
duration,
|
|
42
|
+
delay: Math.random() * duration,
|
|
43
|
+
iterations: Number.POSITIVE_INFINITY,
|
|
44
|
+
direction: "alternate",
|
|
45
|
+
easing: "ease-in-out",
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export function garland(options) {
|
|
49
|
+
assertBrowser("garland");
|
|
50
|
+
const settings = readOptions("garland", options, ["colors", "zIndex"]);
|
|
51
|
+
const colors = checkColorList("garland", "colors", settings.colors ?? DEFAULT_COLORS);
|
|
52
|
+
const zIndex = checkInteger("garland", "zIndex", settings.zIndex ?? 9999);
|
|
53
|
+
const layer = createOverlay(zIndex);
|
|
54
|
+
const strip = svgElement("svg", {});
|
|
55
|
+
strip.style.display = "block";
|
|
56
|
+
strip.style.width = "100%";
|
|
57
|
+
strip.style.height = `${STRIP_HEIGHT}px`;
|
|
58
|
+
const glowId = uniqueId("glow");
|
|
59
|
+
const glowFilter = svgElement("filter", {
|
|
60
|
+
id: glowId,
|
|
61
|
+
x: "-100%",
|
|
62
|
+
y: "-100%",
|
|
63
|
+
width: "300%",
|
|
64
|
+
height: "300%",
|
|
65
|
+
});
|
|
66
|
+
glowFilter.append(svgElement("feGaussianBlur", { stdDeviation: GLOW_BLUR }));
|
|
67
|
+
const defs = svgElement("defs", {});
|
|
68
|
+
defs.append(glowFilter);
|
|
69
|
+
const wire = svgElement("path", {});
|
|
70
|
+
wire.style.fill = "none";
|
|
71
|
+
wire.style.stroke = WIRE_COLOR;
|
|
72
|
+
wire.style.strokeWidth = "1.5";
|
|
73
|
+
wire.style.strokeDasharray = "none";
|
|
74
|
+
wire.style.strokeOpacity = "1";
|
|
75
|
+
const lights = svgElement("g", {});
|
|
76
|
+
strip.append(defs, wire, lights);
|
|
77
|
+
layer.append(strip);
|
|
78
|
+
let width = 0;
|
|
79
|
+
let bulbs = [];
|
|
80
|
+
function hang() {
|
|
81
|
+
width = layer.clientWidth;
|
|
82
|
+
const layout = garlandLayout(width);
|
|
83
|
+
wire.setAttribute("d", layout.wire);
|
|
84
|
+
bulbs = layout.bulbs.map((point, index) => createBulb(point, colorAt(colors, index), glowId));
|
|
85
|
+
lights.replaceChildren(...bulbs);
|
|
86
|
+
}
|
|
87
|
+
hang();
|
|
88
|
+
const controller = new AbortController();
|
|
89
|
+
let twinkles = [];
|
|
90
|
+
function play() {
|
|
91
|
+
twinkles = bulbs.map((bulb) => twinkle(bulb));
|
|
92
|
+
}
|
|
93
|
+
function pause() {
|
|
94
|
+
for (const t of twinkles) {
|
|
95
|
+
t.cancel();
|
|
96
|
+
}
|
|
97
|
+
twinkles = [];
|
|
98
|
+
}
|
|
99
|
+
watchReducedMotion((reduced) => {
|
|
100
|
+
if (reduced)
|
|
101
|
+
pause();
|
|
102
|
+
else
|
|
103
|
+
play();
|
|
104
|
+
}, controller.signal);
|
|
105
|
+
const observer = new ResizeObserver(() => {
|
|
106
|
+
if (layer.clientWidth === width) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const wasTwinkling = twinkles.length > 0;
|
|
110
|
+
pause();
|
|
111
|
+
hang();
|
|
112
|
+
if (wasTwinkling)
|
|
113
|
+
play();
|
|
114
|
+
});
|
|
115
|
+
observer.observe(layer);
|
|
116
|
+
return () => {
|
|
117
|
+
controller.abort();
|
|
118
|
+
observer.disconnect();
|
|
119
|
+
pause();
|
|
120
|
+
layer.remove();
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -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,10 @@
|
|
|
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 declare function checkChoice<Choice extends string>(effect: string, name: string, value: unknown, choices: readonly Choice[]): Choice;
|
|
7
|
+
export interface CommonOptions {
|
|
8
|
+
zIndex?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function readOptions(effect: string, options: unknown, allowed: readonly string[]): Record<string, unknown>;
|
|
@@ -0,0 +1,93 @@
|
|
|
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 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
|
+
}
|
|
78
|
+
export function readOptions(effect, options, allowed) {
|
|
79
|
+
if (options === undefined) {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
if (typeof options !== "object" ||
|
|
83
|
+
options === null ||
|
|
84
|
+
Array.isArray(options)) {
|
|
85
|
+
throw new TypeError(`winter-fx: ${effect}() settings must be an object, got ${show(options)}`);
|
|
86
|
+
}
|
|
87
|
+
for (const key of Object.keys(options)) {
|
|
88
|
+
if (!allowed.includes(key)) {
|
|
89
|
+
throw new TypeError(`winter-fx: ${effect}() has no setting "${key}"`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return options;
|
|
93
|
+
}
|
|
@@ -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,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
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
}
|
|
9
|
+
export interface Bounds {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
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
|
+
}
|
|
22
|
+
export interface Flake {
|
|
23
|
+
readonly x: number;
|
|
24
|
+
readonly y: number;
|
|
25
|
+
readonly radius: number;
|
|
26
|
+
readonly speed: number;
|
|
27
|
+
readonly layer: Layer;
|
|
28
|
+
readonly phase: number;
|
|
29
|
+
readonly swaySpeed: number;
|
|
30
|
+
readonly crystal?: Crystal;
|
|
31
|
+
}
|
|
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[];
|
|
38
|
+
export declare function swayOffset(flake: Flake): number;
|
|
39
|
+
export declare function elapsedSeconds(previous: number | undefined, now: number): number;
|
|
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[];
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { between } from "../shared/random.js";
|
|
2
|
+
import { createCrystal } from "./crystals.js";
|
|
3
|
+
export const SHAPES = ["dot", "crystal"];
|
|
4
|
+
const AREA_PER_FLAKE = 5300;
|
|
5
|
+
export function flakeCount(bounds, amount) {
|
|
6
|
+
return Math.round(((bounds.width * bounds.height) / AREA_PER_FLAKE) * amount);
|
|
7
|
+
}
|
|
8
|
+
export const BACK = {
|
|
9
|
+
minRadius: 1,
|
|
10
|
+
maxRadius: 1.5,
|
|
11
|
+
minSpeed: 20,
|
|
12
|
+
maxSpeed: 40,
|
|
13
|
+
opacity: 0.4,
|
|
14
|
+
lineWidth: 0.75,
|
|
15
|
+
};
|
|
16
|
+
export const MIDDLE = {
|
|
17
|
+
minRadius: 1.5,
|
|
18
|
+
maxRadius: 2.5,
|
|
19
|
+
minSpeed: 40,
|
|
20
|
+
maxSpeed: 70,
|
|
21
|
+
opacity: 0.7,
|
|
22
|
+
lineWidth: 0.85,
|
|
23
|
+
};
|
|
24
|
+
export const FRONT = {
|
|
25
|
+
minRadius: 2.5,
|
|
26
|
+
maxRadius: 4,
|
|
27
|
+
minSpeed: 70,
|
|
28
|
+
maxSpeed: 110,
|
|
29
|
+
opacity: 0.95,
|
|
30
|
+
lineWidth: 1,
|
|
31
|
+
};
|
|
32
|
+
export const LAYERS = [BACK, MIDDLE, FRONT];
|
|
33
|
+
function pickLayer(pick) {
|
|
34
|
+
if (pick < 0.5) {
|
|
35
|
+
return BACK;
|
|
36
|
+
}
|
|
37
|
+
else if (pick < 0.8) {
|
|
38
|
+
return MIDDLE;
|
|
39
|
+
}
|
|
40
|
+
return FRONT;
|
|
41
|
+
}
|
|
42
|
+
const MIN_CRYSTAL_SWAY_SPEED = 1.25;
|
|
43
|
+
const MAX_CRYSTAL_SWAY_SPEED = 2.5;
|
|
44
|
+
export function createFlake(bounds, look, random) {
|
|
45
|
+
const layer = pickLayer(random());
|
|
46
|
+
const flake = {
|
|
47
|
+
x: random() * bounds.width,
|
|
48
|
+
y: random() * bounds.height,
|
|
49
|
+
radius: between(random, layer.minRadius, layer.maxRadius) * look.size,
|
|
50
|
+
speed: between(random, layer.minSpeed, layer.maxSpeed),
|
|
51
|
+
layer,
|
|
52
|
+
phase: random() * 2 * Math.PI,
|
|
53
|
+
swaySpeed: 0.5 + random(),
|
|
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
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function createFlakes(count, bounds, look, random) {
|
|
65
|
+
return Array.from({ length: count }, () => createFlake(bounds, look, random));
|
|
66
|
+
}
|
|
67
|
+
const SWAY_PX = 8;
|
|
68
|
+
export function swayOffset(flake) {
|
|
69
|
+
return Math.sin(flake.phase) * (flake.crystal?.sway ?? SWAY_PX);
|
|
70
|
+
}
|
|
71
|
+
const MAX_STEP_SECONDS = 0.1;
|
|
72
|
+
export function elapsedSeconds(previous, now) {
|
|
73
|
+
if (previous === undefined)
|
|
74
|
+
return 0;
|
|
75
|
+
return Math.min((now - previous) / 1000, MAX_STEP_SECONDS);
|
|
76
|
+
}
|
|
77
|
+
export function moveFlake(flake, seconds, speedFactor, bounds, look, random) {
|
|
78
|
+
const newY = flake.y + speedFactor * seconds * flake.speed;
|
|
79
|
+
if (newY - flake.radius > bounds.height) {
|
|
80
|
+
const fresh = createFlake(bounds, look, random);
|
|
81
|
+
return { ...fresh, y: -fresh.radius };
|
|
82
|
+
}
|
|
83
|
+
return { ...flake, y: newY, phase: flake.phase + flake.swaySpeed * seconds };
|
|
84
|
+
}
|
|
85
|
+
export function resizeFlakes(flakes, bounds, amount, look, random) {
|
|
86
|
+
const count = flakeCount(bounds, amount);
|
|
87
|
+
const firstFlakes = flakes.slice(0, count);
|
|
88
|
+
const keptFlakes = firstFlakes.map((flake) => {
|
|
89
|
+
if (flake.x >= bounds.width) {
|
|
90
|
+
return { ...flake, x: random() * bounds.width };
|
|
91
|
+
}
|
|
92
|
+
return flake;
|
|
93
|
+
});
|
|
94
|
+
const newFlakes = createFlakes(count - keptFlakes.length, bounds, look, random);
|
|
95
|
+
return [...keptFlakes, ...newFlakes];
|
|
96
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type CommonOptions } from "../shared/options.ts";
|
|
2
|
+
import type { Stop } from "../shared/stop.ts";
|
|
3
|
+
import { type Shape } from "./flakes.ts";
|
|
4
|
+
export interface SnowOptions extends CommonOptions {
|
|
5
|
+
color?: string;
|
|
6
|
+
amount?: number;
|
|
7
|
+
speed?: number;
|
|
8
|
+
shape?: Shape;
|
|
9
|
+
size?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function snow(options?: SnowOptions): Stop;
|