This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Cohereum/fix-offset-coordinates
Fix for inconsistent values from nativeEvent.offsetX and Y
- Loading branch information
Showing
5 changed files
with
30 additions
and
18 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
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,23 @@ | ||
function getOffsetCoordPercentage(e, container) { | ||
// nativeEvent.offsetX gives inconsistent results when dragging | ||
// up and to the left rather than the more natural down and to the | ||
// right. The reason could be browser implementation (it is still experimental) | ||
// or it could be that nativeEvent offsets are based on target rather than | ||
// currentTarget. | ||
// To keep consistent behavior of the selector use the bounding client rect. | ||
const rect = container.getBoundingClientRect(); | ||
const offsetX = e.clientX - rect.x; | ||
const offsetY = e.clientY - rect.y; | ||
|
||
return { | ||
x: offsetX / rect.width * 100, | ||
y: offsetY / rect.height * 100 | ||
}; | ||
} | ||
|
||
function getCoordPercentage(e) { | ||
return getOffsetCoordPercentage(e, e.currentTarget); | ||
} | ||
|
||
|
||
export { getOffsetCoordPercentage, getCoordPercentage }; |
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