https://codesandbox.io/s/drag-find-y-f2jltf
I know what I want to accomplish but I don’t know how to go about it.
You can see in my CodeSandbox when you drag the balls towards the target element (the box with white border) the X always has a certain distance it has to travel. Which is great because I can use this fixed distance for other equations. You can see this in the console log.
But because the balls run down the height of the page, the distance on the y axis that it takes to reach the target varies every time. What equation do I use to get a fixed Y for this distance that is the same for every ball? I want to use this value for other equations.
What am I not considering to factor into the equation? The height of the overall page minus something else?
I tried to explain this the best I can I just don’t know how to explain it.
2
Answers
I figured it out. All I needed to do was use the React pointer event onPointerMove in order to get access to currentTarget. On currentTarget I now have access to getBoundingClientRect() which has the properties I need such as top and left.
These properties show me the distance between my ball and the top and left of the screen. I log these and store them in my motionValue. While dragging the balls I console log the values and note down where the ball needs to be in order to be inside the target.
Code below and codesandbox updated: https://codesandbox.io/s/drag-find-y-f2jltf?file=/src/App.js
Assuming that you are dealing with a linear relationship between x and y, what you need to do to figure out y from x is to plug into the inverse linear transformation matrix.
How’s your math? There’s definitely information on how to do this on the web.