forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformMatrix.js
More file actions
186 lines (155 loc) · 4.23 KB
/
Copy pathTransformMatrix.js
File metadata and controls
186 lines (155 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var mathCos = Math.cos;
var mathSin = Math.sin;
var mathSqrt = Math.sqrt;
var mathAcos = Math.acos;
var mathAtan = Math.atan;
var TransformMatrix = function (a, b, c, d, tx, ty)
{
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;
this.matrix = new Float32Array([a, b, c, d, tx, ty, 0, 0, 1]);
this.decomposedMatrix = {
translateX: 0,
translateY: 0,
scaleX: 1,
scaleY: 1,
rotation: 0
};
};
TransformMatrix.prototype.loadIdentity = function ()
{
var matrix = this.matrix;
matrix[0] = 1;
matrix[1] = 0;
matrix[2] = 0;
matrix[3] = 1;
matrix[4] = 0;
matrix[5] = 0;
return this;
};
TransformMatrix.prototype.translate = function (x, y)
{
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;
};
TransformMatrix.prototype.scale = function (x, y)
{
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;
};
TransformMatrix.prototype.rotate = function (radian)
{
var radianSin = mathSin(radian);
var radianCos = mathCos(radian);
return this.transform(radianCos, -radianSin, radianSin, radianCos, 0, 0);
};
TransformMatrix.prototype.multiply = function (otherMatrix)
{
var matrix = this.matrix;
var a0 = matrix[0];
var b0 = matrix[1];
var c0 = matrix[2];
var d0 = matrix[3];
var tx0 = matrix[4];
var ty0 = matrix[5];
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;
};
TransformMatrix.prototype.transform = function (a, b, c, d, tx, ty)
{
var matrix = this.matrix;
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;
};
TransformMatrix.prototype.setTransform = function (a, b, c, d, tx, ty)
{
var matrix = this.matrix;
matrix[0] = a;
matrix[1] = b;
matrix[2] = c;
matrix[3] = d;
matrix[4] = tx;
matrix[5] = ty;
return this;
};
TransformMatrix.prototype.decomposeMatrix = function ()
{
var decomposedMatrix = this.decomposedMatrix;
var matrix = this.matrix;
var a = matrix[0];
var b = matrix[1];
var c = matrix[2];
var d = matrix[3];
var a2 = a * a;
var b2 = b * b;
var c2 = c * c;
var d2 = d * d;
var sx = mathSqrt(a2 + c2);
var sy = mathSqrt(b2 + d2);
decomposedMatrix.translateX = matrix[4];
decomposedMatrix.translateY = matrix[5];
decomposedMatrix.scaleX = sx;
decomposedMatrix.scaleY = sy;
decomposedMatrix.rotation = mathAcos(a / sx) * (mathAtan(-c / a) < 0 ? -1 : 1);
return decomposedMatrix;
};
/* identity + translate + rotate + scale */
TransformMatrix.prototype.applyITRS = function (x, y, rotation, scaleX, scaleY)
{
var matrix = this.matrix;
var a = 1;
var b = 0;
var c = 0;
var d = 1;
var e = 0;
var f = 0;
var sr = mathSin(rotation);
var cr = mathCos(rotation);
// Translate
matrix[4] = a * x + c * y + e;
matrix[5] = b * x + d * y + f;
// Rotate
matrix[0] = cr * a + -sr * c;
matrix[1] = cr * b + -sr * d;
matrix[2] = sr * a + cr * c;
matrix[3] = sr * b + cr * d;
// Scale
matrix[0] = matrix[0] * scaleX;
matrix[1] = matrix[1] * scaleX;
matrix[2] = matrix[2] * scaleY;
matrix[3] = matrix[3] * scaleY;
return this;
};
module.exports = TransformMatrix;