最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - canvas:how to complete translate,skew,rotate...in just one transform statement? - Stack Overflow

programmeradmin4浏览0评论

I studying 'transform' in recent days,and now i know how to do translate,rotate,skew,scale by transform's matirx. but if i want to do all actions above in one transform statement, how can i do?

ctx.transform(a,b,c,d,e,f);

when we want rotate something by transform, we must post 4 arguments for each one (a,b,c,d), So ,if i want rotate and scale, for example, rotate 30 deg and scale (1.5,2), can the transform done them at the same time? so what the values of (a,b,c,d)? and how calculate them?

and another question: Is there an order in transform? I mean if i use transform to rotate and scale and translate, what's the order between them? after all, the order is very important, 'translate first,scale next' with 'scale first,translate next', will get different results.

I studying 'transform' in recent days,and now i know how to do translate,rotate,skew,scale by transform's matirx. but if i want to do all actions above in one transform statement, how can i do?

ctx.transform(a,b,c,d,e,f);

when we want rotate something by transform, we must post 4 arguments for each one (a,b,c,d), So ,if i want rotate and scale, for example, rotate 30 deg and scale (1.5,2), can the transform done them at the same time? so what the values of (a,b,c,d)? and how calculate them?

and another question: Is there an order in transform? I mean if i use transform to rotate and scale and translate, what's the order between them? after all, the order is very important, 'translate first,scale next' with 'scale first,translate next', will get different results.

Share Improve this question edited Aug 26, 2013 at 7:07 LIXer asked Aug 26, 2013 at 5:11 LIXerLIXer 3621 gold badge4 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

This is the math done by context.transform(a,b,c,d,e,f)

When you use a single context.transform to do multiple operations (translate+scale+rotate)

  • The translate is done first.
  • The scale and rotate are done next (order of these does not matter).

This is the matrix math in javascript form:

    // a=0, b=1, c=2, d=3, e=4, f=5

    // declare an array that will hold our transform math
    // this is the identity matrix (where no transforms exist)
    var matrix=[1,0,0,1,0,0];

    // for example, 

    // rotate 30 degrees clockwise from 0 degrees
    // note: 0 degrees extends rightward horizontally from the origin
    rotate(30*Math.PI/180);

    // scale by 1.5 in X and 2.0 in Y scales
    scale(1.5,2,0);

    // plug our transform array into the context
    context.transform(matrix[0],matrix[1],matrix[2],matrix[3],matrix[4],matrix[5]);



    // do the translate to the array 
    function translate(x,y){
        matrix[4] += matrix[0] * x + matrix[2] * y;
        matrix[5] += matrix[1] * x + matrix[3] * y;
    }

    // do the scale to the array
    function scale(x,y){
        matrix[0] *= x;
        matrix[1] *= x;
        matrix[2] *= y;
        matrix[3] *= y;    
    }

    // do the rotate to the array
    function rotate(radians){
        var cos = Math.cos(radians);
        var sin = Math.sin(radians);
        var m11 = matrix[0] * cos + matrix[2] * sin;
        var m12 = matrix[1] * cos + matrix[3] * sin;
        var m21 = -matrix[0] * sin + matrix[2] * cos;
        var m22 = -matrix[1] * sin + matrix[3] * cos;
        matrix[0] = m11;
        matrix[1] = m12;
        matrix[2] = m21;
        matrix[3] = m22;   
    }

There's all kinds of flexibility here that might be what you're looking for. See for example:

CSS3 Multiple transforms

How to apply multiple transforms in CSS?

then see here:

http://www.quora./Is-there-any-way-to-make-sequential-CSS-transition-without-JavaScript

Every transform function is described mathematically using a 4*4 matrix, To perform more than 1 function say, rotate and scale, then you need to multiply the 4*4 matrices created for each of them,

For details on Mathematical Description of Transform Functions please refer < http://www.w3/TR/css3-transforms/#Scale3dDefined>

For details on how to apply multiple transforms please refer Matrices, vectors, and matrix3d() section in http://dev.opera./articles/view/understanding-3d-transforms/

There is an order for your transforms based on the order in which you give each function doesnt hold any precedence, say scale(10) translate(200) would be different from ttranslate(200)scale(10),thats obvious as Matrix multiplication is NOT mutative in general AB ≠ BA

发布评论

评论列表(0)

  1. 暂无评论