简体   繁体   English

JavaScript从常规CSS获取矩阵转换并将matrix2D转换为matrix3D

[英]JavaScript get matrix transform from regular CSS and convert matrix2D to matrix3D

I need to get the matrix transform from a regular CSS transform like this one: rotateX(10deg) rotateZ(10deg) . 我需要从像这样的常规CSS变换获取矩阵变换: rotateX(10deg) rotateZ(10deg)

I know there is an existring solution for WebKit (WebKitCSSMatrix) but there is nothing like that for Firefox or IE... 我知道有一个针对WebKit(WebKitCSSMatrix)的存在解决方案,但没有针对Firefox或IE的解决方案...

So, I tried to get the matrix by setting the transform to an invisible (not in the DOM) and getting it with getComputedStyle but this method return nothing if the element is hidden or detached from the document. 因此,我试图通过将转换设置为不可见(而不是在DOM中)并使用getComputedStyle获取它来获取矩阵,但是如果元素是隐藏的或与文档分离,则此方法不返回任何内容。

Is there a good tutorial to convert values to Matrix ? 有很好的教程将值转换为Matrix吗?

Then, how to convert a 2D matrix to a 3D ? 然后,如何将2D矩阵转换为3D? from 6 to 16 values... 从6到16个值...

you can calculate the matrix yourself. 您可以自己计算矩阵。 this pages explains how to describe rotations as a 2D matrix : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Matrix.html 本页介绍了如何将旋转描述为2D矩阵http : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/geom/Matrix.html

for a rotation this would be: 轮换是:

在此处输入图片说明

the matrix values can be accessed through these properties. 可以通过这些属性访问矩阵值。 the last row does not need to be modified for 2d. 最后一行无需修改2d。 to initialize a matrix in css use: matrix(a,b,c,d,e,f) 在CSS中初始化矩阵,请使用: matrix(a,b,c,d,e,f)

基质性质

for 3D matrices you can find a nice introduction here: http://www.eleqtriq.com/2010/05/understanding-css-3d-transforms/ 对于3D矩阵,您可以在此处找到不错的介绍: http : //www.eleqtriq.com/2010/05/understanding-css-3d-transforms/

than read about how to set up a matrix manually: http://www.eleqtriq.com/2010/05/css-3d-matrix-transformations/ 而不是阅读有关如何手动设置矩阵的信息: http//www.eleqtriq.com/2010/05/css-3d-matrix-transformations/

in both cases you will have to multiply several matrices if you want apply more than one transformation. 在这两种情况下,如果要应用多个变换,都必须将多个矩阵相乘。 information about how to do this can be found here: http://www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/ 有关如何执行此操作的信息,请参见以下网址http : //www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/


NOTE: 注意:
getComputedStyle method only work's for elements which are appended to your document getComputedStyle方法仅适用于附加到文档中的元素


let's start with a function that make's a hidden element and append's it into your document : 让我们从一个使它成为隐藏元素的函数开始,然后将其附加到您的文档中:

var make_test_el = 
    function () {
        var el = document.createElement("div");

        // some browsers doesn't add transform styles if display is inline
        el.style.display = "block";

        // to be sure your element won't be shown 
        el.style.width = "0px";
        el.style.height = "0px";
        el.style.margin = "0px";
        el.style.padding = "0px";
        el.style.overflow = "hidden";

        document.body.appendChild(el);
        return el;
    }

then just use it by the following code: 然后只需通过以下代码使用它:

var value = 'rotateX(10deg) rotateZ(10deg)'; // your readable value for transformation

var elem = make_test_el();

elem.style.transform = value;

// take your result and alert it 
var result = window.getComputedStyle(elem,null).transform;
alert(result);

//then remove appended element
elem.parentNode.removeChild(elem);


demo : http://jsfiddle.net/hbh7ypc9/1/ 演示: http : //jsfiddle.net/hbh7ypc9/1/

the result that you'll see maybe matrix3d(...) or matrix(...) 您可能会看到的结果matrix3d(...)matrix(...)
It depend's on browser's RenderEngine and the given value 它取决于浏览器的RenderEngine给定的值
for example: 例如:
IE10 will always give you matrix3d(...) IE10将始终为您提供matrix3d(...)
but for chrome and opera depend's on given value 但是对于ChromeOpera来说,取决于给定的值

but I've no idea about converting a 2D matrix to a 3D one 但我不知道将2D矩阵转换为3D矩阵的想法
that's a question I've too 我也是这个问题
actually matrix values are really hard to understand 实际上矩阵值真的很难理解
good luck... 祝好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM