The getRelativeCoordinates function is used to calculate the position of the mouse cursor relative to a specific HTML element. (As you can see here: Coordinates)
You kinda were getting the mouse cursor’s coordinates relative to the window but then you asked if you could get the mouse cursor’s coordinates relative to the #ui-wrapper.
The getRelativeCoordinates function takes two arguments:
Event: The mouse event object, which contains information about the mouse cursor’s position.
Element: The HTML element for which you want to calculate the cursor’s relative position.
And, as per the code I wrote before, inside the function you can see I’m calling the getBounding method on the element, this method will return an object containing the size and position of the element, having properties like top, right, bottom and left.
These properties represent the distance from the edges to the viewport.
After that we subtract the “left” and the “top” properties from the clientx and client y. These events represent the horizontal and vertical coordinates of the mouse cursor relative to the viewport.
So, if we subtract the element position from the cursor position, we get the cursor position.
This is what the getRelativeCoordinates function returns: an object containing the “x” and the “y” coordinates of the cursos relative to the given element.
When you’re passing in e, top left of your screen will always be (0, 0) because that’s the cords in relation to the window. I realized that when attempting to create a version for myself. The new coordinates function is displaying (0, 0) when hovering top left of your screen.
Actually, you only need to change your e.x and e.y to e.clientXand e.clientY respectively in your original script, since they get the mouse x and y coordinates relative to the viewport of the element being clicked on.
You can look at the examples and explanations in MDN here