Flash Flex Rotate Around a Point by Matrix

On April 28, 2011, in Flash Flex AIR, by James Liu

The Flex provides a lots of functions about Matrix Transformation functions. Using these matrix functions, we can implements 2D transformation in a very easy way. For 2D transformation, the most common transformation is Translate, Scale, Rotate, and Skew. By default, all these transformations are according to the Top-Left point, (0, 0) point. For example, when we set a rotation as 45 in a canvas, the canvas will rotate 90 degrees around (0,0), as figure 1 shown. The Scale and Skew also use the same rule to transformation. But lots of times, we want to rotate a canvas, movieclip, or an image, around a certain point, for example, rotate it around center point, as figure 2 shown. In this post, I will show you how to make a rotation around a point.

Default Rotation ExampleRotate Around Center Example

We need to decide a rotation point first. In my example, I will use the center point as the rotation point. Here, we need to understand that the coordinate of the rotation point should be basing on the rotation object. That means if the rotation object width and height are 100×100, the point coordinate will be (50, 50), the center point. Then we follow the 4 steps:

  1. Get the new point basing on the current matrix;
  2. Translate the object to the new point;
  3. Rotate the object
  4. Translate the object back from the new point;

In this way, we can rotate the object around the point, regardless the object has already other matrix transforms. Below is the code example:

var aroundPoint:Point = resetCenterPoint(displayObj, displayObj.width/2, displayObj.height/2);
(displayObj as DisplayObject).addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void
rotate(displayObj, 45, aroundPoint.x, aroundPoint.y);

function resetCenterPoint(obj:DisplayObject, orgX:Number, orgY:Number):Point
var m:Matrix = obj.transform.matrix;

var orgXY:Point = m.deltaTransformPoint(new Point(orgX, orgY));
orgXY.x += m.tx;
orgXY.y += m.ty;
return orgXY;

function rotate(obj:DisplayObject, angle:Number, aroundX:Number, aroundY:Number):void
var m:Matrix = obj.transform.matrix;

//var orgXY:Point = resetCenterPoint(obj, obj.width/2, obj.height/2);

var cm:Matrix = new Matrix(1, 0, 0, 1, -aroundX, -aroundY);

m.rotate(Math.PI * angle / 180);

var rcm:Matrix = new Matrix(1, 0, 0, 1, aroundX, aroundY);

obj.transform.matrix = m;

Download Source Code

Tagged with:  

10 Responses to “Flash Flex Rotate Around a Point by Matrix”

  1. Duncan says:

    In the rotate function you don’t need to create new matrices for the translations.

    Calling the translate function should be the same as creating an identity matrix, applying the translate, then concatenating this matrix with your object matrix.

    var m:Matrix = obj.transform.matrix;
    m.translate( -aroundX, -aroundY );
    m.rotate(Math.PI * angle / 180);
    m.translate( aroundX, aroundY );
    obj.transform.matrix = m;

  2. Loradae says:

    That’s way more cvleer than I was expecting. Thanks!

  3. Tish says:

    I was lokiong everywhere and this popped up like nothing!

  4. Dubey says:


    Thanks a lot for the code….however, when I try the rotation by 1 degree….the square’s position is changed with every 360 rotation. I tried 1 degree because i want to have angle determined by mouse drag on the square which would vary from low to high values.

    Please let me know if this problem can be worked around.


  5. Amit Tamse says:

    Thanks for posting! Good for learning.

Leave a Reply

Free WordPress Themes