@thednp/dommatrix 2.0.0-alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,912 @@
1
+ import type { Matrix, Matrix3d, JSONMatrix, CSSMatrixInput, PointTuple } from './types';
2
+
3
+ /** A model for JSONMatrix */
4
+ const JSON_MATRIX: JSONMatrix = {
5
+ a: 1,
6
+ b: 0,
7
+ c: 0,
8
+ d: 1,
9
+ e: 0,
10
+ f: 0,
11
+ m11: 1,
12
+ m12: 0,
13
+ m13: 0,
14
+ m14: 0,
15
+ m21: 0,
16
+ m22: 1,
17
+ m23: 0,
18
+ m24: 0,
19
+ m31: 0,
20
+ m32: 0,
21
+ m33: 1,
22
+ m34: 0,
23
+ m41: 0,
24
+ m42: 0,
25
+ m43: 0,
26
+ m44: 1,
27
+ is2D: true,
28
+ isIdentity: true,
29
+ };
30
+
31
+ // CSSMatrix Static methods
32
+ // * `fromArray` is a more simple implementation, should also accept Float[32/64]Array;
33
+ // * `fromMatrix` load values from another CSSMatrix/DOMMatrix instance or JSON object;
34
+ // * `fromString` parses and loads values from any valid CSS transform string (TransformList).
35
+ // * `isCompatibleArray` Checks if an array is compatible with CSSMatrix.
36
+ // * `isCompatibleObject` Checks if an object is compatible with CSSMatrix.
37
+
38
+ /** Checks if an array is compatible with CSSMatrix */
39
+ const isCompatibleArray = (array?: unknown): array is Matrix | Matrix3d | Float32Array | Float64Array => {
40
+ return (
41
+ (array instanceof Float64Array ||
42
+ array instanceof Float32Array ||
43
+ (Array.isArray(array) && array.every(x => typeof x === 'number'))) &&
44
+ [6, 16].some(x => array.length === x)
45
+ );
46
+ };
47
+
48
+ /** Checks if an object is compatible with CSSMatrix */
49
+ const isCompatibleObject = (object?: unknown): object is CSSMatrix | DOMMatrix | JSONMatrix => {
50
+ return (
51
+ object instanceof DOMMatrix ||
52
+ object instanceof CSSMatrix ||
53
+ (typeof object === 'object' && Object.keys(JSON_MATRIX).every(k => object && k in object))
54
+ );
55
+ };
56
+
57
+ /**
58
+ * Creates a new mutable `CSSMatrix` instance given an array of 16/6 floating point values.
59
+ * This static method invalidates arrays that contain non-number elements.
60
+ *
61
+ * If the array has six values, the result is a 2D matrix; if the array has 16 values,
62
+ * the result is a 3D matrix. Otherwise, a TypeError exception is thrown.
63
+ *
64
+ * @param array an `Array` to feed values from.
65
+ * @return the resulted matrix.
66
+ */
67
+ const fromArray = (array: any[] | Float32Array | Float64Array): CSSMatrix => {
68
+ const m = new CSSMatrix();
69
+ const a = Array.from(array);
70
+
71
+ if (!isCompatibleArray(a)) {
72
+ throw TypeError(`CSSMatrix: "${a.join(',')}" must be an array with 6/16 numbers.`);
73
+ }
74
+ if (a.length === 16) {
75
+ const [m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44] = a;
76
+
77
+ m.m11 = m11;
78
+ m.a = m11;
79
+
80
+ m.m21 = m21;
81
+ m.c = m21;
82
+
83
+ m.m31 = m31;
84
+
85
+ m.m41 = m41;
86
+ m.e = m41;
87
+
88
+ m.m12 = m12;
89
+ m.b = m12;
90
+
91
+ m.m22 = m22;
92
+ m.d = m22;
93
+
94
+ m.m32 = m32;
95
+
96
+ m.m42 = m42;
97
+ m.f = m42;
98
+
99
+ m.m13 = m13;
100
+ m.m23 = m23;
101
+ m.m33 = m33;
102
+ m.m43 = m43;
103
+ m.m14 = m14;
104
+ m.m24 = m24;
105
+ m.m34 = m34;
106
+ m.m44 = m44;
107
+ } else if (a.length === 6) {
108
+ const [M11, M12, M21, M22, M41, M42] = a;
109
+
110
+ m.m11 = M11;
111
+ m.a = M11;
112
+
113
+ m.m12 = M12;
114
+ m.b = M12;
115
+
116
+ m.m21 = M21;
117
+ m.c = M21;
118
+
119
+ m.m22 = M22;
120
+ m.d = M22;
121
+
122
+ m.m41 = M41;
123
+ m.e = M41;
124
+
125
+ m.m42 = M42;
126
+ m.f = M42;
127
+ }
128
+ return m;
129
+ };
130
+
131
+ /**
132
+ * Creates a new mutable `CSSMatrix` instance given an existing matrix or a
133
+ * `DOMMatrix` instance which provides the values for its properties.
134
+ *
135
+ * @param m the source matrix to feed values from.
136
+ * @return the resulted matrix.
137
+ */
138
+ const fromMatrix = (m: CSSMatrix | DOMMatrix | JSONMatrix): CSSMatrix => {
139
+ if (isCompatibleObject(m)) {
140
+ return fromArray([
141
+ m.m11,
142
+ m.m12,
143
+ m.m13,
144
+ m.m14,
145
+ m.m21,
146
+ m.m22,
147
+ m.m23,
148
+ m.m24,
149
+ m.m31,
150
+ m.m32,
151
+ m.m33,
152
+ m.m34,
153
+ m.m41,
154
+ m.m42,
155
+ m.m43,
156
+ m.m44,
157
+ ]);
158
+ }
159
+ throw TypeError(`CSSMatrix: "${JSON.stringify(m)}" is not a DOMMatrix / CSSMatrix / JSON compatible object.`);
160
+ };
161
+
162
+ /**
163
+ * Creates a new mutable `CSSMatrix` given any valid CSS transform string,
164
+ * or what we call `TransformList`:
165
+ *
166
+ * * `matrix(a, b, c, d, e, f)` - valid matrix() transform function
167
+ * * `matrix3d(m11, m12, m13, ...m44)` - valid matrix3d() transform function
168
+ * * `translate(tx, ty) rotateX(alpha)` - any valid transform function(s)
169
+ *
170
+ * @copyright thednp © 2021
171
+ *
172
+ * @param source valid CSS transform string syntax.
173
+ * @return the resulted matrix.
174
+ */
175
+ const fromString = (source: string): CSSMatrix => {
176
+ if (typeof source !== 'string') {
177
+ throw TypeError(`CSSMatrix: "${JSON.stringify(source)}" is not a string.`);
178
+ }
179
+ const str = String(source).replace(/\s/g, '');
180
+ let m = new CSSMatrix();
181
+ const invalidStringError = `CSSMatrix: invalid transform string "${source}"`;
182
+
183
+ // const px = ['perspective'];
184
+ // const length = ['translate', 'translate3d', 'translateX', 'translateY', 'translateZ'];
185
+ // const deg = ['rotate', 'rotate3d', 'rotateX', 'rotateY', 'rotateZ', 'skew', 'skewX', 'skewY'];
186
+ // const abs = ['scale', 'scale3d', 'matrix', 'matrix3d'];
187
+ // const transformFunctions = px.concat(length, deg, abs);
188
+
189
+ str
190
+ .split(')')
191
+ .filter(f => f)
192
+ .forEach(tf => {
193
+ const [prop, value] = tf.split('(');
194
+
195
+ // invalidate empty string
196
+ if (!value) throw TypeError(invalidStringError);
197
+
198
+ const components = value
199
+ .split(',')
200
+ .map(n => (n.includes('rad') ? parseFloat(n) * (180 / Math.PI) : parseFloat(n)));
201
+
202
+ const [x, y, z, a] = components;
203
+ const xyz = [x, y, z];
204
+ const xyza = [x, y, z, a];
205
+
206
+ // single number value expected
207
+ if (prop === 'perspective' && x && [y, z].every(n => n === undefined)) {
208
+ m.m34 = -1 / x;
209
+ // 6/16 number values expected
210
+ } else if (
211
+ prop.includes('matrix') &&
212
+ [6, 16].includes(components.length) &&
213
+ components.every(n => !Number.isNaN(+n))
214
+ ) {
215
+ const values = components.map(n => (Math.abs(n) < 1e-6 ? 0 : n));
216
+ m = m.multiply(fromArray(values as Matrix | Matrix3d));
217
+ // 3 values expected
218
+ } else if (prop === 'translate3d' && xyz.every(n => !Number.isNaN(+n))) {
219
+ m = m.translate(x, y, z);
220
+ // single/double number value(s) expected
221
+ } else if (prop === 'translate' && x && z === undefined) {
222
+ m = m.translate(x, y || 0, 0);
223
+ // all 4 values expected
224
+ } else if (prop === 'rotate3d' && xyza.every(n => !Number.isNaN(+n)) && a) {
225
+ m = m.rotateAxisAngle(x, y, z, a);
226
+ // single value expected
227
+ } else if (prop === 'rotate' && x && [y, z].every(n => n === undefined)) {
228
+ m = m.rotate(0, 0, x);
229
+ // 3 values expected
230
+ } else if (prop === 'scale3d' && xyz.every(n => !Number.isNaN(+n)) && xyz.some(n => n !== 1)) {
231
+ m = m.scale(x, y, z);
232
+ // single value expected
233
+ } else if (prop === 'scale' && !Number.isNaN(x) && x !== 1 && z === undefined) {
234
+ const nosy = Number.isNaN(+y);
235
+ const sy = nosy ? x : y;
236
+ m = m.scale(x, sy, 1);
237
+ // single/double value expected
238
+ } else if (prop === 'skew' && (x || (!Number.isNaN(x) && y)) && z === undefined) {
239
+ m = m.skew(x, y || 0);
240
+ } else if (
241
+ ['translate', 'rotate', 'scale', 'skew'].some(p => prop.includes(p)) &&
242
+ /[XYZ]/.test(prop) &&
243
+ x &&
244
+ [y, z].every(n => n === undefined) // a single value expected
245
+ ) {
246
+ if ('skewX' === prop || 'skewY' === prop) {
247
+ m = m[prop](x);
248
+ } else {
249
+ const fn = prop.replace(/[XYZ]/, '') as 'scale' | 'translate' | 'rotate';
250
+ const axis = prop.replace(fn, '');
251
+ const idx = ['X', 'Y', 'Z'].indexOf(axis);
252
+ const def = fn === 'scale' ? 1 : 0;
253
+ const axeValues: [number, number, number] = [idx === 0 ? x : def, idx === 1 ? x : def, idx === 2 ? x : def];
254
+ m = m[fn](...axeValues);
255
+ }
256
+ } else {
257
+ throw TypeError(invalidStringError);
258
+ }
259
+ });
260
+
261
+ return m;
262
+ };
263
+
264
+ /**
265
+ * Returns an *Array* containing elements which comprise the matrix.
266
+ * The method can return either the 16 elements or the 6 elements
267
+ * depending on the value of the `is2D` parameter.
268
+ *
269
+ * @param m the source matrix to feed values from.
270
+ * @param is2D *Array* representation of the matrix
271
+ * @return an *Array* representation of the matrix
272
+ */
273
+ const toArray = (m: CSSMatrix | DOMMatrix | JSONMatrix, is2D?: boolean): Matrix | Matrix3d => {
274
+ if (is2D) {
275
+ return [m.a, m.b, m.c, m.d, m.e, m.f];
276
+ }
277
+ return [
278
+ m.m11,
279
+ m.m12,
280
+ m.m13,
281
+ m.m14,
282
+ m.m21,
283
+ m.m22,
284
+ m.m23,
285
+ m.m24,
286
+ m.m31,
287
+ m.m32,
288
+ m.m33,
289
+ m.m34,
290
+ m.m41,
291
+ m.m42,
292
+ m.m43,
293
+ m.m44,
294
+ ];
295
+ };
296
+
297
+ // Transform Functions
298
+ // https://www.w3.org/TR/css-transforms-1/#transform-functions
299
+
300
+ /**
301
+ * Creates a new `CSSMatrix` for the translation matrix and returns it.
302
+ * This method is equivalent to the CSS `translate3d()` function.
303
+ *
304
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate3d
305
+ *
306
+ * @param x the `x-axis` position.
307
+ * @param y the `y-axis` position.
308
+ * @param z the `z-axis` position.
309
+ * @return the resulted matrix.
310
+ */
311
+ const Translate = (x: number, y: number, z: number): CSSMatrix => {
312
+ const m = new CSSMatrix();
313
+ m.m41 = x;
314
+ m.e = x;
315
+ m.m42 = y;
316
+ m.f = y;
317
+ m.m43 = z;
318
+ return m;
319
+ };
320
+
321
+ /**
322
+ * Creates a new `CSSMatrix` for the rotation matrix and returns it.
323
+ *
324
+ * http://en.wikipedia.org/wiki/Rotation_matrix
325
+ *
326
+ * @param rx the `x-axis` rotation.
327
+ * @param ry the `y-axis` rotation.
328
+ * @param rz the `z-axis` rotation.
329
+ * @return the resulted matrix.
330
+ */
331
+ const Rotate = (rx: number, ry: number, rz: number): CSSMatrix => {
332
+ const m = new CSSMatrix();
333
+ const degToRad = Math.PI / 180;
334
+ const radX = rx * degToRad;
335
+ const radY = ry * degToRad;
336
+ const radZ = rz * degToRad;
337
+
338
+ // minus sin() because of right-handed system
339
+ const cosx = Math.cos(radX);
340
+ const sinx = -Math.sin(radX);
341
+ const cosy = Math.cos(radY);
342
+ const siny = -Math.sin(radY);
343
+ const cosz = Math.cos(radZ);
344
+ const sinz = -Math.sin(radZ);
345
+
346
+ const m11 = cosy * cosz;
347
+ const m12 = -cosy * sinz;
348
+
349
+ m.m11 = m11;
350
+ m.a = m11;
351
+
352
+ m.m12 = m12;
353
+ m.b = m12;
354
+
355
+ m.m13 = siny;
356
+
357
+ const m21 = sinx * siny * cosz + cosx * sinz;
358
+ m.m21 = m21;
359
+ m.c = m21;
360
+
361
+ const m22 = cosx * cosz - sinx * siny * sinz;
362
+ m.m22 = m22;
363
+ m.d = m22;
364
+
365
+ m.m23 = -sinx * cosy;
366
+
367
+ m.m31 = sinx * sinz - cosx * siny * cosz;
368
+ m.m32 = sinx * cosz + cosx * siny * sinz;
369
+ m.m33 = cosx * cosy;
370
+
371
+ return m;
372
+ };
373
+
374
+ /**
375
+ * Creates a new `CSSMatrix` for the rotation matrix and returns it.
376
+ * This method is equivalent to the CSS `rotate3d()` function.
377
+ *
378
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotate3d
379
+ *
380
+ * @param x the `x-axis` vector length.
381
+ * @param y the `y-axis` vector length.
382
+ * @param z the `z-axis` vector length.
383
+ * @param alpha the value in degrees of the rotation.
384
+ * @return the resulted matrix.
385
+ */
386
+ const RotateAxisAngle = (x: number, y: number, z: number, alpha: number): CSSMatrix => {
387
+ const m = new CSSMatrix();
388
+ const length = Math.sqrt(x * x + y * y + z * z);
389
+
390
+ if (length === 0) {
391
+ // bad vector length, return identity
392
+ return m;
393
+ }
394
+
395
+ const X = x / length;
396
+ const Y = y / length;
397
+ const Z = z / length;
398
+
399
+ const angle = alpha * (Math.PI / 360);
400
+ const sinA = Math.sin(angle);
401
+ const cosA = Math.cos(angle);
402
+ const sinA2 = sinA * sinA;
403
+ const x2 = X * X;
404
+ const y2 = Y * Y;
405
+ const z2 = Z * Z;
406
+
407
+ const m11 = 1 - 2 * (y2 + z2) * sinA2;
408
+ m.m11 = m11;
409
+ m.a = m11;
410
+
411
+ const m12 = 2 * (X * Y * sinA2 + Z * sinA * cosA);
412
+ m.m12 = m12;
413
+ m.b = m12;
414
+
415
+ m.m13 = 2 * (X * Z * sinA2 - Y * sinA * cosA);
416
+
417
+ const m21 = 2 * (Y * X * sinA2 - Z * sinA * cosA);
418
+ m.m21 = m21;
419
+ m.c = m21;
420
+
421
+ const m22 = 1 - 2 * (z2 + x2) * sinA2;
422
+ m.m22 = m22;
423
+ m.d = m22;
424
+
425
+ m.m23 = 2 * (Y * Z * sinA2 + X * sinA * cosA);
426
+ m.m31 = 2 * (Z * X * sinA2 + Y * sinA * cosA);
427
+ m.m32 = 2 * (Z * Y * sinA2 - X * sinA * cosA);
428
+ m.m33 = 1 - 2 * (x2 + y2) * sinA2;
429
+
430
+ return m;
431
+ };
432
+
433
+ /**
434
+ * Creates a new `CSSMatrix` for the scale matrix and returns it.
435
+ * This method is equivalent to the CSS `scale3d()` function, except it doesn't
436
+ * accept {x, y, z} transform origin parameters.
437
+ *
438
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale3d
439
+ *
440
+ * @param x the `x-axis` scale.
441
+ * @param y the `y-axis` scale.
442
+ * @param z the `z-axis` scale.
443
+ * @return the resulted matrix.
444
+ */
445
+ const Scale = (x: number, y: number, z: number): CSSMatrix => {
446
+ const m = new CSSMatrix();
447
+ m.m11 = x;
448
+ m.a = x;
449
+
450
+ m.m22 = y;
451
+ m.d = y;
452
+
453
+ m.m33 = z;
454
+ return m;
455
+ };
456
+
457
+ /**
458
+ * Creates a new `CSSMatrix` for the shear of both the `x-axis` and`y-axis`
459
+ * matrix and returns it. This method is equivalent to the CSS `skew()` function.
460
+ *
461
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/skew
462
+ *
463
+ * @param angleX the X-angle in degrees.
464
+ * @param angleY the Y-angle in degrees.
465
+ * @return the resulted matrix.
466
+ */
467
+ const Skew = (angleX: number, angleY: number): CSSMatrix => {
468
+ const m = new CSSMatrix();
469
+ if (angleX) {
470
+ const radX = (angleX * Math.PI) / 180;
471
+ const tX = Math.tan(radX);
472
+ m.m21 = tX;
473
+ m.c = tX;
474
+ }
475
+ if (angleY) {
476
+ const radY = (angleY * Math.PI) / 180;
477
+ const tY = Math.tan(radY);
478
+ m.m12 = tY;
479
+ m.b = tY;
480
+ }
481
+ return m;
482
+ };
483
+
484
+ /**
485
+ * Creates a new `CSSMatrix` for the shear of the `x-axis` rotation matrix and
486
+ * returns it. This method is equivalent to the CSS `skewX()` function.
487
+ *
488
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/skewX
489
+ *
490
+ * @param angle the angle in degrees.
491
+ * @return the resulted matrix.
492
+ */
493
+ const SkewX = (angle: number): CSSMatrix => {
494
+ return Skew(angle, 0);
495
+ };
496
+
497
+ /**
498
+ * Creates a new `CSSMatrix` for the shear of the `y-axis` rotation matrix and
499
+ * returns it. This method is equivalent to the CSS `skewY()` function.
500
+ *
501
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/skewY
502
+ *
503
+ * @param angle the angle in degrees.
504
+ * @return the resulted matrix.
505
+ */
506
+ const SkewY = (angle: number): CSSMatrix => {
507
+ return Skew(0, angle);
508
+ };
509
+
510
+ /**
511
+ * Creates a new `CSSMatrix` resulted from the multiplication of two matrixes
512
+ * and returns it. Both matrixes are not changed.
513
+ *
514
+ * @param m1 the first matrix.
515
+ * @param m2 the second matrix.
516
+ * @return the resulted matrix.
517
+ */
518
+ const Multiply = (m1: CSSMatrix | DOMMatrix | JSONMatrix, m2: CSSMatrix | DOMMatrix | JSONMatrix): CSSMatrix => {
519
+ const m11 = m2.m11 * m1.m11 + m2.m12 * m1.m21 + m2.m13 * m1.m31 + m2.m14 * m1.m41;
520
+ const m12 = m2.m11 * m1.m12 + m2.m12 * m1.m22 + m2.m13 * m1.m32 + m2.m14 * m1.m42;
521
+ const m13 = m2.m11 * m1.m13 + m2.m12 * m1.m23 + m2.m13 * m1.m33 + m2.m14 * m1.m43;
522
+ const m14 = m2.m11 * m1.m14 + m2.m12 * m1.m24 + m2.m13 * m1.m34 + m2.m14 * m1.m44;
523
+
524
+ const m21 = m2.m21 * m1.m11 + m2.m22 * m1.m21 + m2.m23 * m1.m31 + m2.m24 * m1.m41;
525
+ const m22 = m2.m21 * m1.m12 + m2.m22 * m1.m22 + m2.m23 * m1.m32 + m2.m24 * m1.m42;
526
+ const m23 = m2.m21 * m1.m13 + m2.m22 * m1.m23 + m2.m23 * m1.m33 + m2.m24 * m1.m43;
527
+ const m24 = m2.m21 * m1.m14 + m2.m22 * m1.m24 + m2.m23 * m1.m34 + m2.m24 * m1.m44;
528
+
529
+ const m31 = m2.m31 * m1.m11 + m2.m32 * m1.m21 + m2.m33 * m1.m31 + m2.m34 * m1.m41;
530
+ const m32 = m2.m31 * m1.m12 + m2.m32 * m1.m22 + m2.m33 * m1.m32 + m2.m34 * m1.m42;
531
+ const m33 = m2.m31 * m1.m13 + m2.m32 * m1.m23 + m2.m33 * m1.m33 + m2.m34 * m1.m43;
532
+ const m34 = m2.m31 * m1.m14 + m2.m32 * m1.m24 + m2.m33 * m1.m34 + m2.m34 * m1.m44;
533
+
534
+ const m41 = m2.m41 * m1.m11 + m2.m42 * m1.m21 + m2.m43 * m1.m31 + m2.m44 * m1.m41;
535
+ const m42 = m2.m41 * m1.m12 + m2.m42 * m1.m22 + m2.m43 * m1.m32 + m2.m44 * m1.m42;
536
+ const m43 = m2.m41 * m1.m13 + m2.m42 * m1.m23 + m2.m43 * m1.m33 + m2.m44 * m1.m43;
537
+ const m44 = m2.m41 * m1.m14 + m2.m42 * m1.m24 + m2.m43 * m1.m34 + m2.m44 * m1.m44;
538
+
539
+ return fromArray([m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44]);
540
+ };
541
+
542
+ /**
543
+ * Creates and returns a new `DOMMatrix` compatible instance
544
+ * with equivalent instance.
545
+ *
546
+ * @class CSSMatrix
547
+ *
548
+ * @author thednp <https://github.com/thednp/DOMMatrix/>
549
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMMatrix
550
+ */
551
+ export default class CSSMatrix {
552
+ declare m11: number;
553
+ declare m12: number;
554
+ declare m13: number;
555
+ declare m14: number;
556
+ declare m21: number;
557
+ declare m22: number;
558
+ declare m23: number;
559
+ declare m24: number;
560
+ declare m31: number;
561
+ declare m32: number;
562
+ declare m33: number;
563
+ declare m34: number;
564
+ declare m41: number;
565
+ declare m42: number;
566
+ declare m43: number;
567
+ declare m44: number;
568
+ declare a: number;
569
+ declare b: number;
570
+ declare c: number;
571
+ declare d: number;
572
+ declare e: number;
573
+ declare f: number;
574
+ static Translate = Translate;
575
+ static Rotate = Rotate;
576
+ static RotateAxisAngle = RotateAxisAngle;
577
+ static Scale = Scale;
578
+ static SkewX = SkewX;
579
+ static SkewY = SkewY;
580
+ static Skew = Skew;
581
+ static Multiply = Multiply;
582
+ static fromArray = fromArray;
583
+ static fromMatrix = fromMatrix;
584
+ static fromString = fromString;
585
+ static toArray = toArray;
586
+ static isCompatibleArray = isCompatibleArray;
587
+ static isCompatibleObject = isCompatibleObject;
588
+
589
+ /**
590
+ * @constructor
591
+ * @param init accepts all parameter configurations:
592
+ * * valid CSS transform string,
593
+ * * CSSMatrix/DOMMatrix instance,
594
+ * * a 6/16 elements *Array*.
595
+ */
596
+ constructor(init?: CSSMatrixInput) {
597
+ // array 6
598
+ this.a = 1;
599
+ this.b = 0;
600
+ this.c = 0;
601
+ this.d = 1;
602
+ this.e = 0;
603
+ this.f = 0;
604
+ // array 16
605
+ this.m11 = 1;
606
+ this.m12 = 0;
607
+ this.m13 = 0;
608
+ this.m14 = 0;
609
+ this.m21 = 0;
610
+ this.m22 = 1;
611
+ this.m23 = 0;
612
+ this.m24 = 0;
613
+ this.m31 = 0;
614
+ this.m32 = 0;
615
+ this.m33 = 1;
616
+ this.m34 = 0;
617
+ this.m41 = 0;
618
+ this.m42 = 0;
619
+ this.m43 = 0;
620
+ this.m44 = 1;
621
+
622
+ if (init) {
623
+ return this.setMatrixValue(init);
624
+ }
625
+ return this;
626
+ }
627
+
628
+ /**
629
+ * A `Boolean` whose value is `true` if the matrix is the identity matrix. The identity
630
+ * matrix is one in which every value is 0 except those on the main diagonal from top-left
631
+ * to bottom-right corner (in other words, where the offsets in each direction are equal).
632
+ *
633
+ * @return the current property value
634
+ */
635
+ get isIdentity(): boolean {
636
+ return (
637
+ this.m11 === 1 &&
638
+ this.m12 === 0 &&
639
+ this.m13 === 0 &&
640
+ this.m14 === 0 &&
641
+ this.m21 === 0 &&
642
+ this.m22 === 1 &&
643
+ this.m23 === 0 &&
644
+ this.m24 === 0 &&
645
+ this.m31 === 0 &&
646
+ this.m32 === 0 &&
647
+ this.m33 === 1 &&
648
+ this.m34 === 0 &&
649
+ this.m41 === 0 &&
650
+ this.m42 === 0 &&
651
+ this.m43 === 0 &&
652
+ this.m44 === 1
653
+ );
654
+ }
655
+
656
+ /**
657
+ * A `Boolean` flag whose value is `true` if the matrix was initialized as a 2D matrix
658
+ * and `false` if the matrix is 3D.
659
+ *
660
+ * @return the current property value
661
+ */
662
+ get is2D(): boolean {
663
+ return this.m31 === 0 && this.m32 === 0 && this.m33 === 1 && this.m34 === 0 && this.m43 === 0 && this.m44 === 1;
664
+ }
665
+
666
+ /**
667
+ * The `setMatrixValue` method replaces the existing matrix with one computed
668
+ * in the browser. EG: `matrix(1,0.25,-0.25,1,0,0)`
669
+ *
670
+ * The method accepts any *Array* values, the result of
671
+ * `DOMMatrix` instance method `toFloat64Array()` / `toFloat32Array()` calls
672
+ * or `CSSMatrix` instance method `toArray()`.
673
+ *
674
+ * This method expects valid *matrix()* / *matrix3d()* string values, as well
675
+ * as other transform functions like *translateX(10px)*.
676
+ *
677
+ * @param source
678
+ * @return the matrix instance
679
+ */
680
+ setMatrixValue(source?: CSSMatrixInput): CSSMatrix {
681
+ // CSS transform string source - TransformList first
682
+ if (typeof source === 'string' && source.length && source !== 'none') {
683
+ return fromString(source);
684
+ }
685
+
686
+ // [Array | Float[32/64]Array] come next
687
+ if (Array.isArray(source) || source instanceof Float64Array || source instanceof Float32Array) {
688
+ return fromArray(source);
689
+ }
690
+
691
+ // new CSSMatrix(CSSMatrix | DOMMatrix | JSONMatrix) last
692
+ if (typeof source === 'object') {
693
+ return fromMatrix(source);
694
+ }
695
+
696
+ return this;
697
+ }
698
+
699
+ /**
700
+ * Returns a *Float32Array* containing elements which comprise the matrix.
701
+ * The method can return either the 16 elements or the 6 elements
702
+ * depending on the value of the `is2D` parameter.
703
+ *
704
+ * @param is2D *Array* representation of the matrix
705
+ * @return an *Array* representation of the matrix
706
+ */
707
+ toFloat32Array(is2D?: boolean): Float32Array {
708
+ return Float32Array.from(toArray(this, is2D));
709
+ }
710
+
711
+ /**
712
+ * Returns a *Float64Array* containing elements which comprise the matrix.
713
+ * The method can return either the 16 elements or the 6 elements
714
+ * depending on the value of the `is2D` parameter.
715
+ *
716
+ * @param is2D *Array* representation of the matrix
717
+ * @return an *Array* representation of the matrix
718
+ */
719
+ toFloat64Array(is2D?: boolean): Float64Array {
720
+ return Float64Array.from(toArray(this, is2D));
721
+ }
722
+
723
+ /**
724
+ * Creates and returns a string representation of the matrix in `CSS` matrix syntax,
725
+ * using the appropriate `CSS` matrix notation.
726
+ *
727
+ * matrix3d *matrix3d(m11, m12, m13, m14, m21, ...)*
728
+ * matrix *matrix(a, b, c, d, e, f)*
729
+ *
730
+ * @return a string representation of the matrix
731
+ */
732
+ toString(): string {
733
+ const { is2D } = this;
734
+ const values = this.toFloat64Array(is2D).join(', ');
735
+ const type = is2D ? 'matrix' : 'matrix3d';
736
+ return `${type}(${values})`;
737
+ }
738
+
739
+ /**
740
+ * Returns a JSON representation of the `CSSMatrix` instance, a standard *Object*
741
+ * that includes `{a,b,c,d,e,f}` and `{m11,m12,m13,..m44}` properties as well
742
+ * as the `is2D` & `isIdentity` properties.
743
+ *
744
+ * The result can also be used as a second parameter for the `fromMatrix` static method
745
+ * to load values into another matrix instance.
746
+ *
747
+ * @return an *Object* with all matrix values.
748
+ */
749
+ toJSON(): JSONMatrix {
750
+ const { is2D, isIdentity } = this;
751
+ return { ...this, is2D, isIdentity };
752
+ }
753
+
754
+ /**
755
+ * The Multiply method returns a new CSSMatrix which is the result of this
756
+ * matrix multiplied by the passed matrix, with the passed matrix to the right.
757
+ * This matrix is not modified.
758
+ *
759
+ * @param m2 CSSMatrix
760
+ * @return The resulted matrix.
761
+ */
762
+ multiply(m2: CSSMatrix | DOMMatrix | JSONMatrix): CSSMatrix {
763
+ return Multiply(this, m2);
764
+ }
765
+
766
+ /**
767
+ * The translate method returns a new matrix which is this matrix post
768
+ * multiplied by a translation matrix containing the passed values. If the z
769
+ * component is undefined, a 0 value is used in its place. This matrix is not
770
+ * modified.
771
+ *
772
+ * @param x X component of the translation value.
773
+ * @param y Y component of the translation value.
774
+ * @param z Z component of the translation value.
775
+ * @return The resulted matrix
776
+ */
777
+ translate(x: number, y?: number, z?: number): CSSMatrix {
778
+ const X = x;
779
+ let Y = y;
780
+ let Z = z;
781
+ if (typeof Y === 'undefined') Y = 0;
782
+ if (typeof Z === 'undefined') Z = 0;
783
+ return Multiply(this, Translate(X, Y, Z));
784
+ }
785
+
786
+ /**
787
+ * The scale method returns a new matrix which is this matrix post multiplied by
788
+ * a scale matrix containing the passed values. If the z component is undefined,
789
+ * a 1 value is used in its place. If the y component is undefined, the x
790
+ * component value is used in its place. This matrix is not modified.
791
+ *
792
+ * @param x The X component of the scale value.
793
+ * @param y The Y component of the scale value.
794
+ * @param z The Z component of the scale value.
795
+ * @return The resulted matrix
796
+ */
797
+ scale(x: number, y?: number, z?: number): CSSMatrix {
798
+ const X = x;
799
+ let Y = y;
800
+ let Z = z;
801
+ if (typeof Y === 'undefined') Y = x;
802
+ if (typeof Z === 'undefined') Z = 1; // Z must be 1 if undefined
803
+
804
+ return Multiply(this, Scale(X, Y, Z));
805
+ }
806
+
807
+ /**
808
+ * The rotate method returns a new matrix which is this matrix post multiplied
809
+ * by each of 3 rotation matrices about the major axes, first X, then Y, then Z.
810
+ * If the y and z components are undefined, the x value is used to rotate the
811
+ * object about the z axis, as though the vector (0,0,x) were passed. All
812
+ * rotation values are in degrees. This matrix is not modified.
813
+ *
814
+ * @param rx The X component of the rotation, or Z if Y and Z are null.
815
+ * @param ry The (optional) Y component of the rotation value.
816
+ * @param rz The (optional) Z component of the rotation value.
817
+ * @return The resulted matrix
818
+ */
819
+ rotate(rx: number, ry?: number, rz?: number): CSSMatrix {
820
+ let RX = rx;
821
+ let RY = ry || 0;
822
+ let RZ = rz || 0;
823
+
824
+ if (typeof rx === 'number' && typeof ry === 'undefined' && typeof rz === 'undefined') {
825
+ RZ = RX;
826
+ RX = 0;
827
+ RY = 0;
828
+ }
829
+
830
+ return Multiply(this, Rotate(RX, RY, RZ));
831
+ }
832
+
833
+ /**
834
+ * The rotateAxisAngle method returns a new matrix which is this matrix post
835
+ * multiplied by a rotation matrix with the given axis and `angle`. The right-hand
836
+ * rule is used to determine the direction of rotation. All rotation values are
837
+ * in degrees. This matrix is not modified.
838
+ *
839
+ * @param x The X component of the axis vector.
840
+ * @param y The Y component of the axis vector.
841
+ * @param z The Z component of the axis vector.
842
+ * @param angle The angle of rotation about the axis vector, in degrees.
843
+ * @return The resulted matrix
844
+ */
845
+ rotateAxisAngle(x: number, y: number, z: number, angle: number): CSSMatrix {
846
+ if ([x, y, z, angle].some(n => Number.isNaN(+n))) {
847
+ throw new TypeError('CSSMatrix: expecting 4 values');
848
+ }
849
+ return Multiply(this, RotateAxisAngle(x, y, z, angle));
850
+ }
851
+
852
+ /**
853
+ * Specifies a skew transformation along the `x-axis` by the given angle.
854
+ * This matrix is not modified.
855
+ *
856
+ * @param angle The angle amount in degrees to skew.
857
+ * @return The resulted matrix
858
+ */
859
+ skewX(angle: number): CSSMatrix {
860
+ return Multiply(this, SkewX(angle));
861
+ }
862
+
863
+ /**
864
+ * Specifies a skew transformation along the `y-axis` by the given angle.
865
+ * This matrix is not modified.
866
+ *
867
+ * @param angle The angle amount in degrees to skew.
868
+ * @return The resulted matrix
869
+ */
870
+ skewY(angle: number): CSSMatrix {
871
+ return Multiply(this, SkewY(angle));
872
+ }
873
+
874
+ /**
875
+ * Specifies a skew transformation along both the `x-axis` and `y-axis`.
876
+ * This matrix is not modified.
877
+ *
878
+ * @param angleX The X-angle amount in degrees to skew.
879
+ * @param angleY The angle amount in degrees to skew.
880
+ * @return The resulted matrix
881
+ */
882
+ skew(angleX: number, angleY: number): CSSMatrix {
883
+ return Multiply(this, Skew(angleX, angleY));
884
+ }
885
+
886
+ /**
887
+ * Transforms a specified vector using the matrix, returning a new
888
+ * {x,y,z,w} Tuple *Object* comprising the transformed vector.
889
+ * Neither the matrix nor the original vector are altered.
890
+ *
891
+ * The method is equivalent with `transformPoint()` method
892
+ * of the `DOMMatrix` constructor.
893
+ *
894
+ * @param t Tuple with `{x,y,z,w}` components
895
+ * @return the resulting Tuple
896
+ */
897
+ transformPoint(t: PointTuple | DOMPoint): PointTuple | DOMPoint {
898
+ const x = this.m11 * t.x + this.m21 * t.y + this.m31 * t.z + this.m41 * t.w;
899
+ const y = this.m12 * t.x + this.m22 * t.y + this.m32 * t.z + this.m42 * t.w;
900
+ const z = this.m13 * t.x + this.m23 * t.y + this.m33 * t.z + this.m43 * t.w;
901
+ const w = this.m14 * t.x + this.m24 * t.y + this.m34 * t.z + this.m44 * t.w;
902
+
903
+ return t instanceof DOMPoint
904
+ ? new DOMPoint(x, y, z, w)
905
+ : {
906
+ x,
907
+ y,
908
+ z,
909
+ w,
910
+ };
911
+ }
912
+ }