Skip to content

Instantly share code, notes, and snippets.

@benbarnett
Created December 22, 2010 11:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbarnett/751425 to your computer and use it in GitHub Desktop.
Save benbarnett/751425 to your computer and use it in GitHub Desktop.
Calculate translation from transform matrix
function translation(elem) {
var cssPrefixes = ["", "-webkit-", "-moz-", "-o-"],
cStyle = window.getComputedStyle(elem, null),
translation = {
x: 0,
y: 0
};
for (var i = cssPrefixes.length - 1; i >= 0; i--){
var transform = cStyle.getPropertyValue(cssPrefixes[i] + "transform");
if (transform && (/matrix/i).test(transform)) {
var explodedMatrix = transform.replace(/^matrix\(/i, '').split(/, |\)$/g);
translation = {
x: explodedMatrix[4],
y: explodedMatrix[5]
};
break;
}
}
return translation;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment