Last active
May 9, 2020 02:39
-
-
Save wklsh/445eef5f20ca4f0ee9f7c2ee10d60c46 to your computer and use it in GitHub Desktop.
Calculate Distance Between Mouse and center of Element
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
/** | |
* Calculate disance between mouse and center of element | |
* @param {obj} element target element | |
* @param {num} mouseX mouse pos X value | |
* @param {num} mouseY mouse pos Y value | |
*/ | |
export function calcDistance(element, mouseX, mouseY) { | |
const elDimensions = element.getBoundingClientRect(); | |
return Math.floor( | |
Math.sqrt( | |
Math.pow(mouseX - (elDimensions.left + elDimensions.width / 2), 2) + | |
Math.pow(mouseY - (elDimensions.top + elDimensions.height / 2), 2), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment