我正在尝试使用CSS3 rotateY
旋转图像。我需要使用jQuery获得旋转的角度。
我的问题是理解图像旋转了多少圈。
的例子:
180 degrees: matrix3d(-1, 0, -0.00000000000000012246467991473532, 0, 0, 1, 0, 0, 0.00000000000000012246467991473532, 0, -1, 0, 0, 0, 0, 1)
360 degrees: matrix3d(1, 0, 0.00000000000000024492935982947064, 0, 0, 1, 0, 0, -0.00000000000000024492935982947064, 0, 1, 0, 0, 0, 0, 1)
540 degrees: matrix3d(-1, 0, -0.00000000000000036739403974420594, 0, 0, 1, 0, 0, 0.00000000000000036739403974420594, 0, -1, 0, 0, 0, 0, 1)
720 degrees: matrix3d(1, 0, 0.0000000000000004898587196589413, 0, 0, 1, 0, 0, -0.0000000000000004898587196589413, 0, 1, 0, 0, 0, 0, 1)
可以看到,每180度,第三个元素的绝对值加0.00000000000000012246467991473532。我会对这个结果感到满意,然而在某些时候,这个逻辑打破了,不再适用了。
在第4个循环之后,被添加的数字变成随机的。
获得旋转周期数的正确方法是什么?
-------------------------- <</strong博士strong> TL;> --------------------------
function getRotationCycle (domElement, axis) {
var computedStyle = window.getComputedStyle(domElement),
matrixStyle = computedStyle.transform || computedStyle.WebkitTransform || computedStyle.MozTransform || computedStyle.msTransform || computedStyle.OTransform || computedStyle.KhtmlTransform;
if (!matrixStyle || matrixStyle.substr(0, 9) !== 'matrix3d(') {
//return 0; //????
throw "Something's wrong with 3D transform style. Probably no style applied at all OR unknown CSS3 vendor OR unknown/unsupported 3D matrix representation string OR CSS3 3D transform is not fully supported in this browser";
}
var matrix = WebKitCSSMatrix && (new WebKitCSSMatrix(matrixStyle)) ||
MozCSSMatrix && (new MozCSSMatrix(matrixStyle)) ||
MsCSSMatrix && (new MsCSSMatrix(matrixStyle)) ||
OCSSMatrix && (new OCSSMatrix(matrixStyle)) ||
CSSMatrix && (new CSSMatrix(matrixStyle)));
if (!matrix || isNaN(matrix.a) || isNaN(matrix.b) || isNaN(matrix.c)) {
//not sure about all versions of FireFox
throw "Could not catch CSSMatrix constructor for current browser, OR the constructor has returned a non-standard matrix object (need [a b c] numerical properties to work)";
}
var rotation;
// todo: giving an axis array is a good idea, or we could return all three rotations if no parameter given
switch (axis.toUpperCase()) {
case 'X':
rotation = Math.acos(matrix.a);
break;
case 'Y':
rotation = Math.asin(matrix.b);
break;
case 'Z':
throw "TODO: Sorry, Math people. I really need help here! Please implement this case for me. This will help you: http://9elements.com/html5demos/matrix3d/";
//rotation = Math.acos(matrix.a);
break;
default:
throw "This function accepts rotation axis name (string) as the first parameter. Possible values: 'X', 'Y', 'Z'";
}
//if (isNaN(rotation))...
rotation *= 180/Math.PI; //getting rotation in degrees. This is good for me for debug purposes but bad for performance, of course, ...
return rotation % 360; // ... so you can skip degrees and do it in radians only
}
更多的文档在这里:https://developer.mozilla.org,谷歌和微软似乎没什么有用的,苹果有一些。
一切都从这篇文章开始:css3转换matrix3d值。感谢作者提供的起点和数学转换。
但是给出的答案不完整,它只在WebKit中工作。
工作代码是用纯JavaScript编写的,因为:-它可以复制粘贴到任何环境-它的工作速度快一点(或很多,取决于)
如果你想要jQuery版本,使用这个代码片段来获取matrixStyle
字符串。
var matrixStyle = element$.css('transform')) || element$.css('-webkit-transform')) || element$.css('-moz-transform')) || element$.css('-ms-transform')) || element$.css('-o-transform')); // jQuery
矩阵的每一部分都在这里解释:http://9elements.com/html5demos/matrix3d/