The Code
Without further ado, here’s the solution:
private static function rotateImage(image:Image, degrees:Number):void { // Calculate rotation and offsets var radians:Number = degrees * (Math.PI / 180.0); var offsetWidth:Number = image.contentWidth/2.0; var offsetHeight:Number = image.contentHeight/2.0; // Perform rotation var matrix:Matrix = new Matrix(); matrix.translate(-offsetWidth, -offsetHeight); matrix.rotate(radians); matrix.translate(+offsetWidth, +offsetHeight); matrix.concat(image.transform.matrix); image.transform.matrix = matrix; }
Also, here’s a related function to perform a horizontal (left to right) flip of an image:
private static function flipImage(image:Image):void { // Calculate offset var offsetWidth:Number = image.contentWidth/2.0; var offsetHeight:Number = image.contentHeight/2.0; // Perform horizontal flip var matrix:Matrix = new Matrix(); matrix.translate(-offsetWidth, -offsetHeight); matrix.scale(-1, 1); // change to matrix.scale(1,-1) for vertical flip matrix.translate(+offsetWidth, +offsetHeight); matrix.concat(image.transform.matrix); image.transform.matrix = matrix; }
The Problem Explained
The problem, in a nutshell, is that Flex maintains the top left corner of the image for all transformation and ignores the center point. For example, just calling the rotate() method will produce rotations around the corner such as in this example. Many almost-solutions, see here and here, resolve this by first moving (translating) the image around its center by using the image’s vertical and horizontal midpoint.
A more complex issue arises, though, when the image is no longer positioned at its anchor, or in technical speak the image’s transformation tx/ty values are not 0. I found once you sufficiently drag/drop, rotate, or otherwise modify the image, all of the solutions tend to fall apart. In my situation, all the images were oriented with tx/ty referring to the anchor of the canvas, making these previous solutions difficult to rely upon.
The Solution Explained
After some fun manipulating matrices, I’ve written and tested a solution that will properly rotate an existing image around its center regardless of where it is positioned on the canvas or what transformations have been performed on it. It starts by applying rotation around the image’s center to the identity matrix. In this way, it simulates rotating the matrix around its midpoint translated position, but it is actually applied to the identity matrix. Once the rotational matrix has been calculated, the image is stuck at the top of the canvas. What we really need is a matrix that will take the rotational matrix we’ve just calculated and apply all the previous transformations to orient/rotate the image in the proper position. Well it so happens we have such a matrix, the image’s transformation matrix in fact! So, we apply the transformation matrix of the image to our calculated rotational matrix and voila! The result is our transformed image rotated by the specified amount around its center. We just set the image’s transformation matrix to this result and we are done.