nexa-animation 0.7.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/dist/animatedSignal.d.ts +22 -0
- package/dist/animatedSignal.js +101 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/dist/interpolate.d.ts +1 -0
- package/dist/interpolate.js +7 -0
- package/dist/stagger.d.ts +5 -0
- package/dist/stagger.js +12 -0
- package/package.json +28 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type EasingFn = (t: number) => number;
|
|
2
|
+
export interface TimingConfig {
|
|
3
|
+
to: number;
|
|
4
|
+
duration: number;
|
|
5
|
+
easing?: string | EasingFn;
|
|
6
|
+
onComplete?: () => void;
|
|
7
|
+
}
|
|
8
|
+
export interface SpringConfig {
|
|
9
|
+
to: number;
|
|
10
|
+
stiffness?: number;
|
|
11
|
+
damping?: number;
|
|
12
|
+
mass?: number;
|
|
13
|
+
onComplete?: () => void;
|
|
14
|
+
}
|
|
15
|
+
export interface AnimatedSignal {
|
|
16
|
+
value: number;
|
|
17
|
+
timing(config: TimingConfig): void;
|
|
18
|
+
spring(config: SpringConfig): void;
|
|
19
|
+
stop(): void;
|
|
20
|
+
}
|
|
21
|
+
export declare function animatedSignal(initial: number): AnimatedSignal;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { signal } from 'nexa-reactivity';
|
|
2
|
+
const easings = {
|
|
3
|
+
linear: (t) => t,
|
|
4
|
+
'ease-in': (t) => t * t,
|
|
5
|
+
'ease-out': (t) => t * (2 - t),
|
|
6
|
+
'ease-in-out': (t) => (t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t),
|
|
7
|
+
};
|
|
8
|
+
export function animatedSignal(initial) {
|
|
9
|
+
const _signal = signal(initial);
|
|
10
|
+
let _rafId = null;
|
|
11
|
+
let _timerId = null;
|
|
12
|
+
let _stopped = false;
|
|
13
|
+
const cancelCurrent = () => {
|
|
14
|
+
if (_rafId !== null) {
|
|
15
|
+
cancelAnimationFrame(_rafId);
|
|
16
|
+
_rafId = null;
|
|
17
|
+
}
|
|
18
|
+
if (_timerId !== null) {
|
|
19
|
+
clearTimeout(_timerId);
|
|
20
|
+
_timerId = null;
|
|
21
|
+
}
|
|
22
|
+
_stopped = true;
|
|
23
|
+
};
|
|
24
|
+
const now = () => typeof performance !== 'undefined' ? performance.now() : Date.now();
|
|
25
|
+
const scheduleFrame = (cb) => {
|
|
26
|
+
if (typeof requestAnimationFrame !== 'undefined') {
|
|
27
|
+
_rafId = requestAnimationFrame((time) => {
|
|
28
|
+
_rafId = null;
|
|
29
|
+
cb(time);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
_timerId = setTimeout(() => {
|
|
34
|
+
_timerId = null;
|
|
35
|
+
cb(now());
|
|
36
|
+
}, 16);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const obj = {
|
|
40
|
+
get value() {
|
|
41
|
+
return _signal.value;
|
|
42
|
+
},
|
|
43
|
+
set value(v) {
|
|
44
|
+
cancelCurrent();
|
|
45
|
+
_signal.value = v;
|
|
46
|
+
},
|
|
47
|
+
timing({ to, duration, easing = 'ease-out', onComplete }) {
|
|
48
|
+
cancelCurrent();
|
|
49
|
+
_stopped = false;
|
|
50
|
+
const from = _signal.peek();
|
|
51
|
+
const easeFn = typeof easing === 'function' ? easing : (easings[easing] ?? easings['ease-out']);
|
|
52
|
+
const startTime = now();
|
|
53
|
+
const tick = (time) => {
|
|
54
|
+
if (_stopped)
|
|
55
|
+
return;
|
|
56
|
+
const elapsed = time - startTime;
|
|
57
|
+
const t = Math.min(Math.max(elapsed / duration, 0), 1);
|
|
58
|
+
_signal.value = from + (to - from) * easeFn(t);
|
|
59
|
+
if (t < 1) {
|
|
60
|
+
scheduleFrame(tick);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
_signal.value = to;
|
|
64
|
+
onComplete?.();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
scheduleFrame(tick);
|
|
68
|
+
},
|
|
69
|
+
spring({ to, stiffness = 170, damping = 26, mass = 1, onComplete }) {
|
|
70
|
+
cancelCurrent();
|
|
71
|
+
_stopped = false;
|
|
72
|
+
let pos = _signal.peek();
|
|
73
|
+
let vel = 0;
|
|
74
|
+
let lastTime = now();
|
|
75
|
+
const tick = (time) => {
|
|
76
|
+
if (_stopped)
|
|
77
|
+
return;
|
|
78
|
+
// Prevent huge jumps if tab was inactive
|
|
79
|
+
const dt = Math.min((time - lastTime) / 1000, 0.064);
|
|
80
|
+
lastTime = time;
|
|
81
|
+
const force = -stiffness * (pos - to) - damping * vel;
|
|
82
|
+
vel += (force / mass) * dt;
|
|
83
|
+
pos += vel * dt;
|
|
84
|
+
_signal.value = pos;
|
|
85
|
+
const isSettled = Math.abs(pos - to) < 0.01 && Math.abs(vel) < 0.01;
|
|
86
|
+
if (isSettled) {
|
|
87
|
+
_signal.value = to;
|
|
88
|
+
onComplete?.();
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
scheduleFrame(tick);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
scheduleFrame(tick);
|
|
95
|
+
},
|
|
96
|
+
stop() {
|
|
97
|
+
cancelCurrent();
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
return obj;
|
|
101
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { animatedSignal } from './animatedSignal.js';
|
|
2
|
+
export { interpolate } from './interpolate.js';
|
|
3
|
+
export { stagger } from './stagger.js';
|
|
4
|
+
export type { TimingConfig, SpringConfig, AnimatedSignal } from './animatedSignal.js';
|
|
5
|
+
export type { StaggerOptions } from './stagger.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function interpolate(input: number, inputRange: [number, number], outputRange: [number, number]): number;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function interpolate(input, inputRange, outputRange) {
|
|
2
|
+
const [inMin, inMax] = inputRange;
|
|
3
|
+
const [outMin, outMax] = outputRange;
|
|
4
|
+
const clamped = Math.max(inMin, Math.min(inMax, input));
|
|
5
|
+
const t = (clamped - inMin) / (inMax - inMin);
|
|
6
|
+
return outMin + t * (outMax - outMin);
|
|
7
|
+
}
|
package/dist/stagger.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nexa-animation",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"nexa-reactivity": "0.7.0"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
}
|
|
28
|
+
}
|