Last active
August 29, 2015 14:23
-
-
Save jabney/7133d6ddb9e44bfc7720 to your computer and use it in GitHub Desktop.
Javascript function to return post-transform coordinates of an 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
// Return the post-transform coords of an element, | |
// assuming that proper pre-transform coords are supplied. | |
function getElementCoords(element, coords) { | |
var ctm = element.getCTM(), | |
xn = ctm.e + coords.x*ctm.a, | |
yn = ctm.f + coords.y*ctm.d; | |
return { x: xn, y: yn }; | |
}; | |
var circle = document.getElementById('svgCircle'), | |
x = +circle.getAttribute('cx'), | |
y = +circle.getAttribute('cy'), | |
coords = getElementCoords(circle, {x:x, y:y}); // Any object with x,y properties will do. | |
// An alternate method that takes translate and scale | |
// parameters to accomplish the same task. | |
function getScreenCoords(x, y, translate, scale) { | |
var xn = translate[0] + x*scale; | |
var yn = translate[1] + y*scale; | |
return { x: xn, y: yn }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment