Skip to content

Instantly share code, notes, and snippets.

@milosdjakonovic
Last active December 30, 2015 17:04
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 milosdjakonovic/66a8861cd26e6cd707d6 to your computer and use it in GitHub Desktop.
Save milosdjakonovic/66a8861cd26e6cd707d6 to your computer and use it in GitHub Desktop.
Small tool for conversion of transform functions from human readable to matrix and matrix3d values
/*
* Useful for forcing all transform functions defined in @keyframes to execute simultaneously
* Many (including myself until recently) doesn't know that transform functions defined
* into @keyframes doesn't play simultaneously
* but in reversed order of declaration
*
* More info about this issue:
* https://css-tricks.com/debugging-css-keyframe-animations/
*
* USAGE
* simply calcaulate matrix by:
* 1. including this script in somewhere in <body>
* 2. doing the job with MatrixConv('transform value')
*
* for example: MatrixConv('rotate(180deg) translate3d(199px, 20px,0)')
* returns 'matrix(-1, 1.22465e-16, -1.22465e-16, -1, -199, -20)'
--------------------
*/
window.MatrixConv=window.MatrixConv||(function(w,d){
var div = d.createElement('div'), transformProperty;
div.style.visibility='hidden';
div.style.position='absolute';
if('transform' in div.style){
transformProperty='transform'
} else if( 'WebkitTransform' in div.style ){
transformProperty='webkitTransform'
} else if('MozTransform' in div.style){
transformProperty='MozTransform'
} else if('OTransform' in div.style){
transformProperty='OTransform'
}
return function(transformReadable){
div.style[transformProperty]=transformReadable;
d.body.appendChild(div);
var computed = w.getComputedStyle(div,null),
computedTransform = computed[transformProperty];
d.body.removeChild(div);
div.style[transformProperty]='none';
return computedTransform;
}
})(window,document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment