Is there js plugin convert the matrix parameter to css3 transform property?

后端 未结 4 2132
萌比男神i
萌比男神i 2021-01-05 09:27

Suppose I have css3 transform style:

img
{
   -webkit-transform:rotate(10deg) translate(100px,20px);
   -moz-transform:rotate(10deg) translate(100px,20px);
}         


        
4条回答
  •  自闭症患者
    2021-01-05 10:10

    You can decompose the numbers back into various components. There are two common techniques to do this called QR and LU for short.

    If you feed it the current matrix values you could do:

    function decompose(a, b, c, d, e, f, useLU) {
    
        var acos = Math.acos, // caching for readability below
            atan = Math.atan,
            sqrt = Math.sqrt,
            pi   = Math.PI,
    
            translate = {x: e, y: f},
            rotation  = 0,
            scale     = {x: 1, y: 1},
            skew      = {x: 0, y: 0},
    
            determ = a * d - b * c;   // get determinant
    
        if (useLU) {
            if (a) {
                skew =  {x: atan(c / a), y: atan(b / a)};
                scale = {x: a,           y: determ / a};
            }
            else if (b) {
                rotation = pi * 0.5;
                scale    = {x: b, y: determ / b};
                skew.x   = atan(d / b);
            }
            else { // a = b = 0
                scale  = {x: c, y: d};
                skew.x = pi * 0.25;
            }
        }
        else {
            // Apply the QR-like decomposition.
            if (a || b) {
                var r = sqrt(a*a + b*b);
                rotation = b > 0 ? acos(a / r) : -acos(a / r);
                scale    = {x: r, y: determ / r};
                skew.x   = atan((a*c + b*d) / (r*r));
            }
            else if (c || d) {
                var s = sqrt(c*c + d*d);
                rotation = pi * 0.5 - (d > 0 ? acos(-c / s) : -acos(c / s));
                scale    = {x: determ/s, y: s};
                skew.y   = atan((a*c + b*d) / (s*s));
            }
            else { // a = b = c = d = 0
                scale = {x:0, y:0};     // = invalid matrix
            }
        }
    
        return {
            scale    : scale,
            translate: translate,
            rotation : rotation,
            skew     : skew
        };
    }
    

    Now you can use the object and its values to set rotate, scale etc.

    See also my transformation-matrix-js and Decomposition of 2D transform-matrices for more details.

提交回复
热议问题