Skip to content Skip to sidebar Skip to footer

React-native: Get X And Y Coordinates Of Draggable Object Using Panresponder.panhandlers (nativeelement)

I am developing an app, which provides the design of Different shapes like Square and Circle, I have used the following Library https://www.npmjs.com/package/react-native-draggabl

Solution 1:

I used react-native-draggable in a project too. pageX, pageY are the coordinates of your actual touch, while locationX, locationY are the offset of your touch from the top-left of the object. You didn't specify how you're rendering the shapes, but for instance if you're rendering an image to drag, you can get the exact top-left coordinates by getting (pageX-locationX), (pageY-locationY). And you can pull them out of nativeEvent like this:

<Draggable
  renderSize={68}
  renderColor='rad'
  x={80.80097961425781}
  y={72.79156494140625}
  renderText='B'
  onDragRelease={(e) => {console.log("pageX, pageY = " + e.nativeEvent.pageX + ", " + e.nativeEvent.pageY);
  console.log("locX, locY = " + e.nativeEvent.locationX + ", " + e.nativeEvent.locationY)}}>
  reverse={false}
/>

Also, I changed pressDragRelease to onDragRelease; not sure if you wrapped onDragRelease in your own separate function or you have a different version or something, but in mine it's onDrag / onDragRelease. Hope that helps.

Post a Comment for "React-native: Get X And Y Coordinates Of Draggable Object Using Panresponder.panhandlers (nativeelement)"