Skip to content

Instantly share code, notes, and snippets.

@jabney
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabney/7133d6ddb9e44bfc7720 to your computer and use it in GitHub Desktop.
Save jabney/7133d6ddb9e44bfc7720 to your computer and use it in GitHub Desktop.
Javascript function to return post-transform coordinates of an element.
// 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