Transform2D class

This class describes a generic 2D transform, which is a combination of translations, rotations, reflections and scaling. These transforms are combined into a single matrix, that can be either applied to a canvas, composed with another transform, or used directly to convert coordinates.

The transform can be visualized as 2 reference frames: a "global" and a "local". At first, these two reference frames coincide. Then, the following sequence of transforms is applied:

  • translation to point position;
  • rotation by angle radians clockwise;
  • scaling in X and Y directions by scale factors;
  • final translation by offset, in local coordinates.

The class is optimized for repeated use: the transform matrix is cached and then recalculated only when the underlying properties change. Moreover, recalculation of the transform is postponed until the matrix is actually requested by the user. Thus, modifying multiple properties at once does not incur the penalty of unnecessary recalculations.

This class implements the ChangeNotifier API, allowing you to subscribe for notifications whenever the transform matrix changes. In addition, you can subscribe to get notified when individual components of the transform change: position, scale, and offset (but not angle).

Inheritance

Constructors

Transform2D()
Transform2D.copy(Transform2D other)
factory

Properties

angle double
The rotation part of the transform. This represents rotation around the position point in clockwise direction by angle radians. If the angle is negative then the rotation is counterclockwise.
getter/setter pair
angleDegrees double
Similar to angle, but uses degrees instead of radians.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
hasReflection bool
Whether the transform includes a reflection, i.e. it flips the orientation of the coordinate system.
no setter
isAxisAligned bool
Whether the transform keeps horizontal (vertical) lines as horizontal (vertical).
no setter
isConformal bool
Whether the transform preserves angles. A conformal transformation may consist of a translation, rotation, and uniform scaling. A reflection is not considered conformal.
no setter
isTranslation bool
Whether the transform represents a pure translation, i.e. a transform of the form (x, y) -> (x + Δx, y + Δy).
no setter
offset NotifyingVector2
Additional offset applied after all other transforms. Unlike other transforms, this offset is applied in the local coordinate system. For example, an offset of (1, 0) describes a shift by 1 unit along the X axis, however, this shift is applied after that axis was repositioned, rotated and scaled.
getter/setter pair
position NotifyingVector2
The translation part of the transform. This translation is applied relative to the global coordinate space.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scale NotifyingVector2
The scale part of the transform. The default scale factor is (1, 1), a scale greater than 1 corresponds to expansion, and less than 1 is contraction. A negative scale is also allowed, and it corresponds to a mirror reflection around the corresponding axis. Scale factors can be different for X and Y directions.
getter/setter pair
transformMatrix Matrix4
The total transformation matrix for the component. This matrix combines translation, rotation, reflection and scale transforms into a single entity. The matrix is cached and gets recalculated only as necessary.
no setter
x double
X coordinate of the translation transform.
getter/setter pair
y double
Y coordinate of the translation transform.
getter/setter pair

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
clone() Transform2D
Clone of this.
closeTo(Transform2D other, {double tolerance = 1e-10}) bool
Check whether this transform is equal to other, up to the given tolerance. Setting tolerance to zero will check for exact equality. Transforms are considered equal if their rotation angles are the same or differ by a multiple of 2π, and if all other transform parameters: translation, scale, and offset are the same.
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
inherited
flipHorizontally() → void
Flip the coordinate system horizontally.
flipVertically() → void
Flip the coordinate system vertically.
globalToLocal(Vector2 point, {Vector2? output}) Vector2
Transform point from the global coordinate space into the local coordinates. Thus, this method performs the inverse of the current transform.
localToGlobal(Vector2 point, {Vector2? output}) Vector2
Transform point from local coordinates into the parent coordinate space. Effectively, this function applies the current transform to point.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
setFrom(Transform2D other) → void
Set this to the values of the other Transform2D.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited