react-ui-animate 3.0.0-rc.0 → 3.0.0-rc.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/dist/gestures/helpers/math.d.ts +1 -1
- package/dist/index.js +1 -2897
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/.vscode/settings.json +0 -3
- package/dist/animation/animationType.d.ts +0 -15
- package/dist/animation/core/animation/Animation.d.ts +0 -16
- package/dist/animation/core/animation/FluidValue.d.ts +0 -21
- package/dist/animation/core/animation/RequestAnimationFrame.d.ts +0 -8
- package/dist/animation/core/animation/SpringAnimation.d.ts +0 -41
- package/dist/animation/core/animation/TimingAnimation.d.ts +0 -35
- package/dist/animation/core/animation/TransitionValue.d.ts +0 -21
- package/dist/animation/core/helpers/camelToDash.d.ts +0 -7
- package/dist/animation/core/helpers/canInterpolate.d.ts +0 -11
- package/dist/animation/core/helpers/getAnimatableObject.d.ts +0 -22
- package/dist/animation/core/helpers/getNonAnimatableStyle.d.ts +0 -6
- package/dist/animation/core/react/Tags.d.ts +0 -3
- package/dist/animation/core/react/TransformStyles.d.ts +0 -8
- package/dist/animation/core/react/animated.d.ts +0 -29
- package/dist/animation/core/react/combineRefs.d.ts +0 -2
- package/dist/animation/core/react/functions/camelToDash.d.ts +0 -7
- package/dist/animation/core/react/functions/getAnimatableObject.d.ts +0 -22
- package/dist/animation/core/react/functions/getCleanProps.d.ts +0 -4
- package/dist/animation/core/react/functions/getCssValue.d.ts +0 -8
- package/dist/animation/core/react/functions/getNonAnimatableStyle.d.ts +0 -6
- package/dist/animation/core/react/functions/index.d.ts +0 -7
- package/dist/animation/core/react/functions/isDefined.d.ts +0 -6
- package/dist/animation/core/react/functions/isTransitionValue.d.ts +0 -6
- package/dist/animation/core/react/helpers/camelToDash.d.ts +0 -7
- package/dist/animation/core/react/helpers/canInterpolate.d.ts +0 -7
- package/dist/animation/core/react/helpers/getAnimatableObject.d.ts +0 -22
- package/dist/animation/core/react/helpers/getCleanProps.d.ts +0 -4
- package/dist/animation/core/react/helpers/getCssValue.d.ts +0 -8
- package/dist/animation/core/react/helpers/getNonAnimatableStyle.d.ts +0 -6
- package/dist/animation/core/react/helpers/index.d.ts +0 -7
- package/dist/animation/core/react/helpers/isDefined.d.ts +0 -6
- package/dist/animation/core/react/helpers/isFluidValue.d.ts +0 -6
- package/dist/animation/core/react/helpers/isTransitionValue.d.ts +0 -6
- package/dist/animation/core/react/useMounts.d.ts +0 -26
- package/dist/animation/core/react/useTransition.d.ts +0 -9
- package/dist/animation/core/react/useTransitions.d.ts +0 -11
- package/dist/animation/core/types/animation.d.ts +0 -54
- package/dist/animation/core/types/common.d.ts +0 -4
- package/dist/animation/core/types/fluid.d.ts +0 -19
- package/dist/animation/core/types/index.d.ts +0 -2
- package/dist/animation/getInitialConfig.d.ts +0 -3
- package/dist/animation/index.d.ts +0 -6
- package/dist/animation/interpolation.d.ts +0 -21
- package/dist/animation/modules/AnimatedBlock.d.ts +0 -8
- package/dist/animation/modules/AnimatedImage.d.ts +0 -8
- package/dist/animation/modules/AnimatedInline.d.ts +0 -8
- package/dist/animation/modules/MountedBlock.d.ts +0 -29
- package/dist/animation/modules/ScrollableBlock.d.ts +0 -21
- package/dist/animation/modules/TransitionBlock.d.ts +0 -17
- package/dist/animation/modules/index.d.ts +0 -6
- package/dist/animation/useAnimatedValue.d.ts +0 -22
- package/dist/animation/useMountedValue.d.ts +0 -15
- package/dist/gestures/eventAttacher.d.ts +0 -11
- package/dist/gestures/index.d.ts +0 -2
- package/dist/gestures/math.d.ts +0 -34
- package/dist/gestures/types.d.ts +0 -51
- package/dist/gestures/withDefault.d.ts +0 -4
- package/dist/utils/isDefined.d.ts +0 -1
- package/ecosystem.config.js +0 -12
- package/example/README.md +0 -46
- package/example/package-lock.json +0 -19597
- package/example/package.json +0 -45
- package/example/public/favicon.ico +0 -0
- package/example/public/index.html +0 -20
- package/example/public/logo192.png +0 -0
- package/example/public/logo512.png +0 -0
- package/example/public/manifest.json +0 -25
- package/example/public/robots.txt +0 -3
- package/example/src/App.tsx +0 -41
- package/example/src/components/Draggable.tsx +0 -46
- package/example/src/components/Gestures.tsx +0 -151
- package/example/src/components/Interpolation.tsx +0 -21
- package/example/src/components/Loop.tsx +0 -48
- package/example/src/components/MountedBlock.tsx +0 -25
- package/example/src/components/MouseMove.tsx +0 -59
- package/example/src/components/MultistageTransition.tsx +0 -34
- package/example/src/components/Scroll.tsx +0 -39
- package/example/src/components/ScrollableBlock.tsx +0 -27
- package/example/src/components/SnapTo.tsx +0 -55
- package/example/src/components/TransitionBlock.tsx +0 -37
- package/example/src/components/Wheel.tsx +0 -39
- package/example/src/components/index.ts +0 -18
- package/example/src/components/svgLine.tsx +0 -48
- package/example/src/components/useAnimatedValue.tsx +0 -57
- package/example/src/components/useMountedValue.tsx +0 -62
- package/example/src/index.css +0 -8
- package/example/src/index.tsx +0 -16
- package/example/tsconfig.json +0 -26
- package/rollup.config.mjs +0 -18
- package/src/animation/core/controllers/Animation.ts +0 -27
- package/src/animation/core/controllers/FluidValue.ts +0 -97
- package/src/animation/core/controllers/RequestAnimationFrame.ts +0 -13
- package/src/animation/core/controllers/SpringAnimation.ts +0 -218
- package/src/animation/core/controllers/TimingAnimation.ts +0 -152
- package/src/animation/core/easing/Bezier.ts +0 -146
- package/src/animation/core/easing/Easing.ts +0 -132
- package/src/animation/core/helpers/camelCaseToKebabCase.ts +0 -10
- package/src/animation/core/helpers/getCleanProps.ts +0 -16
- package/src/animation/core/helpers/getCssValue.ts +0 -60
- package/src/animation/core/helpers/index.ts +0 -5
- package/src/animation/core/helpers/isDefined.ts +0 -14
- package/src/animation/core/helpers/isFluidValue.ts +0 -11
- package/src/animation/core/index.ts +0 -16
- package/src/animation/core/interpolation/Colors.ts +0 -232
- package/src/animation/core/interpolation/Interpolation.ts +0 -395
- package/src/animation/core/interpolation/__tests__/Colors.test.tsx +0 -35
- package/src/animation/core/react/fluid.ts +0 -197
- package/src/animation/core/react/makeFluid.ts +0 -294
- package/src/animation/core/react/transforms.ts +0 -90
- package/src/animation/core/react/useFluidValue.ts +0 -43
- package/src/animation/core/react/useMount.ts +0 -58
- package/src/animation/core/types/animation.d.ts +0 -56
- package/src/animation/core/types/common.d.ts +0 -4
- package/src/animation/core/types/fluid.d.ts +0 -38
- package/src/animation/lib/animationType.ts +0 -17
- package/src/animation/lib/getInitialConfig.ts +0 -61
- package/src/animation/lib/index.ts +0 -12
- package/src/animation/lib/interpolation.ts +0 -47
- package/src/animation/lib/modules/AnimatedBlock.ts +0 -8
- package/src/animation/lib/modules/AnimatedImage.ts +0 -8
- package/src/animation/lib/modules/AnimatedInline.ts +0 -8
- package/src/animation/lib/modules/MountedBlock.tsx +0 -51
- package/src/animation/lib/modules/ScrollableBlock.tsx +0 -68
- package/src/animation/lib/modules/TransitionBlock.tsx +0 -28
- package/src/animation/lib/modules/index.ts +0 -6
- package/src/animation/lib/useAnimatedValue.ts +0 -60
- package/src/animation/lib/useMountedValue.ts +0 -18
- package/src/gestures/controllers/DragGesture.ts +0 -178
- package/src/gestures/controllers/Gesture.ts +0 -54
- package/src/gestures/controllers/MouseMoveGesture.ts +0 -111
- package/src/gestures/controllers/ScrollGesture.ts +0 -107
- package/src/gestures/controllers/WheelGesture.ts +0 -123
- package/src/gestures/controllers/index.ts +0 -4
- package/src/gestures/helpers/eventAttacher.ts +0 -67
- package/src/gestures/helpers/index.ts +0 -1
- package/src/gestures/helpers/math.ts +0 -120
- package/src/gestures/helpers/withDefault.ts +0 -3
- package/src/gestures/hooks/index.ts +0 -5
- package/src/gestures/hooks/useDrag.ts +0 -14
- package/src/gestures/hooks/useGesture.ts +0 -38
- package/src/gestures/hooks/useMouseMove.ts +0 -11
- package/src/gestures/hooks/useRecognizer.ts +0 -59
- package/src/gestures/hooks/useScroll.ts +0 -11
- package/src/gestures/hooks/useWheel.ts +0 -11
- package/src/gestures/types/index.d.ts +0 -49
- package/src/hooks/index.ts +0 -3
- package/src/hooks/useMeasure.ts +0 -132
- package/src/hooks/useOutsideClick.ts +0 -36
- package/src/hooks/useWindowDimension.ts +0 -58
- package/src/index.ts +0 -42
- package/src/utils/delay.ts +0 -9
- package/src/utils/index.ts +0 -1
- package/tsconfig.json +0 -24
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { useWheel } from 'react-ui-animate';
|
|
2
|
-
|
|
3
|
-
export const Wheel = () => {
|
|
4
|
-
const bind = useWheel(function (event) {
|
|
5
|
-
console.log('WHEEL', event);
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
return (
|
|
9
|
-
<>
|
|
10
|
-
<div
|
|
11
|
-
{...bind()}
|
|
12
|
-
style={{
|
|
13
|
-
width: 500,
|
|
14
|
-
height: 500,
|
|
15
|
-
overflowY: 'auto',
|
|
16
|
-
backgroundColor: '#3399ff',
|
|
17
|
-
}}
|
|
18
|
-
>
|
|
19
|
-
<div style={{ height: 2000 }} />
|
|
20
|
-
</div>
|
|
21
|
-
|
|
22
|
-
<div style={{ height: 2000 }} />
|
|
23
|
-
|
|
24
|
-
{Array(5)
|
|
25
|
-
.fill(null)
|
|
26
|
-
.map((_, i) => (
|
|
27
|
-
<div
|
|
28
|
-
key={i}
|
|
29
|
-
style={{
|
|
30
|
-
width: 100,
|
|
31
|
-
height: 100,
|
|
32
|
-
backgroundColor: '#3399ff',
|
|
33
|
-
marginBottom: 10,
|
|
34
|
-
}}
|
|
35
|
-
/>
|
|
36
|
-
))}
|
|
37
|
-
</>
|
|
38
|
-
);
|
|
39
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export * from './Draggable';
|
|
2
|
-
export * from './Gestures';
|
|
3
|
-
export * from './MountedBlock';
|
|
4
|
-
export * from './ScrollableBlock';
|
|
5
|
-
export * from './svgLine';
|
|
6
|
-
export * from './useAnimatedValue';
|
|
7
|
-
export * from './useMountedValue';
|
|
8
|
-
export * from './MouseMove';
|
|
9
|
-
export * from './Scroll';
|
|
10
|
-
export * from './Wheel';
|
|
11
|
-
export * from './Gestures';
|
|
12
|
-
export * from './Interpolation';
|
|
13
|
-
export * from './MultistageTransition';
|
|
14
|
-
export * from './Loop';
|
|
15
|
-
export * from './SnapTo';
|
|
16
|
-
export * from './TransitionBlock';
|
|
17
|
-
export * from './useAnimatedValue';
|
|
18
|
-
export * from './useMountedValue';
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useAnimatedValue,
|
|
3
|
-
useDrag,
|
|
4
|
-
AnimationConfigUtils,
|
|
5
|
-
fluid,
|
|
6
|
-
} from 'react-ui-animate';
|
|
7
|
-
|
|
8
|
-
export function SVGLine() {
|
|
9
|
-
const dragX = useAnimatedValue(0, { immediate: true });
|
|
10
|
-
const followX = useAnimatedValue(0, { ...AnimationConfigUtils.ELASTIC });
|
|
11
|
-
|
|
12
|
-
const circleBind = useDrag(({ movementX }) => {
|
|
13
|
-
dragX.value = movementX;
|
|
14
|
-
followX.value = movementX;
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
return (
|
|
18
|
-
<div className='App'>
|
|
19
|
-
<svg
|
|
20
|
-
style={{
|
|
21
|
-
border: '1px solid #3399ff',
|
|
22
|
-
}}
|
|
23
|
-
width={200}
|
|
24
|
-
height={200}
|
|
25
|
-
xmlns='http://www.w3.org/2000/svg'
|
|
26
|
-
>
|
|
27
|
-
<fluid.line
|
|
28
|
-
x1={followX.value}
|
|
29
|
-
y1='10'
|
|
30
|
-
x2={dragX.value}
|
|
31
|
-
y2='50'
|
|
32
|
-
stroke='black'
|
|
33
|
-
/>
|
|
34
|
-
<fluid.circle cx={followX.value} cy='10' r='2' fill='red' />
|
|
35
|
-
<fluid.circle
|
|
36
|
-
{...circleBind()}
|
|
37
|
-
style={{
|
|
38
|
-
cursor: 'pointer',
|
|
39
|
-
}}
|
|
40
|
-
cx={dragX.value}
|
|
41
|
-
cy='50'
|
|
42
|
-
r='5'
|
|
43
|
-
fill='red'
|
|
44
|
-
/>
|
|
45
|
-
</svg>
|
|
46
|
-
</div>
|
|
47
|
-
);
|
|
48
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { useAnimatedValue, AnimatedBlock } from 'react-ui-animate';
|
|
3
|
-
|
|
4
|
-
export const UseAnimatedValue: React.FC = () => {
|
|
5
|
-
// const width = useAnimatedValue(100);
|
|
6
|
-
const bg = useAnimatedValue('position');
|
|
7
|
-
|
|
8
|
-
return (
|
|
9
|
-
<>
|
|
10
|
-
<AnimatedBlock
|
|
11
|
-
a={bg.value}
|
|
12
|
-
style={{
|
|
13
|
-
width: 100,
|
|
14
|
-
height: 100,
|
|
15
|
-
backgroundColor: 'red',
|
|
16
|
-
position: bg.value,
|
|
17
|
-
left: 0,
|
|
18
|
-
top: 0,
|
|
19
|
-
}}
|
|
20
|
-
/>
|
|
21
|
-
<button onClick={() => (bg.value = 'absolute')}>Animate</button>
|
|
22
|
-
{/* <button onClick={() => (bg.value = '#00ff00')}>Green</button> */}
|
|
23
|
-
|
|
24
|
-
{/* <div style={{ perspective: 200 }}>
|
|
25
|
-
<AnimatedBlock
|
|
26
|
-
style={{
|
|
27
|
-
perspective: 400,
|
|
28
|
-
width: width.value,
|
|
29
|
-
height: 100,
|
|
30
|
-
backgroundColor: interpolate(
|
|
31
|
-
width.value,
|
|
32
|
-
[100, 500],
|
|
33
|
-
['#3399ff', 'red']
|
|
34
|
-
),
|
|
35
|
-
translateX: interpolate(width.value, [100, 500], [0, 200]),
|
|
36
|
-
rotateX: interpolate(width.value, [100, 500], [0, 45]),
|
|
37
|
-
}}
|
|
38
|
-
/>
|
|
39
|
-
</div>
|
|
40
|
-
|
|
41
|
-
<button
|
|
42
|
-
onClick={() => {
|
|
43
|
-
width.value = 100;
|
|
44
|
-
}}
|
|
45
|
-
>
|
|
46
|
-
LEFT
|
|
47
|
-
</button>
|
|
48
|
-
<button
|
|
49
|
-
onClick={() => {
|
|
50
|
-
width.value = 500;
|
|
51
|
-
}}
|
|
52
|
-
>
|
|
53
|
-
RIGHT
|
|
54
|
-
</button> */}
|
|
55
|
-
</>
|
|
56
|
-
);
|
|
57
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
useMountedValue,
|
|
4
|
-
AnimatedBlock,
|
|
5
|
-
bInterpolate,
|
|
6
|
-
useAnimatedValue,
|
|
7
|
-
} from 'react-ui-animate';
|
|
8
|
-
|
|
9
|
-
export const UseMountedValue: React.FC = () => {
|
|
10
|
-
const x = useAnimatedValue(0);
|
|
11
|
-
const [open, setOpen] = React.useState(true);
|
|
12
|
-
const mountedValue = useMountedValue(open, {
|
|
13
|
-
from: 0,
|
|
14
|
-
enter: 1,
|
|
15
|
-
exit: 0,
|
|
16
|
-
config: {
|
|
17
|
-
duration: 200,
|
|
18
|
-
},
|
|
19
|
-
enterConfig: { duration: 2000 },
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return (
|
|
23
|
-
<>
|
|
24
|
-
<AnimatedBlock
|
|
25
|
-
radius={x.value}
|
|
26
|
-
style={{
|
|
27
|
-
width: 100,
|
|
28
|
-
height: 100,
|
|
29
|
-
backgroundColor: '#3399ff',
|
|
30
|
-
translateX: 100,
|
|
31
|
-
rotate: x.value,
|
|
32
|
-
}}
|
|
33
|
-
onClick={() => (x.value = 45)}
|
|
34
|
-
/>
|
|
35
|
-
{mountedValue(
|
|
36
|
-
(animation, mounted) =>
|
|
37
|
-
mounted && (
|
|
38
|
-
<AnimatedBlock
|
|
39
|
-
style={{
|
|
40
|
-
width: bInterpolate(animation.value, 100, 500),
|
|
41
|
-
height: 100,
|
|
42
|
-
backgroundColor: bInterpolate(
|
|
43
|
-
animation.value,
|
|
44
|
-
'red',
|
|
45
|
-
'#3399ff'
|
|
46
|
-
),
|
|
47
|
-
translateX: 45,
|
|
48
|
-
}}
|
|
49
|
-
/>
|
|
50
|
-
)
|
|
51
|
-
)}
|
|
52
|
-
|
|
53
|
-
<button
|
|
54
|
-
onClick={() => {
|
|
55
|
-
setOpen((prev) => !prev);
|
|
56
|
-
}}
|
|
57
|
-
>
|
|
58
|
-
ANIMATE ME
|
|
59
|
-
</button>
|
|
60
|
-
</>
|
|
61
|
-
);
|
|
62
|
-
};
|
package/example/src/index.css
DELETED
package/example/src/index.tsx
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import ReactDOM from 'react-dom/client';
|
|
3
|
-
|
|
4
|
-
import './index.css';
|
|
5
|
-
|
|
6
|
-
import App from './App';
|
|
7
|
-
|
|
8
|
-
const root = ReactDOM.createRoot(
|
|
9
|
-
document.getElementById('root') as HTMLElement
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
root.render(
|
|
13
|
-
<React.StrictMode>
|
|
14
|
-
<App />
|
|
15
|
-
</React.StrictMode>
|
|
16
|
-
);
|
package/example/tsconfig.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"strict": true,
|
|
4
|
-
"esModuleInterop": true,
|
|
5
|
-
"lib": [
|
|
6
|
-
"dom",
|
|
7
|
-
"es2015"
|
|
8
|
-
],
|
|
9
|
-
"downlevelIteration": true,
|
|
10
|
-
"jsx": "react-jsx",
|
|
11
|
-
"target": "es5",
|
|
12
|
-
"allowJs": true,
|
|
13
|
-
"skipLibCheck": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
"module": "esnext",
|
|
17
|
-
"moduleResolution": "node",
|
|
18
|
-
"resolveJsonModule": true,
|
|
19
|
-
"isolatedModules": true,
|
|
20
|
-
"allowSyntheticDefaultImports": true,
|
|
21
|
-
"noFallthroughCasesInSwitch": true
|
|
22
|
-
},
|
|
23
|
-
"include": [
|
|
24
|
-
"src"
|
|
25
|
-
]
|
|
26
|
-
}
|
package/rollup.config.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import typescript from "rollup-plugin-typescript2";
|
|
2
|
-
|
|
3
|
-
import pkg from "./package.json" assert { type: "json" };
|
|
4
|
-
|
|
5
|
-
export default {
|
|
6
|
-
input: "src/index.ts",
|
|
7
|
-
output: [
|
|
8
|
-
{
|
|
9
|
-
file: pkg.main,
|
|
10
|
-
format: "cjs",
|
|
11
|
-
exports: "named",
|
|
12
|
-
sourcemap: true,
|
|
13
|
-
strict: false,
|
|
14
|
-
},
|
|
15
|
-
],
|
|
16
|
-
plugins: [typescript()],
|
|
17
|
-
external: ["react", "react-dom"],
|
|
18
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ResultType } from '../types/animation';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Base Animation class
|
|
5
|
-
*/
|
|
6
|
-
export class Animation {
|
|
7
|
-
_active: boolean;
|
|
8
|
-
_onEnd: any;
|
|
9
|
-
/**
|
|
10
|
-
* it is necessary to add _onRest function as well
|
|
11
|
-
* because _onEnd is always re-assigned with onUpdate callback
|
|
12
|
-
* so that _onRest function is not fired, so we have to duplicate it
|
|
13
|
-
*/
|
|
14
|
-
_onRest?: any;
|
|
15
|
-
|
|
16
|
-
_debounceOnEnd(result: ResultType) {
|
|
17
|
-
const onEnd = this._onEnd;
|
|
18
|
-
const onRest = this._onRest;
|
|
19
|
-
this._onEnd = null;
|
|
20
|
-
this._onRest = null;
|
|
21
|
-
|
|
22
|
-
onRest && onRest(result);
|
|
23
|
-
onEnd && onEnd(result);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
stop() {}
|
|
27
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Length,
|
|
3
|
-
ResultType,
|
|
4
|
-
SubscribeFn,
|
|
5
|
-
FluidValueConfig,
|
|
6
|
-
OnUpdateFn,
|
|
7
|
-
AssignValue,
|
|
8
|
-
} from '../types/animation';
|
|
9
|
-
import type { Fn } from '../types/common';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Represents a fluid value that can animate between states.
|
|
13
|
-
*/
|
|
14
|
-
export class FluidValue {
|
|
15
|
-
public _subscribe: SubscribeFn;
|
|
16
|
-
public _value: Length;
|
|
17
|
-
public _config?: FluidValueConfig;
|
|
18
|
-
public _currentValue: { current: Length };
|
|
19
|
-
public _subscriptions: Map<{ uuid: number; property: string }, OnUpdateFn>;
|
|
20
|
-
|
|
21
|
-
public get: () => Length;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Creates a new FluidValue instance.
|
|
25
|
-
* @param initialValue - The initial value of the fluid value.
|
|
26
|
-
* @param config - Optional configuration for the fluid value.
|
|
27
|
-
*/
|
|
28
|
-
constructor(initialValue: Length, config?: FluidValueConfig) {
|
|
29
|
-
this._subscriptions = new Map();
|
|
30
|
-
this._subscribe = (
|
|
31
|
-
onUpdate: OnUpdateFn,
|
|
32
|
-
property: string,
|
|
33
|
-
uuid: number
|
|
34
|
-
) => {
|
|
35
|
-
for (const key of this._subscriptions.keys()) {
|
|
36
|
-
if (key.property === property) {
|
|
37
|
-
this._subscriptions.set(key, onUpdate);
|
|
38
|
-
return () => this._subscriptions.delete(key);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
this._subscriptions.set({ uuid, property }, onUpdate);
|
|
43
|
-
return () => this._subscriptions.delete({ uuid, property });
|
|
44
|
-
};
|
|
45
|
-
this._value = initialValue;
|
|
46
|
-
this._currentValue = { current: initialValue };
|
|
47
|
-
this._config = config;
|
|
48
|
-
this.get = () => this._currentValue.current;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Animates from the current value to the updated value.
|
|
53
|
-
* Determines whether to perform a multi-stage or single-stage transition.
|
|
54
|
-
* @param updatedValue - The value to animate to, or a function that defines a multi-stage transition.
|
|
55
|
-
* @param config - Optional configuration for the animation.
|
|
56
|
-
* @param callback - Optional callback to be called after the animation ends.
|
|
57
|
-
*/
|
|
58
|
-
setValue(
|
|
59
|
-
updatedValue: AssignValue,
|
|
60
|
-
config?: FluidValueConfig,
|
|
61
|
-
callback?: Fn<ResultType, void>
|
|
62
|
-
) {
|
|
63
|
-
/** Multistage transition */
|
|
64
|
-
if (typeof updatedValue === 'function') {
|
|
65
|
-
updatedValue((nextValue, nextConfig) => {
|
|
66
|
-
const multiStagePromise = new Promise((resolve) => {
|
|
67
|
-
for (const subscriptionKey of this._subscriptions.keys()) {
|
|
68
|
-
const updater = this._subscriptions.get(subscriptionKey);
|
|
69
|
-
|
|
70
|
-
if (updater) {
|
|
71
|
-
updater(nextValue, nextConfig ?? config, function (result) {
|
|
72
|
-
if (result.finished) {
|
|
73
|
-
resolve(nextValue);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (callback) {
|
|
77
|
-
callback(result);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
return multiStagePromise;
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Singlestage transition */
|
|
91
|
-
for (const subscriptionKey of this._subscriptions.keys()) {
|
|
92
|
-
const updater = this._subscriptions.get(subscriptionKey);
|
|
93
|
-
|
|
94
|
-
updater && updater(updatedValue, config, callback);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export const RequestAnimationFrame = {
|
|
2
|
-
current: (cb: any) => window.requestAnimationFrame(cb),
|
|
3
|
-
inject(injected: any) {
|
|
4
|
-
RequestAnimationFrame.current = injected;
|
|
5
|
-
},
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const CancelAnimationFrame = {
|
|
9
|
-
current: (id: any) => window.cancelAnimationFrame(id),
|
|
10
|
-
inject(injected: any) {
|
|
11
|
-
CancelAnimationFrame.current = injected;
|
|
12
|
-
},
|
|
13
|
-
};
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { Animation } from './Animation';
|
|
2
|
-
import {
|
|
3
|
-
RequestAnimationFrame,
|
|
4
|
-
CancelAnimationFrame,
|
|
5
|
-
} from './RequestAnimationFrame';
|
|
6
|
-
|
|
7
|
-
import type { FluidValueConfig, ResultType } from '../types/animation';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Class implementing spring based animation
|
|
11
|
-
*/
|
|
12
|
-
export class SpringAnimation extends Animation {
|
|
13
|
-
_overshootClamping: boolean;
|
|
14
|
-
_restDisplacementThreshold: number;
|
|
15
|
-
_restSpeedThreshold: number;
|
|
16
|
-
_initialVelocity?: number;
|
|
17
|
-
_lastVelocity: number;
|
|
18
|
-
_startPosition: number;
|
|
19
|
-
_lastPosition: number;
|
|
20
|
-
_position: number;
|
|
21
|
-
_fromValue: number;
|
|
22
|
-
_toValue: any;
|
|
23
|
-
_mass: number;
|
|
24
|
-
_tension: number;
|
|
25
|
-
_friction: number;
|
|
26
|
-
_lastTime: number;
|
|
27
|
-
_onFrame: (value: number) => void;
|
|
28
|
-
_animationFrame: any;
|
|
29
|
-
_timeout: any;
|
|
30
|
-
|
|
31
|
-
// Modifiers
|
|
32
|
-
_delay: number;
|
|
33
|
-
_onRest?: (value: ResultType) => void;
|
|
34
|
-
_onChange?: (value: number) => void;
|
|
35
|
-
|
|
36
|
-
constructor({
|
|
37
|
-
initialPosition,
|
|
38
|
-
config,
|
|
39
|
-
}: {
|
|
40
|
-
initialPosition: number;
|
|
41
|
-
config?: Omit<FluidValueConfig, 'duration' | 'easing'>;
|
|
42
|
-
}) {
|
|
43
|
-
super();
|
|
44
|
-
|
|
45
|
-
this._overshootClamping = false;
|
|
46
|
-
this._initialVelocity = 0;
|
|
47
|
-
this._lastVelocity = 0;
|
|
48
|
-
this._startPosition = initialPosition;
|
|
49
|
-
this._position = this._startPosition;
|
|
50
|
-
|
|
51
|
-
this._restDisplacementThreshold = config?.restDistance ?? 0.001;
|
|
52
|
-
this._restSpeedThreshold = config?.restDistance ?? 0.001;
|
|
53
|
-
this._mass = config?.mass ?? 1;
|
|
54
|
-
this._tension = config?.tension ?? 170;
|
|
55
|
-
this._friction = config?.friction ?? 26;
|
|
56
|
-
this._delay = config?.delay ?? 0;
|
|
57
|
-
|
|
58
|
-
this._onRest = config?.onRest;
|
|
59
|
-
this._onChange = config?.onChange;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
onChange(value: number) {
|
|
63
|
-
this._onFrame(value);
|
|
64
|
-
|
|
65
|
-
if (this._lastPosition !== value) {
|
|
66
|
-
if (this._onChange) {
|
|
67
|
-
this._onChange(value);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
this._lastPosition = value;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
onUpdate() {
|
|
75
|
-
var now = Date.now();
|
|
76
|
-
|
|
77
|
-
const deltaTime = Math.min(now - this._lastTime, 64);
|
|
78
|
-
this._lastTime = now;
|
|
79
|
-
|
|
80
|
-
const c = this._friction;
|
|
81
|
-
const m = this._mass;
|
|
82
|
-
const k = this._tension;
|
|
83
|
-
|
|
84
|
-
const v0 = -this._lastVelocity;
|
|
85
|
-
const x0 = this._toValue - this._position;
|
|
86
|
-
|
|
87
|
-
const zeta = c / (2 * Math.sqrt(k * m)); // damping ratio
|
|
88
|
-
const omega0 = Math.sqrt(k / m); // undamped angular frequency of the oscillator (rad/ms)
|
|
89
|
-
const omega1 = omega0 * Math.sqrt(1 - zeta ** 2); // exponential decay
|
|
90
|
-
|
|
91
|
-
const t = deltaTime / 1000;
|
|
92
|
-
|
|
93
|
-
const sin1 = Math.sin(omega1 * t);
|
|
94
|
-
const cos1 = Math.cos(omega1 * t);
|
|
95
|
-
|
|
96
|
-
// under damped
|
|
97
|
-
const underDampedEnvelope = Math.exp(-zeta * omega0 * t);
|
|
98
|
-
const underDampedFrag1 =
|
|
99
|
-
underDampedEnvelope *
|
|
100
|
-
(sin1 * ((v0 + zeta * omega0 * x0) / omega1) + x0 * cos1);
|
|
101
|
-
|
|
102
|
-
const underDampedPosition = this._toValue - underDampedFrag1;
|
|
103
|
-
// This looks crazy -- it's actually just the derivative of the oscillation function
|
|
104
|
-
const underDampedVelocity =
|
|
105
|
-
zeta * omega0 * underDampedFrag1 -
|
|
106
|
-
underDampedEnvelope *
|
|
107
|
-
(cos1 * (v0 + zeta * omega0 * x0) - omega1 * x0 * sin1);
|
|
108
|
-
|
|
109
|
-
// critically damped
|
|
110
|
-
const criticallyDampedEnvelope = Math.exp(-omega0 * t);
|
|
111
|
-
const criticallyDampedPosition =
|
|
112
|
-
this._toValue - criticallyDampedEnvelope * (x0 + (v0 + omega0 * x0) * t);
|
|
113
|
-
|
|
114
|
-
const criticallyDampedVelocity =
|
|
115
|
-
criticallyDampedEnvelope *
|
|
116
|
-
(v0 * (t * omega0 - 1) + t * x0 * omega0 * omega0);
|
|
117
|
-
|
|
118
|
-
this.onChange(this._position);
|
|
119
|
-
|
|
120
|
-
const isOvershooting = () => {
|
|
121
|
-
if (this._overshootClamping && this._tension !== 0) {
|
|
122
|
-
return this._position < this._toValue
|
|
123
|
-
? this._position > this._toValue
|
|
124
|
-
: this._position < this._toValue;
|
|
125
|
-
} else {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
const isVelocity = Math.abs(this._lastVelocity) < this._restSpeedThreshold;
|
|
131
|
-
const isDisplacement =
|
|
132
|
-
this._tension === 0 ||
|
|
133
|
-
Math.abs(this._toValue - this._position) <
|
|
134
|
-
this._restDisplacementThreshold;
|
|
135
|
-
|
|
136
|
-
if (zeta < 1) {
|
|
137
|
-
this._position = underDampedPosition;
|
|
138
|
-
this._lastVelocity = underDampedVelocity;
|
|
139
|
-
} else {
|
|
140
|
-
this._position = criticallyDampedPosition;
|
|
141
|
-
this._lastVelocity = criticallyDampedVelocity;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (isOvershooting() || (isVelocity && isDisplacement)) {
|
|
145
|
-
if (this._tension !== 0) {
|
|
146
|
-
this._lastVelocity = 0;
|
|
147
|
-
this._position = this._toValue;
|
|
148
|
-
|
|
149
|
-
this.onChange(this._position);
|
|
150
|
-
}
|
|
151
|
-
// clear lastTimestamp to avoid using stale value by the next spring animation that starts after this one
|
|
152
|
-
this._lastTime = 0;
|
|
153
|
-
|
|
154
|
-
this._debounceOnEnd({ finished: true, value: this._toValue });
|
|
155
|
-
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
this._animationFrame = RequestAnimationFrame.current(
|
|
160
|
-
this.onUpdate.bind(this)
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
stop() {
|
|
165
|
-
this._active = false;
|
|
166
|
-
clearTimeout(this._timeout);
|
|
167
|
-
CancelAnimationFrame.current(this._animationFrame);
|
|
168
|
-
this._debounceOnEnd({ finished: false, value: this._position });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Set value
|
|
172
|
-
set(toValue: number) {
|
|
173
|
-
this.stop();
|
|
174
|
-
this._position = toValue;
|
|
175
|
-
this._lastTime = 0;
|
|
176
|
-
this._lastVelocity = 0;
|
|
177
|
-
this.onChange(toValue);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
start({
|
|
181
|
-
toValue,
|
|
182
|
-
onFrame,
|
|
183
|
-
previousAnimation,
|
|
184
|
-
onEnd,
|
|
185
|
-
}: {
|
|
186
|
-
toValue: number;
|
|
187
|
-
onFrame: (value: number) => void;
|
|
188
|
-
previousAnimation?: SpringAnimation;
|
|
189
|
-
onEnd?: (result: ResultType) => void;
|
|
190
|
-
}) {
|
|
191
|
-
const onStart: any = () => {
|
|
192
|
-
this._onFrame = onFrame;
|
|
193
|
-
this._active = true;
|
|
194
|
-
this._toValue = toValue;
|
|
195
|
-
this._onEnd = onEnd;
|
|
196
|
-
|
|
197
|
-
const now = Date.now();
|
|
198
|
-
|
|
199
|
-
if (previousAnimation instanceof SpringAnimation) {
|
|
200
|
-
this._lastVelocity =
|
|
201
|
-
previousAnimation._lastVelocity || this._lastVelocity || 0;
|
|
202
|
-
this._lastTime = previousAnimation._lastTime || now;
|
|
203
|
-
} else {
|
|
204
|
-
this._lastTime = now;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
this._animationFrame = RequestAnimationFrame.current(
|
|
208
|
-
this.onUpdate.bind(this)
|
|
209
|
-
);
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
if (this._delay !== 0) {
|
|
213
|
-
this._timeout = setTimeout(() => onStart(), this._delay);
|
|
214
|
-
} else {
|
|
215
|
-
onStart();
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|