getTransformationMatrix method

Matrix getTransformationMatrix (DisplayObject targetSpace)

The 2D transformation matrix relative to the given targetSpace.

Note: You can get the transformation matrix either with the getTransformationMatrix or the getTransformationMatrix3D method. You only need to use a 3D transformation matrix if you are working with 3D display objects.

Implementation

Matrix getTransformationMatrix(DisplayObject targetSpace) {
  if (targetSpace == null) return this.globalTransformationMatrix;
  if (targetSpace == this) return Matrix.fromIdentity();

  var ancestor = _getCommonAncestor(targetSpace);
  if (ancestor == null) return null;

  var resultMatrix = Matrix.fromIdentity();
  for (var obj = this; obj != ancestor; obj = obj.parent) {
    if (obj is DisplayObjectContainer3D) {
      throw StateError("Can't calculate 2D matrix for 3D display object.");
    }
    resultMatrix.concat(obj.transformationMatrix);
  }

  if (identical(targetSpace, ancestor)) return resultMatrix;

  var targetMatrix = Matrix.fromIdentity();
  for (var obj = targetSpace; obj != ancestor; obj = obj.parent) {
    if (obj is DisplayObjectContainer3D) {
      throw StateError("Can't calculate 2D matrix for 3D display object.");
    }
    targetMatrix.concat(obj.transformationMatrix);
  }

  targetMatrix.invert();
  resultMatrix.concat(targetMatrix);
  return resultMatrix;
}