Calculate degree based on focalX and focalY in PinchGestureHandler. React Native Reanimated 2 #2374
-
I want to calculate rotation degree of View using focalX and focalY. How we can calculate degree with such focal points or is their any other way to do it using PinchGestureHandler? import React from 'react';
import { StyleSheet, Image, Dimensions } from 'react-native';
import {
PinchGestureHandler,
PinchGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
import Animated, {
useAnimatedGestureHandler,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
// Credit to Mariana Ibanez https://unsplash.com/photos/NJ8Z8Y_xUKc
const imageUri =
'https://images.unsplash.com/photo-1621569642780-4864752e847e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=668&q=80';
const AnimatedImage = Animated.createAnimatedComponent(Image);
const { width, height } = Dimensions.get('window');
export default function App() {
const scale = useSharedValue(1);
const focalX = useSharedValue(0);
const focalY = useSharedValue(0);
const pinchHandler =
useAnimatedGestureHandler({
onActive: (event) => {
scale.value = event.scale;
focalX.value = event.focalX;
focalY.value = event.focalY;
},
onEnd: () => {
scale.value = withTiming(1);
},
});
const rStyle = useAnimatedStyle(() => {
return {
transform: [
{ translateX: focalX.value },
{ translateY: focalY.value },
{ translateX: -width / 2 },
{ translateY: -height / 2 },
{ scale: scale.value },
{ translateX: -focalX.value },
{ translateY: -focalY.value },
{ translateX: width / 2 },
{ translateY: height / 2 },
],
};
});
const focalPointStyle = useAnimatedStyle(() => {
return {
transform: [{ translateX: focalX.value }, { translateY: focalY.value }],
};
});
return (
<PinchGestureHandler onGestureEvent={pinchHandler}>
<Animated.View style={{ flex: 1 }}>
<AnimatedImage
style={[{ flex: 1 }, rStyle]}
source={{ uri: imageUri }}
/>
<Animated.View style={[styles.focalPoint, focalPointStyle]} />
</Animated.View>
</PinchGestureHandler>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
focalPoint: {
...StyleSheet.absoluteFillObject,
width: 20,
height: 20,
backgroundColor: 'blue',
borderRadius: 10,
},
}); Here is the Snack too! I want to rotate it too, while making gesture! @wcandillon Kindly help me Sir. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
There's RotationGH for that. |
Beta Was this translation helpful? Give feedback.
-
@jakub-gonet have a look at code below; const style = useAnimatedStyle(() => {
return {
transform: [
{ scale: scale.value },
]
}
})
<PinchGestureHandler onGestureEvent={panHandler}>
<Animated.View style={[styles.paintContainer, style]} />
</PinchGestureHandler> Above code is what I'm doing in order to scale it. I want to rotate it too. So according to you I can do it like below ...
<PinchGestureHandler onGestureEvent={panHandler}>
<RotationGestureHandler
onGestureEvent={this._onRotateGestureEvent}
onHandlerStateChange={this._onRotateHandlerStateChange}>
<Animated.View style={[styles.paintContainer, style]} />
</RotationGestureHandler>
</PinchGestureHandler> This is how we can use both gestures? |
Beta Was this translation helpful? Give feedback.
@jakub-gonet have a look at code below;
Above code is what I'm doing in order to scale it. I want to rotate it too. So according to you I can do it like below