Transform
Transform holds matrix data to make 2d rotate, translate, scale and skew transformations. Angles are in degrees.
This class represents a 2D affine transformation matrix used for positioning, rotating, scaling, and skewing visual objects. The matrix is stored in the following format:
| a | c | tx |
| b | d | ty |
| 0 | 0 | 1 |
Where:
aanddcontrol scaling and rotationbandccontrol skewing and rotationtxandtycontrol translation (position)
Example usage:
var transform = new Transform();
transform.translate(100, 50); // Move to (100, 50)
transform.rotate(Math.PI / 4); // Rotate 45 degrees
transform.scale(2, 2); // Double the size
// Apply to a point
var newX = transform.transformX(10, 20);
var newY = transform.transformY(10, 20);
The Transform class includes change tracking to optimize rendering pipelines and supports decomposition into individual components (position, scale, rotation, skew).
Instance Members
changedDirty: Boola: FloatThe (0,0) element of the matrix, affects horizontal scaling and rotation.
b: FloatThe (1,0) element of the matrix, affects vertical skewing and rotation.
c: FloatThe (0,1) element of the matrix, affects horizontal skewing and rotation.
d: FloatThe (1,1) element of the matrix, affects vertical scaling and rotation.
tx: FloatThe horizontal translation (x position) in pixels.
ty: FloatThe vertical translation (y position) in pixels.
changed: BoolWhether the transform has changed since the last change event. Call computeChanged() to update this value.
emitChange(): VoidEmitted when any transform property changes. Useful for invalidating caches or triggering re-renders.
computeChanged(): VoidCheck if the transform has changed by comparing current values to previous values.
Updates the changed property. Call this before checking changed to ensure
the value is up to date.
cleanChangedState(): VoidReset the change tracking state.
After calling this, changed will be false until the transform is modified again.
clone(): TransformCreate a copy of this transform.
| Returns | Description |
|---|---|
| Transform | A new Transform with the same matrix values |
Concatenate (multiply) this transform with another transform.
The result is stored in this transform. This is equivalent to:
this = this * m
| Name | Type | Description |
|---|---|---|
m |
Transform | The transform to concatenate with this one |
decompose(?output: DecomposedTransform): DecomposedTransformDecompose the transform matrix into its component parts. Extracts position, scale, rotation, and skew from the matrix.
| Name | Type | Default | Description |
|---|---|---|---|
output |
DecomposedTransform | (optional) | Optional DecomposedTransform to store the result (creates new if null) |
| Returns | Description |
|---|---|
| DecomposedTransform | The decomposed transform containing x, y, scaleX, scaleY, rotation, skewX, skewY |
setFromDecomposed(decomposed: DecomposedTransform): VoidSet this transform from decomposed values. Rebuilds the matrix from position, scale, rotation, and skew components.
| Name | Type | Description |
|---|---|---|
decomposed |
DecomposedTransform | The decomposed transform values to apply |
setFromValues(?x: Float = 0, ?y: Float = 0, ?scaleX: Float = 1, ?scaleY: Float = 1, ?rotation: Float = 0, ?skewX: Float = 0, ?skewY: Float = 0, ?pivotX: Float = 0, ?pivotY: Float = 0): VoidSet this transform from individual component values. Rebuilds the matrix from position, scale, rotation, skew, and pivot.
| Name | Type | Default | Description |
|---|---|---|---|
x |
Float | 0 |
X position |
y |
Float | 0 |
Y position |
scaleX |
Float | 1 |
Horizontal scale factor |
scaleY |
Float | 1 |
Vertical scale factor |
rotation |
Float | 0 |
Rotation angle in radians |
skewX |
Float | 0 |
Horizontal skew angle in radians |
skewY |
Float | 0 |
Vertical skew angle in radians |
pivotX |
Float | 0 |
X coordinate of the pivot point |
pivotY |
Float | 0 |
Y coordinate of the pivot point |
Set this transform by interpolating between two other transforms. Useful for animations and transitions.
| Name | Type | Description |
|---|---|---|
transform1 |
Transform | The starting transform (used when ratio = 0) |
transform2 |
Transform | The ending transform (used when ratio = 1) |
ratio |
Float | The interpolation factor (0 to 1) |
Transform a vector's X component without translation. Useful for transforming directions or deltas.
| Name | Type | Description |
|---|---|---|
x |
Float | The X component of the vector |
y |
Float | The Y component of the vector |
| Returns | Description |
|---|---|
| Float | The transformed X component |
Transform a vector's Y component without translation. Useful for transforming directions or deltas.
| Name | Type | Description |
|---|---|---|
x |
Float | The X component of the vector |
y |
Float | The Y component of the vector |
| Returns | Description |
|---|---|
| Float | The transformed Y component |
Check if this transform equals another transform. Compares all matrix elements for exact equality.
| Name | Type | Description |
|---|---|---|
transform |
Transform | The transform to compare with |
| Returns | Description |
|---|---|
| Bool | true if all matrix elements are equal |
identity(): VoidReset this transform to the identity matrix. The identity matrix has no translation, rotation, scale, or skew. Equivalent to: a=1, b=0, c=0, d=1, tx=0, ty=0
invert(): VoidInvert this transform matrix. The inverted matrix can be used to reverse transformations. If the matrix is not invertible (determinant = 0), sets to a degenerate state.
Rotate the transform by the specified angle. The rotation is applied relative to the current transform.
| Name | Type | Description |
|---|---|---|
angle |
Float | The rotation angle in radians (use degrees * Math.PI / 180 to convert) |
Scale the transform by the specified factors. The scaling is applied relative to the current transform.
| Name | Type | Description |
|---|---|---|
x |
Float | Horizontal scale factor |
y |
Float | Vertical scale factor |
Translate (move) the transform by the specified offset. The translation is applied relative to the current transform.
| Name | Type | Description |
|---|---|---|
x |
Float | Horizontal offset in pixels |
y |
Float | Vertical offset in pixels |
Apply skew transformation. Skewing shears the coordinate space along the X and Y axes.
| Name | Type | Description |
|---|---|---|
skewX |
Float | Horizontal skew angle in radians |
skewY |
Float | Vertical skew angle in radians |
Set the rotation and uniform scale directly. This replaces the current rotation and scale, discarding any skew.
| Name | Type | Default | Description |
|---|---|---|---|
angle |
Float | The rotation angle in radians | |
scale |
Float | 1 |
Uniform scale factor (default: 1) |
Set all matrix values directly.
| Name | Type | Description |
|---|---|---|
a |
Float | Horizontal scaling/rotation component |
b |
Float | Vertical skewing/rotation component |
c |
Float | Horizontal skewing/rotation component |
d |
Float | Vertical scaling/rotation component |
tx |
Float | Horizontal translation |
ty |
Float | Vertical translation |
Copy all values from another transform.
| Name | Type | Description |
|---|---|---|
transform |
Transform | The transform to copy from |
toString(): StringGet a string representation of the transform. Includes both matrix values and decomposed components.
| Returns | Description |
|---|---|
| String | String representation for debugging |
Transform a point's X coordinate. Applies the full transformation including translation.
| Name | Type | Description |
|---|---|---|
x |
Float | The X coordinate of the point |
y |
Float | The Y coordinate of the point |
| Returns | Description |
|---|---|
| Float | The transformed X coordinate |
Transform a point's Y coordinate. Applies the full transformation including translation.
| Name | Type | Description |
|---|---|---|
x |
Float | The X coordinate of the point |
y |
Float | The Y coordinate of the point |
| Returns | Description |
|---|---|
| Float | The transformed Y coordinate |
unbindEvents(): Voidnew(?a: Float = 1, ?b: Float = 0, ?c: Float = 0, ?d: Float = 1, ?tx: Float = 0, ?ty: Float = 0): VoidCreate a new Transform matrix.
| Name | Type | Default | Description |
|---|---|---|---|
a |
Float | 1 |
Horizontal scaling/rotation component (default: 1) |
b |
Float | 0 |
Vertical skewing/rotation component (default: 0) |
c |
Float | 0 |
Horizontal skewing/rotation component (default: 0) |
d |
Float | 1 |
Vertical scaling/rotation component (default: 1) |
tx |
Float | 0 |
Horizontal translation (default: 0) |
ty |
Float | 0 |
Vertical translation (default: 0) |
Private Members
didEmitChange(): VoidMetadata
| Name | Parameters |
|---|---|
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |
:allow |
ceramic.TransformPool |