@thednp/dommatrix 2.0.8 → 2.0.10

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