首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何逆向工程webkit matrix3d转换

如何逆向工程webkit matrix3d转换
EN

Stack Overflow用户
提问于 2012-05-15 00:42:57
回答 3查看 3.5K关注 0票数 6

我有一个立方体元素被旋转并在一个3d空间中被转换。

我只想在给定的时间找到这个转换的rotateY部分。这个是可能的吗?

我有:

代码语言:javascript
复制
var cube = document.getElementById("cube");
var transform = getComputedStyle(cube).webkitTransform;

//the value of transform is:
// matrix3d(-1, 0, 0.00000000000000012246467991473532, 0, 
//           0, 1, 0, 0, 
//          -0.00000000000000012246467991473532, 0, -1, 0, 
//           0, 0, 0, 1)

帮助?:-)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-15 15:07:29

如果你的物体只是绕Y旋转,那么是的,旋转很容易计算-,它是矩阵元素1,1的arccos或3,3,或者是-(元素3,1)或元素1,3的弧素。在你的具体例子中,它大约是180度。如果旋转不仅仅是y轴,那么你必须开始跟踪以前的旋转,它会变得更加混乱。

您的转换将只是输出矩阵的下一行,在本例中为0,0,0。

参见http://www.inversereality.org/tutorials/graphics%20programming/3dwmatrices.html上的示例

票数 4
EN

Stack Overflow用户

发布于 2012-07-02 09:03:32

您可以使用“新WebKitCSSMatrix”函数检索转换信息。例如:

代码语言:javascript
复制
var matrix = new WebKitCSSMatrix($('#element').css('-webkit-transform'));
var translateZ = matrix.m43;

矩阵的每一部分都在这里解释:http://9elements.com/html5demos/matrix3d/ --您可以检索更复杂的属性,如rotationX,对于具有数学混乱的3d矩阵:

代码语言:javascript
复制
var rotationX = Math.acos(matrix.a) * (180/Math.PI);
var rotationY = Math.asin(matrix.b) * (180/Math.PI);
票数 4
EN

Stack Overflow用户

发布于 2013-09-06 12:38:56

给定函数确定给定DOM元素cross-browser.的rotation*transform*scale

如果你想要更多的东西,或者在数学中发现错误,这是一个关于math.stackexchange.com的讨论

http://caniuse.com/transforms3d中列出的浏览器上进行测试

代码语言:javascript
复制
var reverseEngineerTransform3d = function (domElement, parameterName) {

        parameterName = parameterName.toLowerCase();

        var computedStyle = window.getComputedStyle(domElement),
            matrixStyle = computedStyle.transform || computedStyle.WebkitTransform || computedStyle.MozTransform || computedStyle.msTransform || computedStyle.OTransform;

        if (matrixStyle) {
            matrixStyle = matrixStyle.trim();
        }

        if (matrixStyle === 'none') {
            if (parameterName.indexOf('rotate') === 0) {
                return '0deg';
            } else if (parameterName.indexOf('translate') === 0) {
                return '0px';
            } else if (parameterName === 'scale') {
                return 1;
            } else {
                throw "Unsupported 3D parameter " + parameterName;
            }
        }

        else if (!matrixStyle || matrixStyle.substr(0, 9) !== 'matrix3d(') {
            //return something or die ?????
            throw "Something is 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 = window.WebKitCSSMatrix && (new WebKitCSSMatrix(matrixStyle)) ||
                window.MozCSSMatrix && (new MozCSSMatrix(matrixStyle)) ||
                window.MsCSSMatrix && (new MsCSSMatrix(matrixStyle)) ||
                window.OCSSMatrix && (new OCSSMatrix(matrixStyle)) ||
                window.CSSMatrix && (new CSSMatrix(matrixStyle)); // sorry 4 copy-paste my friends

        if (!matrix || isNaN(matrix.a) || isNaN(matrix.b) || isNaN(matrix.c) || isNaN(matrix.m41) || isNaN(matrix.m42) || isNaN(matrix.m43) || isNaN(matrix.m11)) {
            throw "Could not catch CSSMatrix constructor for current browser, OR the constructor has returned a non-standard matrix object (need .a, .b, .c and mXX numerical properties to work)";
        }


        // todo: giving a parameters array (and returning an array) is a good idea, or we could return everything if no parameters given

        switch (parameterName) {

            case 'rotatey':  // in degrees
                return Math.acos(matrix.m11)*180/Math.PI * (matrix.m13 > 0 ? -1 : 1) + 'deg';

            case 'rotatex':  // in degrees
                return Math.asin(matrix.m22)*180/Math.PI + 'deg';

            case 'rotatez':  // in degrees
                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/";

            case 'translatex': // in pixels
                return matrix.m41 + 'px';

            case 'translatey': // in pixels
                return matrix.m42 + 'px';

            case 'translatez': // in pixels
                return matrix.m43 + 'px';

            case 'scale': // no units
                if (matrix.m11 === matrix.m22 && matrix.m22 === matrix.m33) {
                    return matrix.m11;
                } else {
                    throw "I'm not so smart to calculate scale from this matrix";
                }

           // todo: anything else? skew ????

            default:
                throw "This function accepts 3d-parameter name (case-insensitive string) as the second argument. Currently supported: 'rotate[xyz]', 'translate[xyz]', 'scale'. This parameter is unsupported: " + parameterName;
        }
    };
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10592823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档