-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
133 lines (122 loc) · 3.09 KB
/
App.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
122
123
124
125
126
127
128
129
130
131
132
133
import React from 'react';
import {View, StyleSheet, Platform} from 'react-native';
import {
GestureDetector,
Gesture,
GestureHandlerRootView,
} from 'react-native-gesture-handler';
import Animated, {
useSharedValue,
useAnimatedStyle,
} from 'react-native-reanimated';
import Svg, {Circle, G, Text, TSpan} from 'react-native-svg';
const AnimatedSvg = Animated.createAnimatedComponent(Svg);
const App = () => {
const offset = useSharedValue({x: 0, y: 0});
const start = useSharedValue({x: 0, y: 0});
const scale = useSharedValue(1);
const savedScale = useSharedValue(1);
const rotation = useSharedValue(0);
const savedRotation = useSharedValue(0);
const animatedStyles = useAnimatedStyle(() => {
return {
transform: [
{translateX: offset.value.x},
{translateY: offset.value.y},
{scale: scale.value},
{rotateZ: `${rotation.value}rad`},
],
};
});
const dragGesture = Gesture.Pan()
.averageTouches(true)
.onUpdate(e => {
offset.value = {
x: e.translationX + start.value.x,
y: e.translationY + start.value.y,
};
})
.onEnd(() => {
start.value = {
x: offset.value.x,
y: offset.value.y,
};
});
const zoomGesture = Gesture.Pinch()
.onUpdate(event => {
scale.value = savedScale.value * event.scale;
})
.onEnd(() => {
savedScale.value = scale.value;
});
const rotateGesture = Gesture.Rotation()
.onUpdate(event => {
rotation.value = savedRotation.value + event.rotation;
})
.onEnd(() => {
savedRotation.value = rotation.value;
});
const composed = Gesture.Simultaneous(
dragGesture,
Gesture.Simultaneous(zoomGesture, rotateGesture),
);
return (
<View style={styles.root}>
<GestureDetector gesture={composed}>
<Animated.View style={styles.root}>
<AnimatedSvg style={[styles.svg, animatedStyles]}>
<G>
<PanelsList panels={[1, 2, 3]} />
</G>
</AnimatedSvg>
</Animated.View>
</GestureDetector>
</View>
);
};
const PanelsList = ({panels}: {panels: any}) => {
return panels.map((panel: any, index: number) => {
return (
<G
key={`${panel}-${index}`}
onPressIn={() => {
console.log(`touch event in ${panel}`);
}}
delayPressIn={Platform.OS === 'ios' ? 400 : 5}
x={panel * 60}
y={panel * 60}>
<Circle r={30} fill="white" stroke="purple" strokeWidth={5} />
<Text
fontSize={14}
textAnchor="middle"
stroke={'#000'}
fill={'#000'}
x={0}
y={0}>
<TSpan cy={0} cx={0} fontWeight={'100'}>
{panel}
</TSpan>
</Text>
</G>
);
});
};
const styles = StyleSheet.create({
root: {
flex: 1,
backgroundColor: 'blue',
},
svg: {
height: 300,
width: 300,
backgroundColor: 'skyblue',
},
});
const MAAAIN = () => {
return (
<GestureHandlerRootView style={{flex: 1}}>
<App />
</GestureHandlerRootView>
);
};
export default MAAAIN;