fancy-ui-vue 0.3.0 → 0.4.1
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/README.md +9 -6
- package/dist/components/animated-testimonials/AnimatedTestimonials.vue.js +23 -12
- package/dist/components/bg-falling-stars/FallingStarsBg.vue.js +10 -1
- package/dist/components/bg-falling-stars/bg-falling-stars-core.d.ts +6 -0
- package/dist/components/bg-falling-stars/bg-falling-stars-core.js +24 -2
- package/dist/components/bg-stars/StarsBackground.vue.js +1 -1
- package/dist/components/bg-stars/StarsBackground.vue2.js +13 -1
- package/dist/components/bg-stars/bg-stars-core.d.ts +6 -0
- package/dist/components/bg-stars/bg-stars-core.js +21 -1
- package/dist/components/book/Book.vue.js +5 -83
- package/dist/components/book/Book.vue2.js +82 -1
- package/dist/components/box-reveal/BoxReveal.vue.js +5 -47
- package/dist/components/box-reveal/BoxReveal.vue2.js +48 -1
- package/dist/components/compare/StarField.vue.js +1 -1
- package/dist/components/container-text-flip/ContainerTextFlip.vue.js +1 -1
- package/dist/components/container-text-flip/ContainerTextFlip.vue2.js +14 -5
- package/dist/components/flickering-grid/FlickeringGrid.vue.js +19 -1
- package/dist/components/flickering-grid/flickering-grid-core.d.ts +6 -0
- package/dist/components/flickering-grid/flickering-grid-core.js +30 -1
- package/dist/components/flip-words/FlipWords.vue.js +1 -1
- package/dist/components/flip-words/FlipWords.vue2.js +14 -7
- package/dist/components/hyper-text/HyperText.vue.js +6 -97
- package/dist/components/hyper-text/HyperText.vue2.js +101 -0
- package/dist/components/letter-pullup/LetterPullup.vue.js +6 -38
- package/dist/components/letter-pullup/LetterPullup.vue2.js +40 -0
- package/dist/components/logo-cloud/AnimatedLogoCloud.vue.js +1 -1
- package/dist/components/magnetic-field/MagnetPuck.vue.d.ts +51 -0
- package/dist/components/magnetic-field/MagnetPuck.vue.js +7 -0
- package/dist/components/magnetic-field/MagnetPuck.vue2.js +191 -0
- package/dist/components/magnetic-field/MagneticField.vue.d.ts +39 -0
- package/dist/components/magnetic-field/MagneticField.vue.js +7 -0
- package/dist/components/magnetic-field/MagneticField.vue2.js +157 -0
- package/dist/components/magnetic-field/MagneticItem.vue.d.ts +28 -0
- package/dist/components/magnetic-field/MagneticItem.vue.js +7 -0
- package/dist/components/magnetic-field/MagneticItem.vue2.js +72 -0
- package/dist/components/magnetic-field/index.d.ts +10 -0
- package/dist/components/magnetic-field/magnetic-field-core.d.ts +163 -0
- package/dist/components/magnetic-field/magnetic-field-core.js +534 -0
- package/dist/components/magnetic-field/magnetic-field-source.d.ts +58 -0
- package/dist/components/magnetic-field/magnetic-field-source.js +58 -0
- package/dist/components/magnetic-field/types.d.ts +14 -0
- package/dist/components/magnetic-field/types.js +5 -0
- package/dist/components/matrix-rain/MatrixRain.vue.js +20 -1
- package/dist/components/matrix-rain/matrix-rain-core.d.ts +6 -0
- package/dist/components/matrix-rain/matrix-rain-core.js +28 -6
- package/dist/components/noise-reveal/NoiseReveal.vue.js +7 -0
- package/dist/components/noise-reveal/noise-reveal-core.js +23 -7
- package/dist/components/pixel-scene/PixelScene.vue.d.ts +60 -0
- package/dist/components/pixel-scene/PixelScene.vue.js +184 -0
- package/dist/components/pixel-scene/PixelScene.vue2.js +4 -0
- package/dist/components/pixel-scene/index.d.ts +4 -0
- package/dist/components/pixel-scene/pixel-scene-core.d.ts +256 -0
- package/dist/components/pixel-scene/pixel-scene-core.js +878 -0
- package/dist/components/pixel-scene/pixel-scene-hdr.d.ts +68 -0
- package/dist/components/pixel-scene/pixel-scene-hdr.js +312 -0
- package/dist/components/pixel-scene/pixel-scene-scenes.d.ts +31 -0
- package/dist/components/pixel-scene/pixel-scene-scenes.js +1074 -0
- package/dist/components/ripple/Ripple.vue.js +1 -1
- package/dist/components/ripple/Ripple.vue2.js +6 -1
- package/dist/components/sparkles/Sparkles.vue.js +10 -1
- package/dist/components/sparkles/sparkles-core.d.ts +6 -0
- package/dist/components/sparkles/sparkles-core.js +26 -2
- package/dist/components/sparkles-text/SparklesText.vue.js +6 -88
- package/dist/components/sparkles-text/SparklesText.vue2.js +100 -0
- package/dist/components/story-thread/StoryNode.vue.d.ts +11 -0
- package/dist/components/story-thread/StoryNode.vue.js +7 -0
- package/dist/components/story-thread/StoryNode.vue2.js +23 -0
- package/dist/components/story-thread/StoryThread.vue.d.ts +56 -0
- package/dist/components/story-thread/StoryThread.vue.js +7 -0
- package/dist/components/story-thread/StoryThread.vue2.js +103 -0
- package/dist/components/story-thread/index.d.ts +5 -0
- package/dist/components/story-thread/story-thread-core.d.ts +119 -0
- package/dist/components/story-thread/story-thread-core.js +491 -0
- package/dist/components/story-thread/types.d.ts +1 -0
- package/dist/components/sun-switch/SunSwitch.vue.d.ts +61 -0
- package/dist/components/sun-switch/SunSwitch.vue.js +7 -0
- package/dist/components/sun-switch/SunSwitch.vue2.js +559 -0
- package/dist/components/sun-switch/index.d.ts +4 -0
- package/dist/components/sun-switch/pixel-wave-core.d.ts +82 -0
- package/dist/components/sun-switch/pixel-wave-core.js +508 -0
- package/dist/components/sun-switch/sun-switch-core.d.ts +251 -0
- package/dist/components/sun-switch/sun-switch-core.js +546 -0
- package/dist/components/sun-switch/sun-switch-hdr.d.ts +70 -0
- package/dist/components/sun-switch/sun-switch-hdr.js +311 -0
- package/dist/components/terminal-text/TerminalText.vue.js +1 -1
- package/dist/components/terminal-text/TerminalText.vue2.js +36 -4
- package/dist/components/text-generate-effect/TextGenerateEffect.vue.js +5 -72
- package/dist/components/text-generate-effect/TextGenerateEffect.vue2.js +74 -1
- package/dist/components/text-reveal-card/TextRevealCard.vue.js +5 -101
- package/dist/components/text-reveal-card/TextRevealCard.vue2.js +98 -1
- package/dist/components/text-reveal-card/TextRevealStars.vue.js +1 -1
- package/dist/components/text-reveal-card/TextRevealStars.vue2.js +29 -12
- package/dist/components/tracing-beam/TracingBeam.vue.d.ts +20 -1
- package/dist/components/tracing-beam/TracingBeam.vue.js +5 -164
- package/dist/components/tracing-beam/TracingBeam.vue2.js +168 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +361 -338
- package/dist/styles.css +731 -89
- package/package.json +1 -1
- package/dist/components/hyper-text/HyperText.vue3.js +0 -5
- package/dist/components/letter-pullup/LetterPullup.vue3.js +0 -5
- package/dist/components/sparkles-text/SparklesText.vue3.js +0 -5
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MagneticField engine — framework-free, byte-identical in the Svelte, React
|
|
3
|
+
* and Vue trees.
|
|
4
|
+
*
|
|
5
|
+
* Every `MagneticItem` is a static outer node (measured, never transformed)
|
|
6
|
+
* around an inner node that reads three CSS custom properties:
|
|
7
|
+
* `--ft-mf-x` / `--ft-mf-y` (px) and `--ft-mf-r` (deg). This core owns those
|
|
8
|
+
* properties. It keeps one magnet point per input channel, works out a
|
|
9
|
+
* target offset and lean for each item from its distance to the magnet, and
|
|
10
|
+
* steps three springs per item (x, y, rotation) toward it in a single
|
|
11
|
+
* `requestAnimationFrame` loop shared by the whole field. The loop sleeps as
|
|
12
|
+
* soon as every spring is at rest, so a still field schedules nothing.
|
|
13
|
+
*
|
|
14
|
+
* Each frame reads first (the root rect and any stale item rects), then
|
|
15
|
+
* computes targets, then steps, then writes, so no frame forces a reflow per
|
|
16
|
+
* item. Geometry is in field-local px measured from the root's PADDING box,
|
|
17
|
+
* the same box an absolutely positioned child of the root lives in (scrolled
|
|
18
|
+
* content included, when the root itself scrolls).
|
|
19
|
+
*
|
|
20
|
+
* Nothing here touches a browser global at import time; `ResizeObserver`,
|
|
21
|
+
* `requestAnimationFrame` and `performance` are only reached inside functions.
|
|
22
|
+
*/
|
|
23
|
+
export interface MagneticPoint {
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
}
|
|
27
|
+
export interface MagneticSpring {
|
|
28
|
+
stiffness: number;
|
|
29
|
+
damping: number;
|
|
30
|
+
mass: number;
|
|
31
|
+
}
|
|
32
|
+
export type MagneticSpringPreset = "soft" | "default" | "snappy";
|
|
33
|
+
/** Where the field's own magnet comes from (a `MagnetPuck` can drive it in either mode). */
|
|
34
|
+
export type MagneticFieldSource = "pointer" | "manual";
|
|
35
|
+
/** The input channels, in falling priority: puck, then manual, then pointer. */
|
|
36
|
+
export type MagneticSourceChannel = "pointer" | "manual" | "puck";
|
|
37
|
+
/**
|
|
38
|
+
* Spring presets. `default` is near-critical (no visible overshoot, settles in
|
|
39
|
+
* about a third of a second); `soft` overshoots a little; `snappy` is quicker.
|
|
40
|
+
*/
|
|
41
|
+
export declare const MAGNETIC_FIELD_SPRINGS: Readonly<Record<MagneticSpringPreset, Readonly<MagneticSpring>>>;
|
|
42
|
+
export declare const MAGNETIC_FIELD_DEFAULTS: Readonly<{
|
|
43
|
+
radius: 220;
|
|
44
|
+
strength: 1;
|
|
45
|
+
maxTranslate: 24;
|
|
46
|
+
maxRotate: 6;
|
|
47
|
+
polarity: 1 | -1;
|
|
48
|
+
}>;
|
|
49
|
+
export interface MagneticItemOptions {
|
|
50
|
+
/** Heavier items move and lean less and settle more slowly. Clamped to [0.25, 10]. */
|
|
51
|
+
mass: number;
|
|
52
|
+
/** Per-item pull multiplier. */
|
|
53
|
+
strength: number;
|
|
54
|
+
/** Whether the item moves toward the magnet. */
|
|
55
|
+
pull: boolean;
|
|
56
|
+
/** Whether the item leans toward the magnet. */
|
|
57
|
+
rotate: boolean;
|
|
58
|
+
}
|
|
59
|
+
export interface MagneticFieldOptions {
|
|
60
|
+
radius: number;
|
|
61
|
+
strength: number;
|
|
62
|
+
maxTranslate: number;
|
|
63
|
+
maxRotate: number;
|
|
64
|
+
spring: MagneticSpring;
|
|
65
|
+
polarity: 1 | -1;
|
|
66
|
+
reducedMotion: boolean;
|
|
67
|
+
disabled: boolean;
|
|
68
|
+
onActiveChange?: (active: boolean) => void;
|
|
69
|
+
}
|
|
70
|
+
/** Item rect in field-local px (relative to the root's PADDING box). */
|
|
71
|
+
export interface MagneticRect {
|
|
72
|
+
left: number;
|
|
73
|
+
top: number;
|
|
74
|
+
width: number;
|
|
75
|
+
height: number;
|
|
76
|
+
}
|
|
77
|
+
export interface MagneticTarget {
|
|
78
|
+
/** Translation in px. */
|
|
79
|
+
x: number;
|
|
80
|
+
y: number;
|
|
81
|
+
/** Lean in degrees, positive = clockwise. */
|
|
82
|
+
r: number;
|
|
83
|
+
/** Translation falloff, 0..1. */
|
|
84
|
+
f: number;
|
|
85
|
+
}
|
|
86
|
+
/** A preset name or a partial config, completed from `default`. */
|
|
87
|
+
export declare function resolveSpring(spring?: MagneticSpringPreset | Partial<MagneticSpring>): MagneticSpring;
|
|
88
|
+
/**
|
|
89
|
+
* The spring an item of `mass` actually runs: mass scales the inertia and
|
|
90
|
+
* damping scales with its square root, so every item keeps the same damping
|
|
91
|
+
* ratio and only its tempo changes. `rotation` doubles the stiffness (and
|
|
92
|
+
* scales damping by √2) so the lean leads the pull, again at the same ratio.
|
|
93
|
+
*/
|
|
94
|
+
export declare function itemSpring(base: MagneticSpring, mass: number, rotation?: boolean): MagneticSpring;
|
|
95
|
+
/** Distance from `p` to the nearest edge of `r`; 0 inside. */
|
|
96
|
+
export declare function rectDistance(p: MagneticPoint, r: MagneticRect): number;
|
|
97
|
+
/** `smoothstep(1 - d / radius)`: 1 at or inside the edge, 0 at `radius` and beyond. */
|
|
98
|
+
export declare function falloff(d: number, radius: number): number;
|
|
99
|
+
/**
|
|
100
|
+
* Where an item wants to be for a magnet at `p` (null: no magnet, rest).
|
|
101
|
+
*
|
|
102
|
+
* Pull: toward the magnet along the line from the item's centre, scaled by the
|
|
103
|
+
* edge-distance falloff, the strengths and `1 / mass`, capped at
|
|
104
|
+
* `maxTranslate`, and faded to 0 within `max(16, maxTranslate)` px of the
|
|
105
|
+
* centre so the item never jumps past the magnet nor flips direction as the
|
|
106
|
+
* magnet crosses its middle. `polarity` flips it.
|
|
107
|
+
*
|
|
108
|
+
* Lean: the item tilts toward the magnet's horizontal side, like a crowd
|
|
109
|
+
* turning its head: clockwise when the magnet is to its left, counter-clockwise
|
|
110
|
+
* to its right, whatever the height. It saturates quickly, so items lean
|
|
111
|
+
* almost fully while barely moving, and its falloff reaches twice as far as the
|
|
112
|
+
* pull's. `polarity` does not flip it.
|
|
113
|
+
*/
|
|
114
|
+
export declare function itemTarget(p: MagneticPoint | null, r: MagneticRect, field: Pick<MagneticFieldOptions, "radius" | "strength" | "maxTranslate" | "maxRotate" | "polarity">, item: MagneticItemOptions): MagneticTarget;
|
|
115
|
+
/** One spring: position, velocity, target. */
|
|
116
|
+
export interface MagneticSpringState {
|
|
117
|
+
x: number;
|
|
118
|
+
v: number;
|
|
119
|
+
target: number;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Advances a spring by `dt` seconds (clamped to 1/30) in fixed 1/240 s
|
|
123
|
+
* semi-implicit Euler substeps. Snaps to the target and returns false once
|
|
124
|
+
* within `rest` with a speed under `rest * 10`; true while still moving.
|
|
125
|
+
*/
|
|
126
|
+
export declare function stepSpring(s: MagneticSpringState, cfg: MagneticSpring, dt: number, rest: number): boolean;
|
|
127
|
+
export interface MagneticFieldCore {
|
|
128
|
+
/** Sets a channel's magnet in field-local px (`null` clears it). Channel defaults to `"manual"`. */
|
|
129
|
+
setSource(point: MagneticPoint | null, channel?: MagneticSourceChannel): void;
|
|
130
|
+
/** Sets a channel's magnet in client px. Channel defaults to `"pointer"`. */
|
|
131
|
+
setClientSource(clientX: number, clientY: number, channel?: MagneticSourceChannel): void;
|
|
132
|
+
/** Clears one channel, or every channel when called without one. */
|
|
133
|
+
clearSource(channel?: MagneticSourceChannel): void;
|
|
134
|
+
/** Starts driving an item. Returns its unregister function. */
|
|
135
|
+
register(outer: HTMLElement, inner: HTMLElement, opts?: Partial<MagneticItemOptions>): () => void;
|
|
136
|
+
updateItem(outer: HTMLElement, opts: Partial<MagneticItemOptions>): void;
|
|
137
|
+
update(opts: Partial<MagneticFieldOptions>): void;
|
|
138
|
+
/** Marks every cached rect stale (after a scroll or resize). */
|
|
139
|
+
invalidate(): void;
|
|
140
|
+
/** Whether any item is pulled or still settling. */
|
|
141
|
+
readonly active: boolean;
|
|
142
|
+
/** Stops the loop and the observer and leaves every item at rest. */
|
|
143
|
+
destroy(): void;
|
|
144
|
+
}
|
|
145
|
+
export declare function createMagneticFieldCore(root: HTMLElement, initial?: Partial<MagneticFieldOptions>): MagneticFieldCore;
|
|
146
|
+
/**
|
|
147
|
+
* The context value a field hands its items and pucks: a registration buffer
|
|
148
|
+
* that outlives the core. Items and pucks may mount before the field's core
|
|
149
|
+
* exists (child effects run first in every framework) and may outlive it (a
|
|
150
|
+
* strict-mode remount destroys and recreates the core); the hub replays every
|
|
151
|
+
* registered item and the last point of each channel into whichever core is
|
|
152
|
+
* attached.
|
|
153
|
+
*/
|
|
154
|
+
export interface MagneticFieldHub {
|
|
155
|
+
register(outer: HTMLElement, inner: HTMLElement, opts?: Partial<MagneticItemOptions>): () => void;
|
|
156
|
+
updateItem(outer: HTMLElement, opts: Partial<MagneticItemOptions>): void;
|
|
157
|
+
setSource(point: MagneticPoint | null, channel: MagneticSourceChannel): void;
|
|
158
|
+
/** Hands the buffer to `core`: replays every item and the last point of each channel. */
|
|
159
|
+
attach(core: MagneticFieldCore): void;
|
|
160
|
+
/** Forgets the core (about to be destroyed). The buffer is kept. */
|
|
161
|
+
detach(): void;
|
|
162
|
+
}
|
|
163
|
+
export declare function createMagneticFieldHub(): MagneticFieldHub;
|
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
const MAGNETIC_FIELD_SPRINGS = Object.freeze({
|
|
2
|
+
soft: Object.freeze({ stiffness: 170, damping: 18, mass: 1 }),
|
|
3
|
+
default: Object.freeze({ stiffness: 230, damping: 30, mass: 1 }),
|
|
4
|
+
snappy: Object.freeze({ stiffness: 320, damping: 36, mass: 1 })
|
|
5
|
+
});
|
|
6
|
+
const MAGNETIC_FIELD_DEFAULTS = Object.freeze({
|
|
7
|
+
radius: 220,
|
|
8
|
+
strength: 1,
|
|
9
|
+
maxTranslate: 24,
|
|
10
|
+
maxRotate: 6,
|
|
11
|
+
polarity: 1
|
|
12
|
+
});
|
|
13
|
+
const SUBSTEP = 1 / 240;
|
|
14
|
+
const MAX_DT = 1 / 30;
|
|
15
|
+
const FIRST_DT = 1 / 60;
|
|
16
|
+
const REST_POSITION = 0.01;
|
|
17
|
+
const MIN_ITEM_MASS = 0.25;
|
|
18
|
+
const MAX_ITEM_MASS = 10;
|
|
19
|
+
const MIN_CORE = 16;
|
|
20
|
+
const LEAN_GAIN = 3;
|
|
21
|
+
const LEAN_REACH = 2;
|
|
22
|
+
const MIN_VISIBLE = 0.05;
|
|
23
|
+
const DEFAULT_ITEM = Object.freeze({
|
|
24
|
+
mass: 1,
|
|
25
|
+
strength: 1,
|
|
26
|
+
pull: true,
|
|
27
|
+
rotate: true
|
|
28
|
+
});
|
|
29
|
+
const ZERO = Object.freeze({ x: 0, y: 0, r: 0, f: 0 });
|
|
30
|
+
function clamp(v, lo, hi) {
|
|
31
|
+
return v < lo ? lo : v > hi ? hi : v;
|
|
32
|
+
}
|
|
33
|
+
function finite(v, fallback) {
|
|
34
|
+
return Number.isFinite(v) ? v : fallback;
|
|
35
|
+
}
|
|
36
|
+
function smoothstep(x) {
|
|
37
|
+
const t = clamp(x, 0, 1);
|
|
38
|
+
return t * t * (3 - 2 * t);
|
|
39
|
+
}
|
|
40
|
+
function itemMass(mass) {
|
|
41
|
+
return clamp(finite(mass, 1), MIN_ITEM_MASS, MAX_ITEM_MASS);
|
|
42
|
+
}
|
|
43
|
+
function resolveSpring(spring) {
|
|
44
|
+
const base = MAGNETIC_FIELD_SPRINGS.default;
|
|
45
|
+
if (spring === void 0 || spring === null) return { ...base };
|
|
46
|
+
if (typeof spring === "string") return { ...MAGNETIC_FIELD_SPRINGS[spring] ?? base };
|
|
47
|
+
const stiffness = finite(spring.stiffness ?? base.stiffness, base.stiffness);
|
|
48
|
+
const damping = finite(spring.damping ?? base.damping, base.damping);
|
|
49
|
+
const mass = finite(spring.mass ?? base.mass, base.mass);
|
|
50
|
+
return {
|
|
51
|
+
stiffness: Math.max(1e-3, stiffness),
|
|
52
|
+
damping: Math.max(0, damping),
|
|
53
|
+
mass: Math.max(1e-3, mass)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function itemSpring(base, mass, rotation = false) {
|
|
57
|
+
const m = itemMass(mass);
|
|
58
|
+
const k = rotation ? 2 : 1;
|
|
59
|
+
return {
|
|
60
|
+
stiffness: base.stiffness * k,
|
|
61
|
+
damping: base.damping * Math.sqrt(m) * Math.sqrt(k),
|
|
62
|
+
mass: base.mass * m
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function rectDistance(p, r) {
|
|
66
|
+
const dx = Math.max(r.left - p.x, 0, p.x - (r.left + r.width));
|
|
67
|
+
const dy = Math.max(r.top - p.y, 0, p.y - (r.top + r.height));
|
|
68
|
+
return Math.hypot(dx, dy);
|
|
69
|
+
}
|
|
70
|
+
function falloff(d, radius) {
|
|
71
|
+
return radius > 0 ? smoothstep(1 - d / radius) : 0;
|
|
72
|
+
}
|
|
73
|
+
function itemTarget(p, r, field, item) {
|
|
74
|
+
if (!p) return { ...ZERO };
|
|
75
|
+
const d = rectDistance(p, r);
|
|
76
|
+
const f = falloff(d, field.radius);
|
|
77
|
+
const fr = falloff(d, field.radius * LEAN_REACH);
|
|
78
|
+
if (fr <= 0) return { ...ZERO };
|
|
79
|
+
const m = itemMass(item.mass);
|
|
80
|
+
const gain = Math.max(0, finite(field.strength, 0) * finite(item.strength, 0)) / m;
|
|
81
|
+
const maxT = Math.max(0, finite(field.maxTranslate, 0));
|
|
82
|
+
const maxR = Math.max(0, finite(field.maxRotate, 0));
|
|
83
|
+
const cx = r.left + r.width / 2;
|
|
84
|
+
const cy = r.top + r.height / 2;
|
|
85
|
+
const dx = p.x - cx;
|
|
86
|
+
const dy = p.y - cy;
|
|
87
|
+
const len = Math.hypot(dx, dy);
|
|
88
|
+
let x = 0;
|
|
89
|
+
let y = 0;
|
|
90
|
+
let rot = 0;
|
|
91
|
+
if (item.pull && len > 0 && f > 0) {
|
|
92
|
+
const core = Math.max(MIN_CORE, maxT);
|
|
93
|
+
const k = Math.min(maxT, maxT * gain * f) * Math.min(1, len / core);
|
|
94
|
+
const sign = field.polarity === -1 ? -1 : 1;
|
|
95
|
+
x = sign * k * dx / len;
|
|
96
|
+
y = sign * k * dy / len;
|
|
97
|
+
}
|
|
98
|
+
if (item.rotate && len > 0) {
|
|
99
|
+
const scale = Math.max(len, r.width / 2);
|
|
100
|
+
const side = Math.tanh(LEAN_GAIN * -dx / scale);
|
|
101
|
+
rot = clamp(maxR * gain * side * fr, -maxR, maxR);
|
|
102
|
+
}
|
|
103
|
+
if (Math.hypot(x, y) < MIN_VISIBLE) {
|
|
104
|
+
x = 0;
|
|
105
|
+
y = 0;
|
|
106
|
+
}
|
|
107
|
+
if (Math.abs(rot) < MIN_VISIBLE) rot = 0;
|
|
108
|
+
return { x: x || 0, y: y || 0, r: rot || 0, f };
|
|
109
|
+
}
|
|
110
|
+
function stepSpring(s, cfg, dt, rest) {
|
|
111
|
+
let left = Math.min(Math.max(dt, 0), MAX_DT);
|
|
112
|
+
while (left > 1e-9) {
|
|
113
|
+
const h = Math.min(SUBSTEP, left);
|
|
114
|
+
const a = (-cfg.stiffness * (s.x - s.target) - cfg.damping * s.v) / cfg.mass;
|
|
115
|
+
s.v += a * h;
|
|
116
|
+
s.x += s.v * h;
|
|
117
|
+
left -= h;
|
|
118
|
+
}
|
|
119
|
+
if (Math.abs(s.x - s.target) < rest && Math.abs(s.v) < rest * 10) {
|
|
120
|
+
s.x = s.target;
|
|
121
|
+
s.v = 0;
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
const CHANNELS = ["puck", "manual", "pointer"];
|
|
127
|
+
function px(v) {
|
|
128
|
+
return `${(v || 0).toFixed(2)}px`;
|
|
129
|
+
}
|
|
130
|
+
function deg(v) {
|
|
131
|
+
return `${(v || 0).toFixed(2)}deg`;
|
|
132
|
+
}
|
|
133
|
+
function sameItemOptions(a, b) {
|
|
134
|
+
return a.mass === b.mass && a.strength === b.strength && a.pull === b.pull && a.rotate === b.rotate;
|
|
135
|
+
}
|
|
136
|
+
function normaliseItem(opts) {
|
|
137
|
+
return {
|
|
138
|
+
mass: opts?.mass ?? DEFAULT_ITEM.mass,
|
|
139
|
+
strength: opts?.strength ?? DEFAULT_ITEM.strength,
|
|
140
|
+
pull: opts?.pull ?? DEFAULT_ITEM.pull,
|
|
141
|
+
rotate: opts?.rotate ?? DEFAULT_ITEM.rotate
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function createMagneticFieldCore(root, initial) {
|
|
145
|
+
const options = {
|
|
146
|
+
radius: initial?.radius ?? MAGNETIC_FIELD_DEFAULTS.radius,
|
|
147
|
+
strength: initial?.strength ?? MAGNETIC_FIELD_DEFAULTS.strength,
|
|
148
|
+
maxTranslate: initial?.maxTranslate ?? MAGNETIC_FIELD_DEFAULTS.maxTranslate,
|
|
149
|
+
maxRotate: initial?.maxRotate ?? MAGNETIC_FIELD_DEFAULTS.maxRotate,
|
|
150
|
+
spring: resolveSpring(initial?.spring),
|
|
151
|
+
polarity: initial?.polarity === -1 ? -1 : 1,
|
|
152
|
+
reducedMotion: initial?.reducedMotion ?? false,
|
|
153
|
+
disabled: initial?.disabled ?? false,
|
|
154
|
+
onActiveChange: initial?.onActiveChange
|
|
155
|
+
};
|
|
156
|
+
const items = /* @__PURE__ */ new Map();
|
|
157
|
+
const channels = {
|
|
158
|
+
puck: null,
|
|
159
|
+
manual: null,
|
|
160
|
+
pointer: null
|
|
161
|
+
};
|
|
162
|
+
let originX = 0;
|
|
163
|
+
let originY = 0;
|
|
164
|
+
let rootDirty = true;
|
|
165
|
+
let targetsDirty = true;
|
|
166
|
+
let frame = 0;
|
|
167
|
+
let lastT = 0;
|
|
168
|
+
let active = false;
|
|
169
|
+
let destroyed = false;
|
|
170
|
+
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => invalidate());
|
|
171
|
+
observer?.observe(root);
|
|
172
|
+
function anyChannel() {
|
|
173
|
+
return channels.puck !== null || channels.manual !== null || channels.pointer !== null;
|
|
174
|
+
}
|
|
175
|
+
function magnet() {
|
|
176
|
+
if (options.disabled) return null;
|
|
177
|
+
for (const name of CHANNELS) {
|
|
178
|
+
const c = channels[name];
|
|
179
|
+
if (c) return c.client ? { x: c.x - originX, y: c.y - originY } : { x: c.x, y: c.y };
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
function setActive(next) {
|
|
184
|
+
if (next === active) return;
|
|
185
|
+
active = next;
|
|
186
|
+
options.onActiveChange?.(next);
|
|
187
|
+
}
|
|
188
|
+
function write(item) {
|
|
189
|
+
const x = px(item.sx.x);
|
|
190
|
+
const y = px(item.sy.x);
|
|
191
|
+
const r = deg(item.sr.x);
|
|
192
|
+
const style = item.inner.style;
|
|
193
|
+
if (x !== item.wx) style.setProperty("--ft-mf-x", item.wx = x);
|
|
194
|
+
if (y !== item.wy) style.setProperty("--ft-mf-y", item.wy = y);
|
|
195
|
+
if (r !== item.wr) style.setProperty("--ft-mf-r", item.wr = r);
|
|
196
|
+
}
|
|
197
|
+
function setPromoted(item, promoted) {
|
|
198
|
+
if (promoted === item.promoted) return;
|
|
199
|
+
item.promoted = promoted;
|
|
200
|
+
item.inner.style.willChange = promoted ? "transform" : "";
|
|
201
|
+
}
|
|
202
|
+
function resetItem(item) {
|
|
203
|
+
for (const s of [item.sx, item.sy, item.sr]) {
|
|
204
|
+
s.x = 0;
|
|
205
|
+
s.v = 0;
|
|
206
|
+
s.target = 0;
|
|
207
|
+
}
|
|
208
|
+
write(item);
|
|
209
|
+
setPromoted(item, false);
|
|
210
|
+
}
|
|
211
|
+
function snapAll() {
|
|
212
|
+
cancel();
|
|
213
|
+
for (const item of items.values()) resetItem(item);
|
|
214
|
+
targetsDirty = true;
|
|
215
|
+
setActive(false);
|
|
216
|
+
}
|
|
217
|
+
function cancel() {
|
|
218
|
+
if (frame) cancelAnimationFrame(frame);
|
|
219
|
+
frame = 0;
|
|
220
|
+
lastT = 0;
|
|
221
|
+
}
|
|
222
|
+
function kick() {
|
|
223
|
+
if (destroyed) return;
|
|
224
|
+
if (options.reducedMotion) {
|
|
225
|
+
snapAll();
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (!frame && typeof requestAnimationFrame === "function") {
|
|
229
|
+
frame = requestAnimationFrame(step);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function measure() {
|
|
233
|
+
if (rootDirty) {
|
|
234
|
+
const r = root.getBoundingClientRect();
|
|
235
|
+
originX = r.left + (root.clientLeft || 0) - (root.scrollLeft || 0);
|
|
236
|
+
originY = r.top + (root.clientTop || 0) - (root.scrollTop || 0);
|
|
237
|
+
rootDirty = false;
|
|
238
|
+
}
|
|
239
|
+
for (const item of items.values()) {
|
|
240
|
+
if (!item.rectDirty) continue;
|
|
241
|
+
const r = item.outer.getBoundingClientRect();
|
|
242
|
+
item.rect = {
|
|
243
|
+
left: r.left - originX,
|
|
244
|
+
top: r.top - originY,
|
|
245
|
+
width: r.width,
|
|
246
|
+
height: r.height
|
|
247
|
+
};
|
|
248
|
+
item.rectDirty = false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function retarget() {
|
|
252
|
+
const p = magnet();
|
|
253
|
+
for (const item of items.values()) {
|
|
254
|
+
const t = item.rect && p ? itemTarget(p, item.rect, options, item.opts) : ZERO;
|
|
255
|
+
item.sx.target = t.x;
|
|
256
|
+
item.sy.target = t.y;
|
|
257
|
+
item.sr.target = t.r;
|
|
258
|
+
}
|
|
259
|
+
targetsDirty = false;
|
|
260
|
+
}
|
|
261
|
+
function step() {
|
|
262
|
+
frame = 0;
|
|
263
|
+
if (destroyed) return;
|
|
264
|
+
const t = performance.now();
|
|
265
|
+
const dt = lastT ? Math.min(MAX_DT, (t - lastT) / 1e3) : FIRST_DT;
|
|
266
|
+
if (!lastT) markStale();
|
|
267
|
+
lastT = t;
|
|
268
|
+
measure();
|
|
269
|
+
if (targetsDirty) retarget();
|
|
270
|
+
let moving = false;
|
|
271
|
+
let anyEngaged = false;
|
|
272
|
+
for (const item of items.values()) {
|
|
273
|
+
let itemMoving = false;
|
|
274
|
+
if (stepSpring(item.sx, item.cfg, dt, REST_POSITION)) itemMoving = true;
|
|
275
|
+
if (stepSpring(item.sy, item.cfg, dt, REST_POSITION)) itemMoving = true;
|
|
276
|
+
if (stepSpring(item.sr, item.cfgR, dt, REST_POSITION)) itemMoving = true;
|
|
277
|
+
write(item);
|
|
278
|
+
setPromoted(item, itemMoving);
|
|
279
|
+
const engaged2 = itemMoving || item.sx.target !== 0 || item.sy.target !== 0 || item.sr.target !== 0;
|
|
280
|
+
if (itemMoving) moving = true;
|
|
281
|
+
if (engaged2) anyEngaged = true;
|
|
282
|
+
}
|
|
283
|
+
if (moving || targetsDirty) frame = requestAnimationFrame(step);
|
|
284
|
+
else lastT = 0;
|
|
285
|
+
setActive(anyEngaged);
|
|
286
|
+
}
|
|
287
|
+
function shadowed(channel) {
|
|
288
|
+
for (const name of CHANNELS) {
|
|
289
|
+
if (name === channel) return false;
|
|
290
|
+
if (channels[name] !== null) return true;
|
|
291
|
+
}
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
function setChannel(channel, next) {
|
|
295
|
+
if (destroyed) return;
|
|
296
|
+
const prev = channels[channel];
|
|
297
|
+
if (prev === next) return;
|
|
298
|
+
if (prev && next && prev.x === next.x && prev.y === next.y && prev.client === next.client) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
channels[channel] = next;
|
|
302
|
+
if (shadowed(channel)) return;
|
|
303
|
+
targetsDirty = true;
|
|
304
|
+
if (!active && !frame && outOfReach(prev) && outOfReach(next)) return;
|
|
305
|
+
kick();
|
|
306
|
+
}
|
|
307
|
+
function outOfReach(c) {
|
|
308
|
+
if (!c) return true;
|
|
309
|
+
if (c.client && rootDirty) return false;
|
|
310
|
+
const p = c.client ? { x: c.x - originX, y: c.y - originY } : { x: c.x, y: c.y };
|
|
311
|
+
const reach = options.radius * LEAN_REACH;
|
|
312
|
+
for (const item of items.values()) {
|
|
313
|
+
if (item.rectDirty || !item.rect) return false;
|
|
314
|
+
if (rectDistance(p, item.rect) < reach) return false;
|
|
315
|
+
}
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
function markStale() {
|
|
319
|
+
rootDirty = true;
|
|
320
|
+
for (const item of items.values()) item.rectDirty = true;
|
|
321
|
+
targetsDirty = true;
|
|
322
|
+
}
|
|
323
|
+
function engaged(item) {
|
|
324
|
+
for (const s of [item.sx, item.sy, item.sr]) {
|
|
325
|
+
if (s.target !== 0 || s.x !== s.target || s.v !== 0) return true;
|
|
326
|
+
}
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
function invalidate() {
|
|
330
|
+
if (destroyed) return;
|
|
331
|
+
markStale();
|
|
332
|
+
if (anyChannel() || active) kick();
|
|
333
|
+
}
|
|
334
|
+
function updateSprings(item) {
|
|
335
|
+
item.cfg = itemSpring(options.spring, item.opts.mass);
|
|
336
|
+
item.cfgR = itemSpring(options.spring, item.opts.mass, true);
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
setSource(point, channel = "manual") {
|
|
340
|
+
setChannel(
|
|
341
|
+
channel,
|
|
342
|
+
point && Number.isFinite(point.x) && Number.isFinite(point.y) ? { x: point.x, y: point.y, client: false } : null
|
|
343
|
+
);
|
|
344
|
+
},
|
|
345
|
+
setClientSource(clientX, clientY, channel = "pointer") {
|
|
346
|
+
if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) return;
|
|
347
|
+
setChannel(channel, { x: clientX, y: clientY, client: true });
|
|
348
|
+
},
|
|
349
|
+
clearSource(channel) {
|
|
350
|
+
if (channel) {
|
|
351
|
+
setChannel(channel, null);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
for (const name of CHANNELS) setChannel(name, null);
|
|
355
|
+
},
|
|
356
|
+
register(outer, inner, opts) {
|
|
357
|
+
if (destroyed) return () => {
|
|
358
|
+
};
|
|
359
|
+
const existing = items.get(outer);
|
|
360
|
+
if (existing && existing.inner !== inner) resetItem(existing);
|
|
361
|
+
const item = {
|
|
362
|
+
outer,
|
|
363
|
+
inner,
|
|
364
|
+
opts: normaliseItem(opts),
|
|
365
|
+
rect: null,
|
|
366
|
+
rectDirty: true,
|
|
367
|
+
sx: existing && existing.inner === inner ? existing.sx : { x: 0, v: 0, target: 0 },
|
|
368
|
+
sy: existing && existing.inner === inner ? existing.sy : { x: 0, v: 0, target: 0 },
|
|
369
|
+
sr: existing && existing.inner === inner ? existing.sr : { x: 0, v: 0, target: 0 },
|
|
370
|
+
cfg: options.spring,
|
|
371
|
+
cfgR: options.spring,
|
|
372
|
+
wx: "",
|
|
373
|
+
wy: "",
|
|
374
|
+
wr: "",
|
|
375
|
+
promoted: false
|
|
376
|
+
};
|
|
377
|
+
updateSprings(item);
|
|
378
|
+
items.set(outer, item);
|
|
379
|
+
write(item);
|
|
380
|
+
observer?.observe(outer);
|
|
381
|
+
targetsDirty = true;
|
|
382
|
+
if (anyChannel() || active) kick();
|
|
383
|
+
return () => {
|
|
384
|
+
if (items.get(outer) !== item) return;
|
|
385
|
+
items.delete(outer);
|
|
386
|
+
observer?.unobserve(outer);
|
|
387
|
+
resetItem(item);
|
|
388
|
+
if (!frame) setActive([...items.values()].some(engaged));
|
|
389
|
+
};
|
|
390
|
+
},
|
|
391
|
+
updateItem(outer, opts) {
|
|
392
|
+
const item = items.get(outer);
|
|
393
|
+
if (!item || destroyed) return;
|
|
394
|
+
const next = normaliseItem({ ...item.opts, ...opts });
|
|
395
|
+
if (sameItemOptions(item.opts, next)) return;
|
|
396
|
+
const massChanged = next.mass !== item.opts.mass;
|
|
397
|
+
item.opts = next;
|
|
398
|
+
if (massChanged) updateSprings(item);
|
|
399
|
+
targetsDirty = true;
|
|
400
|
+
if (anyChannel() || active) kick();
|
|
401
|
+
},
|
|
402
|
+
update(opts) {
|
|
403
|
+
if (destroyed) return;
|
|
404
|
+
const wasReduced = options.reducedMotion;
|
|
405
|
+
let changed = false;
|
|
406
|
+
if (opts.radius !== void 0 && opts.radius !== options.radius) {
|
|
407
|
+
options.radius = opts.radius;
|
|
408
|
+
changed = true;
|
|
409
|
+
}
|
|
410
|
+
if (opts.strength !== void 0 && opts.strength !== options.strength) {
|
|
411
|
+
options.strength = opts.strength;
|
|
412
|
+
changed = true;
|
|
413
|
+
}
|
|
414
|
+
if (opts.maxTranslate !== void 0 && opts.maxTranslate !== options.maxTranslate) {
|
|
415
|
+
options.maxTranslate = opts.maxTranslate;
|
|
416
|
+
changed = true;
|
|
417
|
+
}
|
|
418
|
+
if (opts.maxRotate !== void 0 && opts.maxRotate !== options.maxRotate) {
|
|
419
|
+
options.maxRotate = opts.maxRotate;
|
|
420
|
+
changed = true;
|
|
421
|
+
}
|
|
422
|
+
if (opts.polarity !== void 0) {
|
|
423
|
+
const polarity = opts.polarity === -1 ? -1 : 1;
|
|
424
|
+
if (polarity !== options.polarity) {
|
|
425
|
+
options.polarity = polarity;
|
|
426
|
+
changed = true;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (opts.disabled !== void 0 && opts.disabled !== options.disabled) {
|
|
430
|
+
options.disabled = opts.disabled;
|
|
431
|
+
changed = true;
|
|
432
|
+
}
|
|
433
|
+
if (opts.reducedMotion !== void 0 && opts.reducedMotion !== options.reducedMotion) {
|
|
434
|
+
options.reducedMotion = opts.reducedMotion;
|
|
435
|
+
changed = true;
|
|
436
|
+
}
|
|
437
|
+
if (opts.spring !== void 0) {
|
|
438
|
+
const spring = resolveSpring(opts.spring);
|
|
439
|
+
const s = options.spring;
|
|
440
|
+
if (spring.stiffness !== s.stiffness || spring.damping !== s.damping || spring.mass !== s.mass) {
|
|
441
|
+
options.spring = spring;
|
|
442
|
+
for (const item of items.values()) updateSprings(item);
|
|
443
|
+
changed = true;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if ("onActiveChange" in opts) options.onActiveChange = opts.onActiveChange;
|
|
447
|
+
if (!changed) return;
|
|
448
|
+
targetsDirty = true;
|
|
449
|
+
if (options.reducedMotion) {
|
|
450
|
+
if (!wasReduced || active) snapAll();
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (anyChannel() || active) kick();
|
|
454
|
+
},
|
|
455
|
+
invalidate,
|
|
456
|
+
get active() {
|
|
457
|
+
return active;
|
|
458
|
+
},
|
|
459
|
+
destroy() {
|
|
460
|
+
if (destroyed) return;
|
|
461
|
+
cancel();
|
|
462
|
+
observer?.disconnect();
|
|
463
|
+
for (const item of items.values()) resetItem(item);
|
|
464
|
+
items.clear();
|
|
465
|
+
for (const name of CHANNELS) channels[name] = null;
|
|
466
|
+
active = false;
|
|
467
|
+
destroyed = true;
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
function createMagneticFieldHub() {
|
|
472
|
+
const entries = /* @__PURE__ */ new Map();
|
|
473
|
+
const sources = {
|
|
474
|
+
puck: null,
|
|
475
|
+
manual: null,
|
|
476
|
+
pointer: null
|
|
477
|
+
};
|
|
478
|
+
let core = null;
|
|
479
|
+
function detach() {
|
|
480
|
+
for (const entry of entries.values()) {
|
|
481
|
+
entry.unregister?.();
|
|
482
|
+
entry.unregister = null;
|
|
483
|
+
}
|
|
484
|
+
core = null;
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
register(outer, inner, opts) {
|
|
488
|
+
entries.get(outer)?.unregister?.();
|
|
489
|
+
const entry = { inner, opts: { ...opts }, unregister: null };
|
|
490
|
+
entries.set(outer, entry);
|
|
491
|
+
if (core) entry.unregister = core.register(outer, inner, entry.opts);
|
|
492
|
+
return () => {
|
|
493
|
+
if (entries.get(outer) !== entry) return;
|
|
494
|
+
entries.delete(outer);
|
|
495
|
+
entry.unregister?.();
|
|
496
|
+
entry.unregister = null;
|
|
497
|
+
};
|
|
498
|
+
},
|
|
499
|
+
updateItem(outer, opts) {
|
|
500
|
+
const entry = entries.get(outer);
|
|
501
|
+
if (!entry) return;
|
|
502
|
+
entry.opts = { ...entry.opts, ...opts };
|
|
503
|
+
core?.updateItem(outer, opts);
|
|
504
|
+
},
|
|
505
|
+
setSource(point, channel) {
|
|
506
|
+
sources[channel] = point ? { x: point.x, y: point.y } : null;
|
|
507
|
+
core?.setSource(sources[channel], channel);
|
|
508
|
+
},
|
|
509
|
+
attach(next) {
|
|
510
|
+
if (core) detach();
|
|
511
|
+
core = next;
|
|
512
|
+
for (const [outer, entry] of entries) {
|
|
513
|
+
entry.unregister = next.register(outer, entry.inner, entry.opts);
|
|
514
|
+
}
|
|
515
|
+
for (const name of CHANNELS) {
|
|
516
|
+
const p = sources[name];
|
|
517
|
+
if (p) next.setSource(p, name);
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
detach
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
export {
|
|
524
|
+
MAGNETIC_FIELD_DEFAULTS,
|
|
525
|
+
MAGNETIC_FIELD_SPRINGS,
|
|
526
|
+
createMagneticFieldCore,
|
|
527
|
+
createMagneticFieldHub,
|
|
528
|
+
falloff,
|
|
529
|
+
itemSpring,
|
|
530
|
+
itemTarget,
|
|
531
|
+
rectDistance,
|
|
532
|
+
resolveSpring,
|
|
533
|
+
stepSpring
|
|
534
|
+
};
|