2017-02-21 00:38:22 +00:00
|
|
|
var mathCos = Math.cos;
|
|
|
|
var mathSin = Math.sin;
|
|
|
|
var mathSqrt = Math.sqrt;
|
|
|
|
var mathAcos = Math.acos;
|
|
|
|
var mathAtan = Math.atan;
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
var TransformMatrix = function (a, b, c, d, tx, ty)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
a = typeof a === 'number' ? a : 1;
|
|
|
|
b = typeof b === 'number' ? b : 0;
|
|
|
|
c = typeof c === 'number' ? c : 0;
|
|
|
|
d = typeof d === 'number' ? d : 1;
|
|
|
|
tx = typeof tx === 'number' ? tx : 0;
|
|
|
|
ty = typeof ty === 'number' ? ty : 0;
|
|
|
|
|
2017-03-06 18:13:39 +00:00
|
|
|
this.matrix = new Float32Array([a, b, c, d, tx, ty, 0, 0, 1]);
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
this.decomposedMatrix = {
|
|
|
|
translateX: 0,
|
|
|
|
translateY: 0,
|
|
|
|
scaleX: 1,
|
|
|
|
scaleY: 1,
|
|
|
|
rotation: 0
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.loadIdentity = function ()
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
|
|
|
|
|
|
|
matrix[0] = 1;
|
|
|
|
matrix[1] = 0;
|
|
|
|
matrix[2] = 0;
|
|
|
|
matrix[3] = 1;
|
|
|
|
matrix[4] = 0;
|
|
|
|
matrix[5] = 0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.translate = function (x, y)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
|
|
|
|
|
|
|
matrix[4] = matrix[0] * x + matrix[2] * y + matrix[4];
|
|
|
|
matrix[5] = matrix[1] * x + matrix[3] * y + matrix[5];
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.scale = function (x, y)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
|
|
|
|
|
|
|
matrix[0] = matrix[0] * x;
|
|
|
|
matrix[1] = matrix[1] * x;
|
|
|
|
matrix[2] = matrix[2] * y;
|
|
|
|
matrix[3] = matrix[3] * y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.rotate = function (radian)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
2017-03-06 18:13:39 +00:00
|
|
|
var radianSin = mathSin(radian);
|
|
|
|
var radianCos = mathCos(radian);
|
2017-02-21 00:38:22 +00:00
|
|
|
|
2017-03-06 18:13:39 +00:00
|
|
|
return this.transform(radianCos, -radianSin, radianSin, radianCos, 0, 0);
|
2017-02-21 00:38:22 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.multiply = function (otherMatrix)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var a0 = matrix[0];
|
|
|
|
var b0 = matrix[1];
|
|
|
|
var c0 = matrix[2];
|
|
|
|
var d0 = matrix[3];
|
|
|
|
var tx0 = matrix[4];
|
|
|
|
var ty0 = matrix[5];
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var a1 = otherMatrix[0];
|
|
|
|
var b1 = otherMatrix[1];
|
|
|
|
var c1 = otherMatrix[2];
|
|
|
|
var d1 = otherMatrix[3];
|
|
|
|
var tx1 = otherMatrix[4];
|
|
|
|
var ty1 = otherMatrix[5];
|
|
|
|
|
|
|
|
matrix[0] = a1 * a0 + b1 * c0;
|
|
|
|
matrix[1] = a1 * b0 + b1 * d0;
|
|
|
|
matrix[2] = c1 * a0 + d1 * c0;
|
|
|
|
matrix[3] = c1 * b0 + d1 * d0;
|
|
|
|
matrix[4] = tx1 * a0 + ty1 * c0 + tx0;
|
|
|
|
matrix[5] = tx1 * b0 + ty1 * d0 + ty0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.transform = function (a, b, c, d, tx, ty)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var a0 = matrix[0];
|
|
|
|
var b0 = matrix[1];
|
|
|
|
var c0 = matrix[2];
|
|
|
|
var d0 = matrix[3];
|
|
|
|
var tx0 = matrix[4];
|
|
|
|
var ty0 = matrix[5];
|
|
|
|
|
|
|
|
matrix[0] = a * a0 + b * c0;
|
|
|
|
matrix[1] = a * b0 + b * d0;
|
|
|
|
matrix[2] = c * a0 + d * c0;
|
|
|
|
matrix[3] = c * b0 + d * d0;
|
|
|
|
matrix[4] = tx * a0 + ty * c0 + tx0;
|
|
|
|
matrix[5] = tx * b0 + ty * d0 + ty0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-06-16 10:11:28 +00:00
|
|
|
TransformMatrix.prototype.transformPoint = function (x, y, point)
|
|
|
|
{
|
|
|
|
if (point === undefined) { point = { x: 0, y: 0 }; }
|
|
|
|
|
|
|
|
var matrix = this.matrix;
|
|
|
|
|
|
|
|
var a = matrix[0];
|
|
|
|
var b = matrix[1];
|
|
|
|
var c = matrix[2];
|
|
|
|
var d = matrix[3];
|
|
|
|
var tx = matrix[4];
|
|
|
|
var ty = matrix[5];
|
|
|
|
|
2017-06-20 23:26:21 +00:00
|
|
|
point.x = x * a + y * c + tx;
|
|
|
|
point.y = x * b + y * d + ty;
|
2017-06-16 10:11:28 +00:00
|
|
|
|
|
|
|
return point;
|
|
|
|
};
|
|
|
|
|
|
|
|
TransformMatrix.prototype.invert = function ()
|
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
|
|
|
|
2017-06-20 23:26:21 +00:00
|
|
|
var a = matrix[0];
|
|
|
|
var b = matrix[1];
|
|
|
|
var c = matrix[2];
|
|
|
|
var d = matrix[3];
|
|
|
|
var tx = matrix[4];
|
|
|
|
var ty = matrix[5];
|
|
|
|
|
|
|
|
var n = a * d - b * c;
|
|
|
|
|
|
|
|
matrix[0] = d / n;
|
|
|
|
matrix[1] = -b / n;
|
|
|
|
matrix[2] = -c / n;
|
|
|
|
matrix[3] = a / n;
|
|
|
|
matrix[4] = (c * ty - d * tx) / n;
|
|
|
|
matrix[5] = -(a * ty - b * tx) / n;
|
2017-06-16 10:11:28 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.setTransform = function (a, b, c, d, tx, ty)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var matrix = this.matrix;
|
|
|
|
|
|
|
|
matrix[0] = a;
|
|
|
|
matrix[1] = b;
|
|
|
|
matrix[2] = c;
|
|
|
|
matrix[3] = d;
|
|
|
|
matrix[4] = tx;
|
|
|
|
matrix[5] = ty;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
TransformMatrix.prototype.decomposeMatrix = function ()
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
|
|
|
var decomposedMatrix = this.decomposedMatrix;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var matrix = this.matrix;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var a = matrix[0];
|
|
|
|
var b = matrix[1];
|
|
|
|
var c = matrix[2];
|
|
|
|
var d = matrix[3];
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var a2 = a * a;
|
|
|
|
var b2 = b * b;
|
|
|
|
var c2 = c * c;
|
|
|
|
var d2 = d * d;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
var sx = mathSqrt(a2 + c2);
|
|
|
|
var sy = mathSqrt(b2 + d2);
|
|
|
|
|
|
|
|
decomposedMatrix.translateX = matrix[4];
|
|
|
|
decomposedMatrix.translateY = matrix[5];
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
decomposedMatrix.scaleX = sx;
|
|
|
|
decomposedMatrix.scaleY = sy;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
decomposedMatrix.rotation = mathAcos(a / sx) * (mathAtan(-c / a) < 0 ? -1 : 1);
|
|
|
|
|
|
|
|
return decomposedMatrix;
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
/* identity + translate + rotate + scale */
|
2017-06-20 23:26:21 +00:00
|
|
|
TransformMatrix.prototype.applyITRS = function (x, y, rotation, scaleX, scaleY)
|
2017-02-21 00:38:22 +00:00
|
|
|
{
|
2017-02-22 16:44:14 +00:00
|
|
|
var matrix = this.matrix;
|
2017-06-20 23:26:21 +00:00
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
var a = 1;
|
|
|
|
var b = 0;
|
|
|
|
var c = 0;
|
|
|
|
var d = 1;
|
2017-06-20 23:26:21 +00:00
|
|
|
var tx = 0;
|
|
|
|
var ty = 0;
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
var sr = mathSin(rotation);
|
|
|
|
var cr = mathCos(rotation);
|
|
|
|
|
|
|
|
// Translate
|
2017-06-20 23:26:21 +00:00
|
|
|
matrix[4] = a * x + c * y + tx;
|
|
|
|
matrix[5] = b * x + d * y + ty;
|
2017-02-22 16:44:14 +00:00
|
|
|
|
|
|
|
// Rotate
|
2017-03-06 18:13:39 +00:00
|
|
|
matrix[0] = cr * a + -sr * c;
|
|
|
|
matrix[1] = cr * b + -sr * d;
|
|
|
|
matrix[2] = sr * a + cr * c;
|
|
|
|
matrix[3] = sr * b + cr * d;
|
2017-02-21 00:38:22 +00:00
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
// Scale
|
|
|
|
matrix[0] = matrix[0] * scaleX;
|
|
|
|
matrix[1] = matrix[1] * scaleX;
|
|
|
|
matrix[2] = matrix[2] * scaleY;
|
|
|
|
matrix[3] = matrix[3] * scaleY;
|
2017-02-21 00:38:22 +00:00
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
return this;
|
2017-02-21 00:38:22 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 16:44:14 +00:00
|
|
|
module.exports = TransformMatrix;
|