2

我正在尝试使用 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 个循环之后,添加的数字变为随机数。

获得旋转周期数的正确方法是什么?

4

1 回答 1

0

-------------------------- TL;DR --------- -----

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,Google和 Microsoft 似乎没有什么用处,Apple 有一些.


一切都从这篇文章开始: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/

于 2013-09-06T12:31:47.677 回答