Webkit's blog post from last year on 3D transforms explains the various transform 'functions' that can be used in the -webkit-transform property. For example:
#myDiv {
-webkit-transform: scale(1.1) rotateY(7deg) translateZ(-1px);
}
My question: how do you access individual values in JavaScript? When you read the webkitTransform property of the element, you just get a matrix3d() function with 16 values in it, like this...
matrix3d(0.958684, 0.000000, .....)
Is there a way to just read the value of an individual transform thing, like rotateY()? Or do I have to read it from the matrix3d() string, and how?
Easy way to extract individual values from a complex transform value:
I think, as syockit says, iterating through the stylesheets is the only way to go, you can use
webkitMatchesSelector
to discover rules which match your element:This assumes markup something like this, I added some extra rules and values to make sure I was pulling out the right values. If you have more than one stylesheet then you need to adjust the first part to iterate through all the stylesheets too, and you'll probably have to deal with specificity if your
-webkit-transform
appears in more than one rule:Just because I didn´t see any working Javascript one Line solutions to convert the matrix code, here is mine. Quick and easy:
First get all the transform Values in a matrix:
To extract transform-y as an Integer:
To extract transform-x as an Integer:
Accessing the rotation value is quite difficult, but there is a good guide, if you want to do it: https://css-tricks.com/get-value-of-css-rotation-through-javascript/
Old but interesting question and no previous response has attempted to answer it.
Unfortunately there's no two-liner solution for the general case and we don't know about what variety of transform steps (rotate, translate etc.) need to be reverse engineered; the fewer the easier.
In recent webkit based browsers, one can simply query the previously assigned property:
Let's continue with the assumption that the computed style needs to be used. The need arises, for example, if the element is in the middle of a CSS transition or CSS animation, i.e. the current transformation is not one that was set directly.
The matrix descriptor string returned by getComputedStyle can indeed be parsed via a regex, but not all versions above are reliable, and they're too opaque. A straightforward way to break down the string, unless it's in a very tight loop to warrant a single-pass regex, is this:
But another way is to simply use this, which wasn't mentioned before:
The benefit of the latter approach is that you get back a 4x4 matrix as well, which is the standard representation for affine transformations, and the drawback is that it's of course WebKit specific. Should you continue to work with the tuple of six values as in the question, it's defined in this part of the CSS standard.
Then the transform, e.g. rotation needs to be reverse engineered. There can be many simple transforms directly supported by CSS, e.g. translate, scale, rotate, skew, and perspective. If only one of them is applied, then you just need to revert the process of computing the transform matrix.
An alternative is to find or translate code which does this for you in JS, e.g. the same document or Section 7.1 of the CSS standard contains such annotated algorithms. The benefit is that the unmatrix approach is able to, with some limitations, return the 'atomic' transforms even if more than one of these (translate, rotate etc.) is applied. Since it's not possible to guarantee the successful reverse engineering of the transform steps for the general case, it's useful to think about what types of transforms are possibly applied, and whether degenerate or other troublesome cases have been avoided. I.e. you need to build the solution as you see fit for your problem.
Specifically, the matrix versions of all 2D CSS transforms are documented here as well, below the meaning of the six values in the CSS 2D transform vector.
Another caveat is that there are other things that influence the visual outcome in terms of geometric operations, for example, transform-origin.
I thought of one possibility. If you're prepared to parse strings in JavaScript, use
data=document.getElementById("myDiv").getAttribute("-webkit-transform");
then interpret
data
.Since you only get the final matrix value from the computed style, you might have to check the element's inline style or stylesheet rules. If
element.style.webkitTransform
gives you nothing, you might to iterate through the document's stylesheets, and see which one matches your element. Then you can regex thewebkitTransform
property to get/set the value.