# transformation-matrix Javascript isomorphic 2D affine transformations written in ES6 syntax. Manipulate transformation matrices with this totally tested library! [](https://standardjs.com) [](https://travis-ci.org/chrvadala/transformation-matrix) [](https://coveralls.io/github/chrvadala/transformation-matrix?branch=master) [](https://www.npmjs.com/package/transformation-matrix) [](https://www.npmjs.com/package/transformation-matrix) [](https://www.paypal.me/chrvadala/25) Transformations, i.e. *linear invertible automorphisms*, are used to map a picture into another one with different size, position and orientation. Given a basis, transformations are represented by means of squared invertible matrices, called **transformation matrices**. A geometric transformation is defined as a one-to-one mapping of a point space to itself, which preservers some geometric relations of figures. - [Geometric Programming for Computer Aided Design](https://books.google.it/books?vid=ISBN9780471899426) This library allows us to: - generate transformation matrices for the following operations: **translation**, **rotation**, **scale**, **shear**, **skew** - merge multiple transformation matrices in a single matrix that is the **composition of multiple matrices** - work with strings in both directions: **parse**, **render** - **apply a transformation matrix to point(s)** ## Usage example (ES6) ```js import {scale, rotate, translate, compose, applyToPoint} from 'transformation-matrix'; let {scale, rotate, translate, compose, applyToPoint} = window.TransformationMatrix; let {scale, rotate, translate, compose, applyToPoint} = require('transformation-matrix') let matrix = compose( translate(40,40), rotate(Math.PI/2), scale(2, 4) ); applyToPoint(matrix, {x: 42, y: 42}); // { x: -128, y: 124.00000000000001 } applyToPoint(matrix, [16, 24]); // [ -56, 72 ] ``` ## Setup ```sh npm install transformation-matrix # or yarn add transformation-matrix ``` ```html ``` ## Data Model A **Transformation Matrix** is defined as an `Object` with 6 keys: `a`, `b`, `c`, `d`, `e` and `f`. ```js const matrix = { a: 1, c: 0, e: 0, b: 0, d: 1, f: 0 } ``` A **Point** can be defined in two different ways: - as `Object`, with inside two keys: `x` and `y` ```js const point = { x: 24, y: 42 } ``` - as `Array`, with two items in the form `[x, y]` ```js const point = [ 24, 42 ] ``` ## Live Demo available at [http://chrvadala.github.io/transformation-matrix/](http://chrvadala.github.io/transformation-matrix/) # Reference ## Functions
Object | ArrayCalculate a point transformed with an affine matrix
arrayCalculate an array of points transformed with an affine matrix
ObjectExtract an affine matrix from an object that contains a,b,c,d,e,f keys Each value could be a float or a string that contains a float
ObjectParse a string matrix formatted as matrix(a,b,c,d,e,f)
ObjectParser for SVG Trasform Attribute http://www.w3.org/TR/SVG/coords.html#TransformAttribute
Warning: This should be considered BETA until it is released a stable version of pegjs.
ObjectReturns a matrix that transforms a triangle t1 into another triangle t2, or throws an exception if it is impossible.
ObjectIdentity matrix
ObjectCalculate a matrix that is the inverse of the provided matrix
booleanCheck if the object contain an affine matrix
ObjectCalculate a rotation matrix
ObjectCalculate a rotation matrix with a DEG angle
ObjectCalculate a scaling matrix
ObjectCalculate a shear matrix
ObjectCalculate a skew matrix
ObjectCalculate a skew matrix using DEG angles
ObjectRounds all elements of the given matrix using the given precision
stringSerialize the matrix to a string that can be used with CSS or SVG
stringSerialize the matrix to a string that can be used with CSS or SVG
stringSerialize the matrix to a string that can be used with CSS or SVG
ObjectMerge multiple matrices into one
ObjectMerge multiple matrices into one (alias of transform)
ObjectCalculate a translate matrix
Object \| Array
Calculate a point transformed with an affine matrix
**Kind**: global function
**Returns**: Object \| Array - Point
| Param | Description |
| --- | --- |
| matrix | Affine matrix |
| point | Point |
## applyToPoints(matrix, points) ⇒ array
Calculate an array of points transformed with an affine matrix
**Kind**: global function
**Returns**: array - Array of points
| Param | Description |
| --- | --- |
| matrix | Affine matrix |
| points | Array of points |
## fromObject(object) ⇒ Object
Extract an affine matrix from an object that contains a,b,c,d,e,f keys
Each value could be a float or a string that contains a float
**Kind**: global function
**Returns**: Object - }
| Param |
| --- |
| object |
## fromString(string) ⇒ Object
Parse a string matrix formatted as matrix(a,b,c,d,e,f)
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Description |
| --- | --- |
| string | String with a matrix |
## fromTransformAttribute(transformString) ⇒ Object
Parser for SVG Trasform Attribute http://www.w3.org/TR/SVG/coords.html#TransformAttribute Object - Parsed matrices
| Param | Description |
| --- | --- |
| transformString | string |
## fromTriangles(t1, t2) ⇒ Object
Returns a matrix that transforms a triangle t1 into another triangle t2, or throws an exception if it is impossible.
**Kind**: global function
**Returns**: Object - Affine matrix which transforms t1 to t2
**Throws**:
- Exception if the matrix becomes not invertible
| Param | Type | Description |
| --- | --- | --- |
| t1 | Array.<{x: number, y: number}> \| Array.<Array.<number>> | an array of points containing the three points for the first triangle |
| t2 | Array.<{x: number, y: number}> \| Array.<Array.<number>> | an array of points containing the three points for the second triangle |
## identity() ⇒ Object
Identity matrix
**Kind**: global function
**Returns**: Object - Affine matrix
## inverse(matrix) ⇒ Object
Calculate a matrix that is the inverse of the provided matrix
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Description |
| --- | --- |
| matrix | Affine matrix |
## isAffineMatrix(object) ⇒ boolean
Check if the object contain an affine matrix
**Kind**: global function
| Param |
| --- |
| object |
## rotate(angle, [cx], [cy]) ⇒ Object
Calculate a rotation matrix
**Kind**: global function
**Returns**: Object - Affine matrix *
| Param | Description |
| --- | --- |
| angle | Angle in radians |
| [cx] | If (cx,cy) are supplied the rotate is about this point |
| [cy] | If (cx,cy) are supplied the rotate is about this point |
## rotateDEG(angle, [cx], [cy]) ⇒ Object
Calculate a rotation matrix with a DEG angle
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Description |
| --- | --- |
| angle | Angle in degree |
| [cx] | If (cx,cy) are supplied the rotate is about this point |
| [cy] | If (cx,cy) are supplied the rotate is about this point |
## scale(sx, [sy]) ⇒ Object
Calculate a scaling matrix
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Default | Description |
| --- | --- | --- |
| sx | | Scaling on axis x |
| [sy] | sx | Scaling on axis y (default sx) |
## shear(shx, shy) ⇒ Object
Calculate a shear matrix
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Description |
| --- | --- |
| shx | Shear on axis x |
| shy | Shear on axis y |
## skew(ax, ay) ⇒ Object
Calculate a skew matrix
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Description |
| --- | --- |
| ax | Skew on axis x |
| ay | Skew on axis y |
## skewDEG(ax, ay) ⇒ Object
Calculate a skew matrix using DEG angles
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Description |
| --- | --- |
| ax | Skew on axis x |
| ay | Skew on axis y |
## smoothMatrix(m, [precision]) ⇒ Object
Rounds all elements of the given matrix using the given precision
**Kind**: global function
**Returns**: Object - the rounded matrix
| Param | Type | Description |
| --- | --- | --- |
| m | Object | a matrix to round |
| [precision] | | a precision to use for Math.round. Defaults to 10000000000 (meaning which rounds to the 10th digit after the comma). |
## toCSS(matrix) ⇒ string
Serialize the matrix to a string that can be used with CSS or SVG
**Kind**: global function
**Returns**: string - String that contains a matrix formatted as matrix(a,b,c,d,e,f)
| Param | Description |
| --- | --- |
| matrix | Affine matrix |
## toSVG(matrix) ⇒ string
Serialize the matrix to a string that can be used with CSS or SVG
**Kind**: global function
**Returns**: string - String that contains a matrix formatted as matrix(a,b,c,d,e,f)
| Param | Description |
| --- | --- |
| matrix | Affine matrix |
## toString(matrix) ⇒ string
Serialize the matrix to a string that can be used with CSS or SVG
**Kind**: global function
**Returns**: string - String that contains a matrix formatted as matrix(a,b,c,d,e,f)
| Param | Description |
| --- | --- |
| matrix | Affine matrix |
## transform(...matrices) ⇒ Object
Merge multiple matrices into one
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Type | Description |
| --- | --- | --- |
| ...matrices | object | list of matrices |
## compose(...matrices) ⇒ Object
Merge multiple matrices into one (alias of `transform`)
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Type | Description |
| --- | --- | --- |
| ...matrices | object | list of matrices |
## translate(tx, [ty]) ⇒ Object
Calculate a translate matrix
**Kind**: global function
**Returns**: Object - Affine matrix
| Param | Default | Description |
| --- | --- | --- |
| tx | | Translation on axis x |
| [ty] | 0 | Translation on axis y |