Skip to content

Commit

Permalink
Get relative coords (software-mansion#2400)
Browse files Browse the repository at this point in the history
This pr introduces the getRelativeCoords function which enables to convert absolute coordinates to coordinates relative to the given view.
  • Loading branch information
jmysliv authored Sep 27, 2021
1 parent fc81672 commit 756bd34
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/docs/api/miscellaneous/getRelativeCoords.md
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>;
};
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = {
'api/miscellaneous/runOnJS',
'api/miscellaneous/runOnUI',
'api/miscellaneous/interpolate',
'api/miscellaneous/getRelativeCoords',
],
},
],
Expand Down
9 changes: 9 additions & 0 deletions react-native-reanimated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@ declare module 'react-native-reanimated' {
pageY: number;
};

export function getRelativeCoords(
ref: RefObject<Component>,
x: number,
y: number
): {
x: number;
y: number;
};

export function scrollTo(
ref: RefObject<ReactNativeScrollView | ScrollView>,
x: number,
Expand Down
1 change: 1 addition & 0 deletions src/reanimated2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './NativeMethods';
export * from './Colors';
export * from './PropAdapters';
export * from './layoutReanimation';
export * from './utils';
21 changes: 21 additions & 0 deletions src/reanimated2/utils.ts
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,
};
}

0 comments on commit 756bd34

Please sign in to comment.