Skip to content

Instantly share code, notes, and snippets.

@ethyde
Created January 10, 2016 18:52
Show Gist options
  • Save ethyde/d8a72fa371d487e87257 to your computer and use it in GitHub Desktop.
Save ethyde/d8a72fa371d487e87257 to your computer and use it in GitHub Desktop.
getCoordinate of any box in JS
var getCoordinate = function(elemId, parentId){
var element = document.getElementById(elemId);
var context = document.getElementById(parentId);
var bodyRect = context.getBoundingClientRect(),
pointPos = element.getBoundingClientRect(),
offsetY = pointPos.top - bodyRect.top;
offsetX = pointPos.left - bodyRect.left;
centerX = offsetX + pointPos.width / 2;
centerY = offsetY + pointPos.height / 2;
return {
height: pointPos.height,
width: pointPos.width,
x: centerX,
y: centerY
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment