# base/transformmatrix (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/transformmatrix/

2D affine transform matrix. (3x2 form compatible with Canvas2D)

## TransformMatrix extends Object

2D affine transform matrix. (3x2 form compatible with Canvas2D)
- | a c e |
  | b d f |
- Point transform: x' = a*x + c*y + e, y' = b*x + d*y + f

Example: Stacking translation and scale, then transforming a point

```js
import { TransformMatrix } from "./build/vanilla.js";

const transformMatrix = new TransformMatrix();
transformMatrix.translate(10, 20);
transformMatrix.scale(2, 2);
const point = transformMatrix.transformPoint(1, 1);
console.log(point.x, point.y);
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `get a` (property, since 0.3.0): Property that returns the a component.
  - returns number: The a component. (x scale and rotation part)
- `get b` (property, since 0.3.0): Property that returns the b component.
  - returns number: The b component. (y shear and rotation part)
- `get c` (property, since 0.3.0): Property that returns the c component.
  - returns number: The c component. (x shear and rotation part)
- `get d` (property, since 0.3.0): Property that returns the d component.
  - returns number: The d component. (y scale and rotation part)
- `get e` (property, since 0.3.0): Property that returns the e component.
  - returns number: The e component. (x translation)
- `get f` (property, since 0.3.0): Property that returns the f component.
  - returns number: The f component. (y translation)
- `setIdentity()` (method, since 0.3.0): Resets to the identity matrix.
- `setTransform(a, b, c, d, e, f)` (method, since 0.3.0): Sets the matrix components directly. (same argument order as Canvas2D setTransform)
  - `a` number: The a component of the matrix.
  - `b` number: The b component of the matrix.
  - `c` number: The c component of the matrix.
  - `d` number: The d component of the matrix.
  - `e` number: The e component of the matrix. (x translation)
  - `f` number: The f component of the matrix. (y translation)
- `copyFrom(other)` (method, since 0.3.0): Copies the components of another matrix.
  - `other` TransformMatrix: The matrix to copy the components from.
- `clone()` (method, since 0.3.0): Clone.
  - returns this: A new matrix with the same components.
- `translate(x, y)` (method, since 0.3.0): Accumulates a translation. (local-space right multiply, same as Canvas2D translate)
  - `x` number: Translation along x.
  - `y` number: Translation along y.
- `rotate(radian)` (method, since 0.3.0): Accumulates a rotation. (local-space right multiply, same as Canvas2D rotate)
  - `radian` number: Rotation angle. (radians)
- `scale(x, y)` (method, since 0.3.0): Accumulates a scale. (local-space right multiply, same as Canvas2D scale)
  - `x` number: Scale along x.
  - `y` number: Scale along y.
- `multiply(other)` (method, since 0.3.0): Accumulates by multiplying another matrix on the right. (this = this x other)
  - `other` TransformMatrix: The matrix multiplied on the right.
- `invert()` (method, since 0.4.0): Inverse matrix. (returns a new matrix and leaves this one as is, null if the determinant is 0)
  - returns TransformMatrix | null: The inverse matrix, or null when the determinant is 0.

  Example: Mapping a point back with the inverse matrix

  ```js
  import { TransformMatrix } from "./build/vanilla.js";
  
  const transformMatrix = new TransformMatrix();
  transformMatrix.translate(10, 20);
  const inverseMatrix = transformMatrix.invert();
  const point = inverseMatrix.transformPoint(10, 20);
  console.log(point.x, point.y);
  ```

- `transformPoint(x, y)` (method, since 0.3.0): Transforms a point.
  - `x` number: The x coordinate of the point to transform.
  - `y` number: The y coordinate of the point to transform.
  - returns { x: number, y: number }: The transformed point.
- `getMaximumScale()` (method, since 0.3.0): Returns the largest of the per-axis scales in the matrix. (for text sharpness)
  - returns number: The larger of the x-axis and y-axis scales.
- `writeToFloat32Array(target)` (method, since 0.3.0): Writes a column-major array for uploading a mat3 uniform.
  - `target` Float32Array: The array to write to, length 9 or more.
