1.1.0 • Published 5 years ago

transformersjs v1.1.0

Weekly downloads
24
License
MIT
Repository
github
Last release
5 years ago

transformers

Lightweight zero-dependency transformation matrix utilities

NPM

Why

Perform transformation matrix calculation in a 2D plane. Use this library to:

  • Create a transformation matrix and manipulate it via translation, rotation, scale, shear, skew
  • Parse a transformation in string format, translate(10 20) rotate(30)
  • Obtain a point after applying transformation
  • Obtain matrix in string format to be used in CSS or SVG

Install

npm install transformersjs

Usage

Initialize a matrix

var transformers = require('transformersjs');

var mat = transformers();
//OR
var mat = transformers({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });

mat.matrix; // { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }

Parse transformation in string

var transformers = require('transformersjs');

var mat = transformers('translate(10, 15) rotate(30)');
//OR
var mat = transformers().translate(10, 15).rotate(30);

mat.matrix; //{ a: 1, b: 0, c: 0, d: 1, e: 10, f: 15 }

Obtain a point after applying transformations

var transformers = require('transformersjs');

var mat = transformers('translate(10, 15)');

mat.pointTo(8, 5); // { x: 18, y: 20 }

Convert matrix to string to be used in CSS or SVG

var transformers = require('transformersjs');

var mat = transformers('translate(10, 15)');

mat.render(); // matrix(1,0,0,1,10,15)

API

Objects

Typedefs

transformers : object

Initializer to create a matrix instance

Kind: global namespace

ParamTypeDescription
inputstring | object | arrayCan be a transformation in string, object, array notation

transformers.multiply(matrix) ⇒ transformers

Perform matrix multiplication

Kind: static method of transformers

ParamTypeDescription
matrixarray | Matrix | Transformersmatrix to be multiplied

Example

var transformers = require('transformersjs');
var mat = transformers();

mat.multiply({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
mat.multiply([1, 0, 0, 1, 0, 0]);
mat.multiply(mat); // { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }

transformers.parse(str) ⇒ transformers

Parse a valid string containing various transformations

Kind: static method of transformers

ParamType
strstring

Example

var transformers = require('transformersjs');
var mat = transformers();

mat.parse('translate(10,10)'); // {a: 1, b: 0, c: 0, d: 1, e: 10, f: 10}

transformers.translate(x, y) ⇒ transformers

Perform translation

Kind: static method of transformers

ParamTypeDefaultDescription
xnumber0translation along x-axis
ynumber0translation along y-axis

Example

var transformers = require('transformersjs');
var mat = transformers('translate(10, 10)');

mat.translate(); // {a: 1, b: 0, c: 0, d: 1, e: 10, f: 10}
mat.translate(5, 5); // {a: 1, b: 0, c: 0, d: 1, e: 15, f: 15}

transformers.rotate(angle, x, y) ⇒ transformers

Perform rotation

Kind: static method of transformers

ParamTypeDescription
anglenumberangle in degree
xnumberrotation along a point in x-axis
ynumberrotation along a point in y-axis

transformers.scale(x, y) ⇒ transformers

Perform scaling

Kind: static method of transformers

ParamTypeDefaultDescription
xnumberscaling along x-axis
ynumberxscaling along y-axis

Example

var transformers = require('transformersjs');
var mat = transformers('translate(10, 10)');

mat.scale(5); // {a: 5, b: 0, c: 0, d: 5, e: 10, f: 10}
mat.scale(5, 2); // {a: 5, b: 0, c: 0, d: 2, e: 10, f: 10}

transformers.shear(x, y) ⇒ transformers

Perform shear

Kind: static method of transformers

ParamTypeDescription
xnumbershear along x-axis
ynumbershear along y-axis

Example

var transformers = require('transformersjs');
var mat = transformers();

mat.shear(5,5); // {a: 1, b: 5, c: 5, d: 1, e: 0, f: 0}

transformers.skew(x, y) ⇒ transformers

Perform skew

Kind: static method of transformers

ParamTypeDescription
xnumberskew along x-axis
ynumberskew along y-axis

Example

var transformers = require('transformersjs');
var mat = transformers();

mat.skew(5,5); // {a: 1, b: -3.3805, c: -3.3805, d: 1, e: 0, f: 0}

transformers.inverse() ⇒ transformers

Inverse current matrix

Kind: static method of transformers
Example

var transformers = require('transformersjs');
var mat = transformers('translate(10, 10)');

mat.inverse(); // {a: 1, b: 0, c: 0, d: 1, e: -10, f: -10}

transformers.pointTo(x, y) ⇒ Object

Obtain a point after applying transformation

Kind: static method of transformers

ParamTypeDefault
xnumber0
ynumber0

Example

var transformers = require('transformersjs');
var mat = transformers('translate(10,10)');

mat.pointTo(); // {x: 10, y: 10}
mat.pointTo(10); // {x: 20, y: 10}

transformers.render() ⇒ string

Converts current matrix to string format to be used in CSS or SVG

Kind: static method of transformers
Example

var transformers = require('transformersjs');
var mat = transformers('translate(10,10)');

mat.render(); // "matrix(1,0,0,1,10,10)"

Matrix : object

Matrix formation

Kind: global typedef
Properties

NameTypeDefault
anumber1
bnumber0
cnumber0
dnumber1
enumber0
fnumber0
1.1.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago