The DisplayObjectTransformer sample application shows a number of features of using the Matrix class to transform a display object, including the following:
The application provides an interface for adjusting the parameters of the matrix transformation, as follows:
When the user clicks the Transform button, the application applies the appropriate transformation.
To get the application files for this sample, see www.adobe.com/go/as3examples. The DisplayObjectTransformer application files can be found in the folder Samples/DisplayObjectTransformer. The application consists of the following files:
|
File |
Description |
|---|---|
|
DisplayObjectTransformer.mxml or DisplayObjectTransformer.fla |
The main application file in Flash (FLA) or Flex (MXML) |
|
com/example/programmingas3/geometry/MatrixTransformer.as |
A class that contains methods for applying matrix transformations. |
|
img/ |
A directory containing sample image files used by the application. |
The MatrixTransformer class includes static methods that apply geometric transformations of Matrix objects.
The transform() method includes parameters for each of the following:
The return value is the resulting matrix.
The transform() method calls the following static methods of the class:
Each returns the source matrix with the applied transformation.
The skew() method skews the matrix by adjusting the b and c properties of the matrix. An optional parameter, unit, determines the units used to define the skew angle, and if necessary, the method converts the angle value to radians:
if (unit == "degrees")
{
angle = Math.PI * 2 * angle / 360;
}
if (unit == "gradients")
{
angle = Math.PI * 2 * angle / 100;
}
A skewMatrix Matrix object is created and adjusted to apply the skew transformation. Initially, it is the identity matrix, as follows:
var skewMatrix:Matrix = new Matrix();
The skewSide parameter determines the side to which the skew is applied. If it is set to "right", the following code sets the b property of the matrix:
skewMatrix.b = Math.tan(angle);
Otherwise, the bottom side is skewed by adjusting the c property of the Matrix, as follows:
skewMatrix.c = Math.tan(angle);
The resulting skew is then applied to the existing matrix by concatenating the two matrixes, as the following example shows:
sourceMatrix.concat(skewMatrix); return sourceMatrix;
As the following example shows, the scale() method first adjusts the scale factor if it is provided as a percentage, and then uses the scale() method of the matrix object:
if (percent)
{
xScale = xScale / 100;
yScale = yScale / 100;
}
sourceMatrix.scale(xScale, yScale);
return sourceMatrix;
The translate() method simply applies the dx and dy translation factors by calling the translate() method of the matrix object, as follows:
sourceMatrix.translate(dx, dy); return sourceMatrix;
The rotate() method converts the input rotation factor to radians (if it is provided in degrees or gradients), and then calls the rotate() method of the matrix object:
if (unit == "degrees")
{
angle = Math.PI * 2 * angle / 360;
}
if (unit == "gradients")
{
angle = Math.PI * 2 * angle / 100;
}
sourceMatrix.rotate(angle);
return sourceMatrix;
The application contains a user interface for getting the transformation parameters from the user. It then passes these, along with the matrix property of the transform property of the display object, to the Matrix.transform() method, as follows:
tempMatrix = MatrixTransformer.transform(tempMatrix, xScaleSlider.value, yScaleSlider.value, dxSlider.value, dySlider.value, rotationSlider.value, skewSlider.value, skewSide );
The application then applies the return value to the matrix property of the transform property of the display object, thereby triggering the transformation:
img.content.transform.matrix = tempMatrix;