-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathFrameCallbackExample.tsx
121 lines (110 loc) · 2.6 KB
/
FrameCallbackExample.tsx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import Animated, {
FrameInfo,
useAnimatedStyle,
useFrameCallback,
useSharedValue,
} from 'react-native-reanimated';
import { Button, StyleSheet, View } from 'react-native';
import React from 'react';
export default function FrameCallbackExample() {
const limit = 200;
const x1 = useSharedValue(0);
const y1 = useSharedValue(0);
const frameCallback1 = useFrameCallback((frameInfo: FrameInfo) => {
if (frameInfo.timeSincePreviousFrame === null) {
console.log('First frame!');
} else {
console.log('Frame info:', frameInfo);
}
if (x1.value === limit && y1.value !== 0) {
y1.value -= 1;
}
if (x1.value === 0 && y1.value !== limit) {
y1.value += 1;
}
if (y1.value === limit && x1.value !== limit) {
x1.value += 1;
}
if (y1.value === 0 && x1.value !== 0) {
x1.value -= 1;
}
}, false);
const animatedStyle1 = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x1.value,
},
{
translateY: y1.value,
},
],
};
});
const x2 = useSharedValue(0);
const y2 = useSharedValue(0);
const frameCallback2 = useFrameCallback(() => {
if (x2.value === limit && y2.value !== 0) {
y2.value -= 2;
}
if (x2.value === 0 && y2.value !== limit) {
y2.value += 2;
}
if (y2.value === limit && x2.value !== limit) {
x2.value += 2;
}
if (y2.value === 0 && x2.value !== 0) {
x2.value -= 2;
}
}, false);
const animatedStyle2 = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x2.value,
},
{
translateY: y2.value,
},
],
};
});
return (
<View style={styles.container}>
<View style={styles.container}>
<Animated.View style={[styles.box1, animatedStyle1]} />
<Animated.View style={[styles.box2, animatedStyle2]} />
</View>
<View style={styles.buttonContainer}>
<Button
title="Start/stop square1 animation 1"
onPress={() => frameCallback1.setActive(!frameCallback1.isActive)}
/>
<Button
title="Start/stop square2 animation"
onPress={() => frameCallback2.setActive(!frameCallback2.isActive)}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
buttonContainer: {
paddingBottom: 50,
},
box1: {
position: 'absolute',
width: 50,
height: 50,
backgroundColor: 'navy',
},
box2: {
position: 'absolute',
width: 50,
height: 50,
backgroundColor: 'red',
},
});