forked from arian/CSSMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSSMatrix.js
401 lines (347 loc) · 12 KB
/
CSSMatrix.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"use strict";
// a CSSMatrix shim
// http://www.w3.org/TR/css3-3d-transforms/#cssmatrix-interface
// http://www.w3.org/TR/css3-2d-transforms/#cssmatrix-interface
/**
* CSSMatrix Shim
* @constructor
*/
var CSSMatrix = function(){
var a = [].slice.call(arguments),
m = this;
if (a.length) for (var i = a.length; i--;){
if (Math.abs(a[i]) < CSSMatrix.SMALL_NUMBER) a[i] = 0;
}
m.setIdentity();
if (a.length == 16){
m.m11 = m.a = a[0]; m.m12 = m.b = a[1]; m.m13 = a[2]; m.m14 = a[3];
m.m21 = m.c = a[4]; m.m22 = m.d = a[5]; m.m23 = a[6]; m.m24 = a[7];
m.m31 = a[8]; m.m32 = a[9]; m.m33 = a[10]; m.m34 = a[11];
m.m41 = m.e = a[12]; m.m42 = m.f = a[13]; m.m43 = a[14]; m.m44 = a[15];
} else if (a.length == 6) {
this.affine = true;
m.m11 = m.a = a[0]; m.m12 = m.b = a[1]; m.m41 = m.e = a[4];
m.m21 = m.c = a[2]; m.m22 = m.d = a[3]; m.m41 = m.f = a[5];
} else if (a.length === 1 && typeof a[0] == 'string') {
m.setMatrixValue(a[0]);
} else if (a.length > 0) {
throw new TypeError('Invalid Matrix Value');
}
};
// decimal values in WebKitCSSMatrix.prototype.toString are truncated to 6 digits
CSSMatrix.SMALL_NUMBER = 1e-6;
// Transformations
// http://en.wikipedia.org/wiki/Rotation_matrix
CSSMatrix.Rotate = function(rx, ry, rz){
rx *= Math.PI / 180;
ry *= Math.PI / 180;
rz *= Math.PI / 180;
// minus sin() because of right-handed system
var cosx = Math.cos(rx), sinx = - Math.sin(rx);
var cosy = Math.cos(ry), siny = - Math.sin(ry);
var cosz = Math.cos(rz), sinz = - Math.sin(rz);
var m = new CSSMatrix();
m.m11 = m.a = cosy * cosz;
m.m12 = m.b = - cosy * sinz;
m.m13 = siny;
m.m21 = m.c = sinx * siny * cosz + cosx * sinz;
m.m22 = m.d = cosx * cosz - sinx * siny * sinz;
m.m23 = - sinx * cosy;
m.m31 = sinx * sinz - cosx * siny * cosz;
m.m32 = sinx * cosz + cosx * siny * sinz;
m.m33 = cosx * cosy;
return m;
};
CSSMatrix.RotateAxisAngle = function(x, y, z, angle){
angle *= Math.PI / 360;
var sinA = Math.sin(angle), cosA = Math.cos(angle), sinA2 = sinA * sinA;
var length = Math.sqrt(x * x + y * y + z * z);
if (length === 0){
// bad vector length, use something reasonable
x = 0;
y = 0;
z = 1;
} else {
x /= length;
y /= length;
z /= length;
}
var x2 = x * x, y2 = y * y, z2 = z * z;
var m = new CSSMatrix();
m.m11 = m.a = 1 - 2 * (y2 + z2) * sinA2;
m.m12 = m.b = 2 * (x * y * sinA2 + z * sinA * cosA);
m.m13 = 2 * (x * z * sinA2 - y * sinA * cosA);
m.m21 = m.c = 2 * (y * x * sinA2 - z * sinA * cosA);
m.m22 = m.d = 1 - 2 * (z2 + x2) * sinA2;
m.m23 = 2 * (y * z * sinA2 + x * sinA * cosA);
m.m31 = 2 * (z * x * sinA2 + y * sinA * cosA);
m.m32 = 2 * (z * y * sinA2 - x * sinA * cosA);
m.m33 = 1 - 2 * (x2 + y2) * sinA2;
m.m14 = m.m24 = m.m34 = 0;
m.m41 = m.e = m.m42 = m.f = m.m43 = 0;
m.m44 = 1;
return m;
};
CSSMatrix.ScaleX = function(x){
var m = new CSSMatrix();
m.m11 = m.a = x;
return m;
};
CSSMatrix.ScaleY = function(y){
var m = new CSSMatrix();
m.m22 = m.d = y;
return m;
};
CSSMatrix.ScaleZ = function(z){
var m = new CSSMatrix();
m.m33 = z;
return m;
};
CSSMatrix.Scale = function(x, y, z){
var m = new CSSMatrix();
m.m11 = m.a = x;
m.m22 = m.d = y;
m.m33 = z;
return m;
};
CSSMatrix.SkewX = function(angle){
angle *= Math.PI / 180;
var m = new CSSMatrix();
m.m21 = m.c = Math.tan(angle);
return m;
};
CSSMatrix.SkewY = function(angle){
angle *= Math.PI / 180;
var m = new CSSMatrix();
m.m12 = m.b = Math.tan(angle);
return m;
};
CSSMatrix.Translate = function(x, y, z){
var m = new CSSMatrix();
m.m41 = m.e = x;
m.m42 = m.f = y;
m.m43 = z;
return m;
};
CSSMatrix.multiply = function(m1, m2){
var m11 = m2.m11 * m1.m11 + m2.m12 * m1.m21 + m2.m13 * m1.m31 + m2.m14 * m1.m41,
m12 = m2.m11 * m1.m12 + m2.m12 * m1.m22 + m2.m13 * m1.m32 + m2.m14 * m1.m42,
m13 = m2.m11 * m1.m13 + m2.m12 * m1.m23 + m2.m13 * m1.m33 + m2.m14 * m1.m43,
m14 = m2.m11 * m1.m14 + m2.m12 * m1.m24 + m2.m13 * m1.m34 + m2.m14 * m1.m44,
m21 = m2.m21 * m1.m11 + m2.m22 * m1.m21 + m2.m23 * m1.m31 + m2.m24 * m1.m41,
m22 = m2.m21 * m1.m12 + m2.m22 * m1.m22 + m2.m23 * m1.m32 + m2.m24 * m1.m42,
m23 = m2.m21 * m1.m13 + m2.m22 * m1.m23 + m2.m23 * m1.m33 + m2.m24 * m1.m43,
m24 = m2.m21 * m1.m14 + m2.m22 * m1.m24 + m2.m23 * m1.m34 + m2.m24 * m1.m44,
m31 = m2.m31 * m1.m11 + m2.m32 * m1.m21 + m2.m33 * m1.m31 + m2.m34 * m1.m41,
m32 = m2.m31 * m1.m12 + m2.m32 * m1.m22 + m2.m33 * m1.m32 + m2.m34 * m1.m42,
m33 = m2.m31 * m1.m13 + m2.m32 * m1.m23 + m2.m33 * m1.m33 + m2.m34 * m1.m43,
m34 = m2.m31 * m1.m14 + m2.m32 * m1.m24 + m2.m33 * m1.m34 + m2.m34 * m1.m44,
m41 = m2.m41 * m1.m11 + m2.m42 * m1.m21 + m2.m43 * m1.m31 + m2.m44 * m1.m41,
m42 = m2.m41 * m1.m12 + m2.m42 * m1.m22 + m2.m43 * m1.m32 + m2.m44 * m1.m42,
m43 = m2.m41 * m1.m13 + m2.m42 * m1.m23 + m2.m43 * m1.m33 + m2.m44 * m1.m43,
m44 = m2.m41 * m1.m14 + m2.m42 * m1.m24 + m2.m43 * m1.m34 + m2.m44 * m1.m44;
return new CSSMatrix(
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44
);
};
// w3c defined methods
/**
* The setMatrixValue method replaces the existing matrix with one computed
* from parsing the passed string as though it had been assigned to the
* transform property in a CSS style rule.
* @param {String} string The string to parse.
*/
CSSMatrix.prototype.setMatrixValue = function(string){
var i, m = this,
parts = [],
patternNone = /^none$/,
patternMatrix = /^matrix\((.*)\)/,
patternMatrix3d = /^matrix3d\((.*)\)/;
string = String(string).trim();
m.setIdentity();
if (patternNone.test(string)) return m;
parts = string.replace(/^.*\((.*)\)$/g, "$1").split(/\s*,\s*/);
for (i = parts.length; i--;) parts[i] = parseFloat(parts[i]);
if (patternMatrix.test(string) && parts.length === 6) {
m.affine = true;
m.m11 = m.a = parts[0]; m.m12 = m.b = parts[2]; m.m41 = m.e = parts[4];
m.m21 = m.c = parts[1]; m.m22 = m.d = parts[3]; m.m42 = m.f = parts[5];
} else if (patternMatrix3d.test(string) && parts.length === 16) {
m.m11 = m.a = parts[0]; m.m12 = m.b = parts[1]; m.m13 = parts[2]; m.m14 = parts[3];
m.m21 = m.c = parts[4]; m.m22 = m.d = parts[5]; m.m23 = parts[6]; m.m24 = parts[7];
m.m31 = parts[8]; m.m32 = parts[9]; m.m33 = parts[10]; m.m34 = parts[11];
m.m41 = m.e = parts[12]; m.m42 = m.f = parts[13]; m.m43 = parts[14]; m.m44 = parts[15];
} else {
throw new TypeError('Invalid Matrix Value');
}
return m;
};
/**
* The multiply method returns a new CSSMatrix which is the result of this
* matrix multiplied by the passed matrix, with the passed matrix to the right.
* This matrix is not modified.
*
* @param {CSSMatrix} m2
* @return {CSSMatrix} The result matrix.
*/
CSSMatrix.prototype.multiply = function(m2){
return CSSMatrix.multiply(this, m2);
};
/**
* The inverse method returns a new matrix which is the inverse of this matrix.
* This matrix is not modified.
*
* method not implemented yet
*/
CSSMatrix.prototype.inverse = function(){
throw new Error('the inverse() method is not implemented (yet).');
};
/**
* The translate method returns a new matrix which is this matrix post
* multiplied by a translation matrix containing the passed values. If the z
* component is undefined, a 0 value is used in its place. This matrix is not
* modified.
*
* @param {number} x X component of the translation value.
* @param {number} y Y component of the translation value.
* @param {number=} z Z component of the translation value.
* @return {CSSMatrix} The result matrix
*/
CSSMatrix.prototype.translate = function(x, y, z){
if (z == null) z = 0;
return CSSMatrix.multiply(this, CSSMatrix.Translate(x, y, z));
};
/**
* The scale method returns a new matrix which is this matrix post multiplied by
* a scale matrix containing the passed values. If the z component is undefined,
* a 1 value is used in its place. If the y component is undefined, the x
* component value is used in its place. This matrix is not modified.
*
* @param {number} x The X component of the scale value.
* @param {number=} y The Y component of the scale value.
* @param {number=} z The Z component of the scale value.
* @return {CSSMatrix} The result matrix
*/
CSSMatrix.prototype.scale = function(x, y, z){
if (y == null) y = x;
if (z == null) z = 1;
return CSSMatrix.multiply(this, CSSMatrix.Scale(x, y, z));
};
/**
* The rotate method returns a new matrix which is this matrix post multiplied
* by each of 3 rotation matrices about the major axes, first X, then Y, then Z.
* If the y and z components are undefined, the x value is used to rotate the
* object about the z axis, as though the vector (0,0,x) were passed. All
* rotation values are in degrees. This matrix is not modified.
*
* @param {number} rx The X component of the rotation value, or the Z component if the rotY and rotZ parameters are undefined.
* @param {number=} ry The (optional) Y component of the rotation value.
* @param {number=} rz The (optional) Z component of the rotation value.
* @return {CSSMatrix} The result matrix
*/
CSSMatrix.prototype.rotate = function(rx, ry, rz){
if (ry == null) ry = rx;
if (rz == null) rz = rx;
return CSSMatrix.multiply(this, CSSMatrix.Rotate(rx, ry, rz));
};
/**
* The rotateAxisAngle method returns a new matrix which is this matrix post
* multiplied by a rotation matrix with the given axis and angle. The right-hand
* rule is used to determine the direction of rotation. All rotation values are
* in degrees. This matrix is not modified.
*
* @param {number} x The X component of the axis vector.
* @param {number=} y The Y component of the axis vector.
* @param {number=} z The Z component of the axis vector.
* @param {number} angle The angle of rotation about the axis vector, in degrees.
* @return {CSSMatrix} The result matrix
*/
CSSMatrix.prototype.rotateAxisAngle = function(x, y, z, angle){
if (y == null) y = x;
if (z == null) z = x;
return CSSMatrix.multiply(this, CSSMatrix.RotateAxisAngle(x, y, z, angle));
};
// Defined in WebKitCSSMatrix, but not in the w3c draft
/**
* Specifies a skew transformation along the x-axis by the given angle.
*
* @param {number} angle The angle amount in degrees to skew.
* @return {CSSMatrix} The result matrix
*/
CSSMatrix.prototype.skewX = function(angle){
return CSSMatrix.multiply(this, CSSMatrix.SkewX(angle));
};
/**
* Specifies a skew transformation along the x-axis by the given angle.
*
* @param {number} angle The angle amount in degrees to skew.
* @return {CSSMatrix} The result matrix
*/
CSSMatrix.prototype.skewY = function(angle){
return CSSMatrix.multiply(this, CSSMatrix.SkewY(angle));
};
/**
* Returns a string representation of the matrix.
* @return {string}
*/
CSSMatrix.prototype.toString = function(){
var m = this;
if (this.affine){
return 'matrix(' + [
m.a, m.b,
m.c, m.d,
m.e, m.f
].join(', ') + ')';
}
// note: the elements here are transposed
return 'matrix3d(' + [
m.m11, m.m12, m.m13, m.m14,
m.m21, m.m22, m.m23, m.m24,
m.m31, m.m32, m.m33, m.m34,
m.m41, m.m42, m.m43, m.m44
].join(', ') + ')';
};
// Additional methods
/**
* Set the current matrix to the identity form
*
* @return {CSSMatrix} this matrix
*/
CSSMatrix.prototype.setIdentity = function(){
var m = this;
m.m11 = m.a = 1; m.m12 = m.b = 0; m.m13 = 0; m.m14 = 0;
m.m21 = m.c = 0; m.m22 = m.d = 1; m.m23 = 0; m.m24 = 0;
m.m31 = 0; m.m32 = 0; m.m33 = 1; m.m34 = 0;
m.m41 = m.e = 0; m.m42 = m.f = 0; m.m43 = 0; m.m44 = 1;
return this;
};
/**
* Transform a tuple (3d point) with this CSSMatrix
*
* @param {Tuple} an object with x, y, z and w properties
* @return {Tuple} the passed tuple
*/
CSSMatrix.prototype.transform = function(t /* tuple */ ){
var m = this;
var x = m.m11 * t.x + m.m12 * t.y + m.m13 * t.z + m.m14 * t.w,
y = m.m21 * t.x + m.m22 * t.y + m.m23 * t.z + m.m24 * t.w,
z = m.m31 * t.x + m.m32 * t.y + m.m33 * t.z + m.m34 * t.w,
w = m.m41 * t.x + m.m42 * t.y + m.m43 * t.z + m.m44 * t.w;
t.x = x / w;
t.y = y / w;
t.z = z / w;
return t;
};
CSSMatrix.prototype.toFullString = function(){
var m = this;
return [
[m.m11, m.m12, m.m13, m.m14].join(', '),
[m.m21, m.m22, m.m23, m.m24].join(', '),
[m.m31, m.m32, m.m33, m.m34].join(', '),
[m.m41, m.m42, m.m43, m.m44].join(', ')
].join('\n');
};
module.exports = CSSMatrix;