The following example shows how you can rotate an image around an arbitrary X and Y origin using the <mx:Rotate/> tag in Flex.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/12/10/rotating-images-using-the-rotate-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
 
    <mx:Rotate id="rotate"
            target="{img}"
            angleFrom="0"
            angleTo="360"
            duration="1000"
            originX="{imgOriginX.value}"
            originY="{imgOriginY.value}" />
 
    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="originX:">
                <mx:NumericStepper id="imgOriginX"
                        minimum="0"
                        maximum="{img.width}"
                        value="0" />
            </mx:FormItem>
            <mx:FormItem label="originY:">
                <mx:NumericStepper id="imgOriginY"
                        minimum="0"
                        maximum="{img.height}"
                        value="0" />
            </mx:FormItem>
            <mx:FormItem>
                <mx:Button label="Rotate"
                        click="rotate.play();" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <mx:Image id="img" source="@Embed('flex_logo.jpg')" />
 
</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

10 Responses to Rotating images using the Rotate class

  1. lala says:

    i wante to do that when i rollOver why can i do that ??? please

  2. peterd says:

    lala,

    You can try setting the rollOverEffect style/effect:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            layout="vertical"
            verticalAlign="middle"
            backgroundColor="white">
    
        <mx:Rotate id="rotate"
                target="{img}"
                angleFrom="0"
                angleTo="360"
                duration="1000"
                originX="{img.height/2}"
                originY="{img.width/2}" />
    
        <mx:Image id="img"
                source="fx_appicon-tn.gif"
                rollOverEffect="{rotate}" />
    
    </mx:Application>
    

    Peter

  3. MechanisM says:

    How to set up rotate on rollOver in loop?
    I mean when mouse over image its still rotating untill mouse gone out?

    • Peter deHaan says:

      @MechanisM,

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
       
          <mx:Rotate id="rotatr"
                  angleFrom="0"
                  angleTo="360"
                  repeatCount="0" />
       
          <mx:Image id="logo"
                  source="flex_logo.jpg"
                  mouseOver="rotatr.play([event.currentTarget]);"
                  mouseOut="rotatr.stop();" />
       
      </mx:Application>

      Peter

  4. halflife_baby says:

    Hi,
    How to rotate an image to 90 degree when the button is clicked?
    If i click the button again, it should totate to 180 degree. (ie) agian it should rotate to 90 degree from the last position.
    I was working with this issue for the last 3 days. Can some one help me?

  5. Mafaz says:

    Hello there,
    The example is a nice one. How can I control the rotation of an image with a mouseDown event? Like I want control on the rotation of the image. What am I supposed to do. Any hints please?

  6. Allen says:

    Hi,

    The rotation feature is good. I need to control the rotation angle by an external feed. Would you have any sample code? Thank You

  7. Johnny Doe says:

    More importantly… How can I rotate an images rotateX, rotateY, and rotateZ? 3D rotation using Flex 3, without special libs or plugins… is it possible?

  8. Circass says:

    Hello,
    i am trying to rotate my image which is a child of my view panel.
    I wrote something like that :

    var rotate:mx.effects.Rotate = new mx.effects.Rotate();
    //var iTmp:Image = new Image();
    //iTmp = viewerPanel.getChildAt(0) as Image;
    rotate.target= viewerPanel.getChildAt(0);
    rotate.angleFrom=viewerPanel.getChildAt(0).rotation;
    rotate.angleTo=viewerPanel.getChildAt(0).rotation – 90;
    rotate.duration=1000;
    rotate.originX=viewerPanel.getChildAt(0).width/2;
    rotate.originY=viewerPanel.getChildAt(0).height/2;
    rotate.play();

    it rotates the panel from top left.

    then i wrote something like that :
    var iTmp:Image = new Image();
    iTmp = viewerPanel.getChildAt(0) as Image;
    var q:Number = 90 * Math.PI / 180 // 30 degrees in radians
    var m:Matrix = new Matrix();
    var centerX:Number = iTmp.width / 2;
    var centerY:Number = iTmp.height /2;
    m.translate(-1 * centerX, -1 * centerY);
    m.rotate(q);
    viewerPanel.removeChildAt(0);
    viewerPanel.addChild(iTmp);
    and this made the same thing.

    So How can i rotate an image which is added to a panel as a child ?
    Thank you.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree