forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get relative coords (software-mansion#2400)
This pr introduces the getRelativeCoords function which enables to convert absolute coordinates to coordinates relative to the given view.
- Loading branch information
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
id: getRelativeCoords | ||
title: getRelativeCoords | ||
sidebar_label: getRelativeCoords | ||
--- | ||
|
||
Determines the location on the screen, relative to the given view. It might be useful, when there are only absolute coordinates available and you need coordinates relative to the parent. | ||
|
||
### Arguments | ||
|
||
#### animatedRef | ||
|
||
The product of [`useAnimatedRef`](../hooks/useAnimatedRef) is a Reanimated's extension of a standard React's ref(delivers view tag on the UI thread). This ref should be passed as a prop to view relative to which we want to know coordinates. | ||
|
||
#### x | ||
|
||
Absolute `x` coordinate. | ||
|
||
#### y | ||
|
||
Absolute `y` coordinate | ||
|
||
### Returns | ||
|
||
Object which contains relative coordinates | ||
|
||
- `x` | ||
- `y` | ||
|
||
### Example | ||
|
||
```js | ||
const Comp = () => { | ||
const aref = useAnimatedRef(); | ||
// ... | ||
|
||
const gestureHandler = useAnimatedGestureHandler({ | ||
onEnd: (event) => { | ||
getRelativeCoords(aref, event.absoluteX, event.absoluteY) | ||
}, | ||
}); | ||
|
||
return <View ref={aref}> | ||
<PanGestureHandler onGestureEvent={gestureHandler}> | ||
<Animated.View style={[styles.box]} /> | ||
</PanGestureHandler> | ||
</View>; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Component } from 'react'; | ||
import { measure } from '.'; | ||
import { RefObjectFunction } from './hook/useAnimatedRef'; | ||
|
||
export interface ComponentCoords { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
export function getRelativeCoords( | ||
parentRef: RefObjectFunction<Component>, | ||
x: number, | ||
y: number | ||
): ComponentCoords { | ||
'worklet'; | ||
const parentCoords = measure(parentRef); | ||
return { | ||
x: x - parentCoords.x, | ||
y: y - parentCoords.y, | ||
}; | ||
} |