-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
JumpingTransition.ts
78 lines (74 loc) · 2.51 KB
/
JumpingTransition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
import type {
ILayoutAnimationBuilder,
LayoutAnimationFunction,
} from '../animationBuilder/commonTypes';
import { withSequence, withTiming } from '../../animation';
import { Easing } from '../../Easing';
import { BaseAnimationBuilder } from '../animationBuilder';
/**
* Layout jumps - quite literally - from one position to another. You can modify the behavior by chaining methods like `.springify()` or `.duration(500)`.
*
* You pass it to the `layout` prop on [an Animated component](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#animated-component).
*
* @see https://docs.swmansion.com/react-native-reanimated/docs/layout-animations/layout-transitions#jumping-transition
*/
export class JumpingTransition
extends BaseAnimationBuilder
implements ILayoutAnimationBuilder
{
static presetName = 'JumpingTransition';
static createInstance<T extends typeof BaseAnimationBuilder>(
this: T
): InstanceType<T> {
return new JumpingTransition() as InstanceType<T>;
}
build = (): LayoutAnimationFunction => {
const delayFunction = this.getDelayFunction();
const callback = this.callbackV;
const delay = this.getDelay();
const duration = (this.durationV ?? 300) / 2;
const config = { duration: duration * 2 };
return (values) => {
'worklet';
const d = Math.max(
Math.abs(values.targetOriginX - values.currentOriginX),
Math.abs(values.targetOriginY - values.currentOriginY)
);
return {
initialValues: {
originX: values.currentOriginX,
originY: values.currentOriginY,
width: values.currentWidth,
height: values.currentHeight,
},
animations: {
originX: delayFunction(
delay,
withTiming(values.targetOriginX, config)
),
originY: delayFunction(
delay,
withSequence(
withTiming(
Math.min(values.targetOriginY, values.currentOriginY) - d,
{
duration,
easing: Easing.out(Easing.exp),
}
),
withTiming(values.targetOriginY, {
...config,
duration,
easing: Easing.bounce,
})
)
),
width: delayFunction(delay, withTiming(values.targetWidth, config)),
height: delayFunction(delay, withTiming(values.targetHeight, config)),
},
callback,
};
};
};
}