solid-drift 0.2.0 → 0.7.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 +735 -10
- package/dist/animate.d.ts +2 -2
- package/dist/animate.js +2 -2
- package/dist/cartoon.d.ts +190 -0
- package/dist/cartoon.js +334 -0
- package/dist/color.d.ts +53 -0
- package/dist/color.js +391 -0
- package/dist/directive.d.ts +5 -5
- package/dist/directive.js +15 -7
- package/dist/easing.d.ts +22 -1
- package/dist/easing.js +49 -1
- package/dist/flip.d.ts +44 -0
- package/dist/flip.js +108 -0
- package/dist/horizontal.d.ts +107 -0
- package/dist/horizontal.js +208 -0
- package/dist/index.d.ts +16 -3
- package/dist/index.js +15 -3
- package/dist/inview.d.ts +4 -4
- package/dist/inview.js +5 -5
- package/dist/motion.d.ts +327 -0
- package/dist/motion.js +729 -0
- package/dist/physics.d.ts +146 -0
- package/dist/physics.js +352 -0
- package/dist/pointer.d.ts +76 -0
- package/dist/pointer.js +123 -0
- package/dist/reduced-motion.d.ts +3 -3
- package/dist/reduced-motion.js +5 -4
- package/dist/scroll.d.ts +3 -3
- package/dist/scroll.js +5 -5
- package/dist/scrollfx.d.ts +156 -0
- package/dist/scrollfx.js +148 -0
- package/dist/scrub.d.ts +51 -0
- package/dist/scrub.js +67 -0
- package/dist/spring.d.ts +39 -4
- package/dist/spring.js +26 -7
- package/dist/stagger.d.ts +4 -4
- package/dist/stagger.js +4 -4
- package/dist/timeline.d.ts +45 -0
- package/dist/timeline.js +93 -0
- package/dist/trail.d.ts +27 -0
- package/dist/trail.js +75 -0
- package/dist/tween.d.ts +3 -3
- package/dist/tween.js +3 -3
- package/dist/typography.d.ts +241 -0
- package/dist/typography.js +812 -0
- package/dist/velocity.d.ts +44 -0
- package/dist/velocity.js +88 -0
- package/package.json +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Accessor } from "solid-js";
|
|
2
|
+
export interface VelocityOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Exponential smoothing of the raw velocity, 0 to 1. Higher values
|
|
5
|
+
* react slower and ride smoother. Default 0.8.
|
|
6
|
+
*/
|
|
7
|
+
smoothing?: number;
|
|
8
|
+
/** Multiplier applied to the raw units per second. Default 1. */
|
|
9
|
+
scale?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Seconds of stillness after which the velocity settles to exactly 0.
|
|
12
|
+
* Default 0.12.
|
|
13
|
+
*/
|
|
14
|
+
settleAfter?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A signal tracking how fast another signal is changing, in scaled units
|
|
18
|
+
* per second, smoothed with an exponential moving average.
|
|
19
|
+
*
|
|
20
|
+
* The classic use is scroll velocity: it spikes while the user flings the
|
|
21
|
+
* page and eases back to 0 when scrolling stops, which makes it perfect
|
|
22
|
+
* for velocity-driven skew, stretch, or motion blur that intensifies with
|
|
23
|
+
* speed. With no source it measures page scroll in pixels per second.
|
|
24
|
+
*
|
|
25
|
+
* The measurement loop runs on the shared clock only while the value is
|
|
26
|
+
* live: it kicks off when the source changes and parks itself once the
|
|
27
|
+
* velocity settles back to 0.
|
|
28
|
+
*
|
|
29
|
+
* SSR-safe: returns a constant `0` accessor on the server. Under reduced
|
|
30
|
+
* motion it also returns constant `0`, since velocity-reactive effects
|
|
31
|
+
* are motion by another name.
|
|
32
|
+
*
|
|
33
|
+
* ```tsx
|
|
34
|
+
* // Skew a list while the user scrolls fast, relax when they stop.
|
|
35
|
+
* const velocity = createVelocity() // px per second of page scroll
|
|
36
|
+
* const skew = () => Math.max(-8, Math.min(8, velocity() / 120))
|
|
37
|
+
* <ul style={{ transform: `skewY(${skew()}deg)` }}>…</ul>
|
|
38
|
+
*
|
|
39
|
+
* // Velocity of any signal, in its own units per second.
|
|
40
|
+
* const [n, setN] = createSignal(0)
|
|
41
|
+
* const speed = createVelocity(n, { smoothing: 0.7 })
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function createVelocity(source?: Accessor<number>, options?: VelocityOptions): Accessor<number>;
|
package/dist/velocity.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createEffect, createSignal, onCleanup, untrack, } from "solid-js";
|
|
2
|
+
import { now, schedule } from "./engine.js";
|
|
3
|
+
import { prefersReducedMotion } from "./reduced-motion.js";
|
|
4
|
+
/**
|
|
5
|
+
* A signal tracking how fast another signal is changing, in scaled units
|
|
6
|
+
* per second, smoothed with an exponential moving average.
|
|
7
|
+
*
|
|
8
|
+
* The classic use is scroll velocity: it spikes while the user flings the
|
|
9
|
+
* page and eases back to 0 when scrolling stops, which makes it perfect
|
|
10
|
+
* for velocity-driven skew, stretch, or motion blur that intensifies with
|
|
11
|
+
* speed. With no source it measures page scroll in pixels per second.
|
|
12
|
+
*
|
|
13
|
+
* The measurement loop runs on the shared clock only while the value is
|
|
14
|
+
* live: it kicks off when the source changes and parks itself once the
|
|
15
|
+
* velocity settles back to 0.
|
|
16
|
+
*
|
|
17
|
+
* SSR-safe: returns a constant `0` accessor on the server. Under reduced
|
|
18
|
+
* motion it also returns constant `0`, since velocity-reactive effects
|
|
19
|
+
* are motion by another name.
|
|
20
|
+
*
|
|
21
|
+
* ```tsx
|
|
22
|
+
* // Skew a list while the user scrolls fast, relax when they stop.
|
|
23
|
+
* const velocity = createVelocity() // px per second of page scroll
|
|
24
|
+
* const skew = () => Math.max(-8, Math.min(8, velocity() / 120))
|
|
25
|
+
* <ul style={{ transform: `skewY(${skew()}deg)` }}>…</ul>
|
|
26
|
+
*
|
|
27
|
+
* // Velocity of any signal, in its own units per second.
|
|
28
|
+
* const [n, setN] = createSignal(0)
|
|
29
|
+
* const speed = createVelocity(n, { smoothing: 0.7 })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function createVelocity(source, options = {}) {
|
|
33
|
+
if (typeof window === "undefined" || prefersReducedMotion()) {
|
|
34
|
+
return () => 0;
|
|
35
|
+
}
|
|
36
|
+
const { smoothing = 0.8, scale = 1, settleAfter = 0.12 } = options;
|
|
37
|
+
const read = source ?? (() => window.scrollY);
|
|
38
|
+
const [velocity, setVelocity] = createSignal(0);
|
|
39
|
+
let current = 0;
|
|
40
|
+
let lastValue = untrack(read);
|
|
41
|
+
let lastT = now();
|
|
42
|
+
let lastChangeT = lastT;
|
|
43
|
+
let cancel = null;
|
|
44
|
+
const loop = (t) => {
|
|
45
|
+
const value = untrack(read);
|
|
46
|
+
const dt = Math.max((t - lastT) / 1000, 0.0001);
|
|
47
|
+
if (value !== lastValue) {
|
|
48
|
+
const instant = ((value - lastValue) / dt) * scale;
|
|
49
|
+
current = current * smoothing + instant * (1 - smoothing);
|
|
50
|
+
lastValue = value;
|
|
51
|
+
lastChangeT = t;
|
|
52
|
+
}
|
|
53
|
+
else if (t - lastChangeT > settleAfter * 1000) {
|
|
54
|
+
current = 0;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// Coast down smoothly between samples instead of holding stale speed.
|
|
58
|
+
current *= Math.pow(0.02, dt);
|
|
59
|
+
}
|
|
60
|
+
lastT = t;
|
|
61
|
+
setVelocity(current);
|
|
62
|
+
const settled = Math.abs(current) < 0.001 && t - lastChangeT > settleAfter * 1000;
|
|
63
|
+
if (settled) {
|
|
64
|
+
setVelocity(0);
|
|
65
|
+
cancel = null;
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
};
|
|
70
|
+
const kick = () => {
|
|
71
|
+
if (cancel)
|
|
72
|
+
return;
|
|
73
|
+
lastT = now();
|
|
74
|
+
cancel = schedule(loop);
|
|
75
|
+
};
|
|
76
|
+
createEffect(() => {
|
|
77
|
+
read(); // track the source so any change kicks the loop
|
|
78
|
+
kick();
|
|
79
|
+
});
|
|
80
|
+
if (!source) {
|
|
81
|
+
// The default page-scroll source is not a reactive signal, so scroll
|
|
82
|
+
// events kick the loop explicitly.
|
|
83
|
+
window.addEventListener("scroll", kick, { passive: true });
|
|
84
|
+
onCleanup(() => window.removeEventListener("scroll", kick));
|
|
85
|
+
}
|
|
86
|
+
onCleanup(() => cancel?.());
|
|
87
|
+
return velocity;
|
|
88
|
+
}
|
package/package.json
CHANGED