从matrix3d获取CSS3旋转周期数

时间:2013-05-04 10:50:06

标签: jquery css3 rotation transform css-transitions

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

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

1 个答案:

答案 0 :(得分: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, 谷歌和微软似乎没有任何用处,Apple有some


一切都从这篇文章开始:css3 converting matrix3d values。感谢作者的起点和数学转换。

但是那里给出的答案并不完整,只适用于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/