@thednp/dommatrix 3.0.5 → 3.1.0

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.
@@ -1,6 +1,6 @@
1
1
  //#region src/index.ts
2
- /** A model for JSONMatrix */
3
- const JSON_MATRIX = {
2
+ /** The property names of a JSONMatrix, used for compatibility checks */
3
+ const JSON_KEYS = Object.keys({
4
4
  a: 1,
5
5
  b: 0,
6
6
  c: 0,
@@ -25,14 +25,14 @@ const JSON_MATRIX = {
25
25
  m44: 1,
26
26
  is2D: true,
27
27
  isIdentity: true
28
- };
28
+ });
29
29
  /** Checks if an array is compatible with CSSMatrix */
30
30
  const isCompatibleArray = (array) => {
31
31
  return (array instanceof Float64Array || array instanceof Float32Array || Array.isArray(array) && array.every((x) => typeof x === "number")) && [6, 16].some((x) => array.length === x);
32
32
  };
33
33
  /** Checks if an object is compatible with CSSMatrix */
34
34
  const isCompatibleObject = (object) => {
35
- return typeof DOMMatrix !== "undefined" && object instanceof DOMMatrix || object instanceof CSSMatrix || typeof object === "object" && Object.keys(JSON_MATRIX).every((k) => object && k in object);
35
+ return typeof DOMMatrix !== "undefined" && object instanceof DOMMatrix || object instanceof CSSMatrix || typeof object === "object" && JSON_KEYS.every((k) => object && k in object);
36
36
  };
37
37
  /**
38
38
  * Creates a new mutable `CSSMatrix` instance given an array of 16/6 floating point values.
@@ -45,49 +45,62 @@ const isCompatibleObject = (object) => {
45
45
  * @return the resulted matrix.
46
46
  */
47
47
  const fromArray = (array) => {
48
- const m = new CSSMatrix();
49
- const a = Array.from(array);
50
- if (!isCompatibleArray(a)) throw TypeError(`CSSMatrix: "${a.join(",")}" must be an array with 6/16 numbers.`);
48
+ if (!isCompatibleArray(array)) throw TypeError(`CSSMatrix: "${Array.from(array).join(",")}" must be an array with 6/16 numbers.`);
51
49
  // istanbul ignore else @preserve
52
- if (a.length === 16) {
53
- const [m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44] = a;
54
- m.m11 = m11;
55
- m.a = m11;
56
- m.m21 = m21;
57
- m.c = m21;
58
- m.m31 = m31;
59
- m.m41 = m41;
60
- m.e = m41;
61
- m.m12 = m12;
62
- m.b = m12;
63
- m.m22 = m22;
64
- m.d = m22;
65
- m.m32 = m32;
66
- m.m42 = m42;
67
- m.f = m42;
68
- m.m13 = m13;
69
- m.m23 = m23;
70
- m.m33 = m33;
71
- m.m43 = m43;
72
- m.m14 = m14;
73
- m.m24 = m24;
74
- m.m34 = m34;
75
- m.m44 = m44;
76
- } else if (a.length === 6) {
77
- const [M11, M12, M21, M22, M41, M42] = a;
78
- m.m11 = M11;
79
- m.a = M11;
80
- m.m12 = M12;
81
- m.b = M12;
82
- m.m21 = M21;
83
- m.c = M21;
84
- m.m22 = M22;
85
- m.d = M22;
86
- m.m41 = M41;
87
- m.e = M41;
88
- m.m42 = M42;
89
- m.f = M42;
50
+ if (array.length === 16) {
51
+ const [m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44] = array;
52
+ return fromValues(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
90
53
  }
54
+ const [M11, M12, M21, M22, M41, M42] = array;
55
+ return fromValues(M11, M12, 0, 0, M21, M22, 0, 0, 0, 0, 1, 0, M41, M42, 0, 1);
56
+ };
57
+ /**
58
+ * Creates a new mutable `CSSMatrix` instance given the 16 values of the matrix.
59
+ * This internal helper skips the validation and intermediate array steps of
60
+ * `fromArray` and is used by the fast paths of the library.
61
+ *
62
+ * @param m11 the `m11` value.
63
+ * @param m12 the `m12` value.
64
+ * @param m13 the `m13` value.
65
+ * @param m14 the `m14` value.
66
+ * @param m21 the `m21` value.
67
+ * @param m22 the `m22` value.
68
+ * @param m23 the `m23` value.
69
+ * @param m24 the `m24` value.
70
+ * @param m31 the `m31` value.
71
+ * @param m32 the `m32` value.
72
+ * @param m33 the `m33` value.
73
+ * @param m34 the `m34` value.
74
+ * @param m41 the `m41` value.
75
+ * @param m42 the `m42` value.
76
+ * @param m43 the `m43` value.
77
+ * @param m44 the `m44` value.
78
+ * @return the resulted matrix.
79
+ */
80
+ const fromValues = (m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44) => {
81
+ const m = new CSSMatrix();
82
+ m.m11 = m11;
83
+ m.a = m11;
84
+ m.m21 = m21;
85
+ m.c = m21;
86
+ m.m31 = m31;
87
+ m.m41 = m41;
88
+ m.e = m41;
89
+ m.m12 = m12;
90
+ m.b = m12;
91
+ m.m22 = m22;
92
+ m.d = m22;
93
+ m.m32 = m32;
94
+ m.m42 = m42;
95
+ m.f = m42;
96
+ m.m13 = m13;
97
+ m.m23 = m23;
98
+ m.m33 = m33;
99
+ m.m43 = m43;
100
+ m.m14 = m14;
101
+ m.m24 = m24;
102
+ m.m34 = m34;
103
+ m.m44 = m44;
91
104
  return m;
92
105
  };
93
106
  /**
@@ -98,24 +111,7 @@ const fromArray = (array) => {
98
111
  * @return the resulted matrix.
99
112
  */
100
113
  const fromMatrix = (m) => {
101
- if (isCompatibleObject(m)) return fromArray([
102
- m.m11,
103
- m.m12,
104
- m.m13,
105
- m.m14,
106
- m.m21,
107
- m.m22,
108
- m.m23,
109
- m.m24,
110
- m.m31,
111
- m.m32,
112
- m.m33,
113
- m.m34,
114
- m.m41,
115
- m.m42,
116
- m.m43,
117
- m.m44
118
- ]);
114
+ if (isCompatibleObject(m)) return fromValues(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);
119
115
  throw TypeError(`CSSMatrix: "${JSON.stringify(m)}" is not a DOMMatrix / CSSMatrix / JSON compatible object.`);
120
116
  };
121
117
  /**
@@ -136,8 +132,13 @@ const fromString = (source) => {
136
132
  const str = String(source).replace(/\s/g, "");
137
133
  const m = new CSSMatrix();
138
134
  const invalidStringError = `CSSMatrix: invalid transform string "${source}"`;
139
- str.split(")").filter((f) => f).forEach((tf) => {
140
- const [prop, value] = tf.split("(");
135
+ const transformFn = /([\w-]+)\(([^)]*)\)/g;
136
+ let consumed = 0;
137
+ let match;
138
+ while (match = transformFn.exec(str)) {
139
+ const prop = match[1];
140
+ const value = match[2];
141
+ consumed += match[0].length;
141
142
  if (!value) throw TypeError(invalidStringError);
142
143
  const components = value.split(",").map((n) => n.includes("rad") ? parseFloat(n) * (180 / Math.PI) : parseFloat(n));
143
144
  const [x, y, z, a] = components;
@@ -189,7 +190,8 @@ const fromString = (source) => {
189
190
  m[method](...axeValues);
190
191
  }
191
192
  else throw TypeError(invalidStringError);
192
- });
193
+ }
194
+ if (consumed !== str.length) throw TypeError(invalidStringError);
193
195
  return m;
194
196
  };
195
197
  /**
@@ -407,14 +409,16 @@ const SkewY = (angle) => {
407
409
  return Skew(0, angle);
408
410
  };
409
411
  /**
410
- * Creates a new `CSSMatrix` resulted from the multiplication of two matrixes
411
- * and returns it. Both matrixes are not changed.
412
+ * Computes the multiplication of two matrixes and stores the result into the
413
+ * third matrix argument, which is also returned. Both source matrixes are
414
+ * not changed.
412
415
  *
413
416
  * @param m1 the first matrix.
414
417
  * @param m2 the second matrix.
418
+ * @param m the matrix to store the result into.
415
419
  * @return the resulted matrix.
416
420
  */
417
- const Multiply = (m1, m2) => {
421
+ const multiplyInto = (m1, m2, m) => {
418
422
  const m11 = m2.m11 * m1.m11 + m2.m12 * m1.m21 + m2.m13 * m1.m31 + m2.m14 * m1.m41;
419
423
  const m12 = m2.m11 * m1.m12 + m2.m12 * m1.m22 + m2.m13 * m1.m32 + m2.m14 * m1.m42;
420
424
  const m13 = m2.m11 * m1.m13 + m2.m12 * m1.m23 + m2.m13 * m1.m33 + m2.m14 * m1.m43;
@@ -431,24 +435,40 @@ const Multiply = (m1, m2) => {
431
435
  const m42 = m2.m41 * m1.m12 + m2.m42 * m1.m22 + m2.m43 * m1.m32 + m2.m44 * m1.m42;
432
436
  const m43 = m2.m41 * m1.m13 + m2.m42 * m1.m23 + m2.m43 * m1.m33 + m2.m44 * m1.m43;
433
437
  const m44 = m2.m41 * m1.m14 + m2.m42 * m1.m24 + m2.m43 * m1.m34 + m2.m44 * m1.m44;
434
- return fromArray([
435
- m11,
436
- m12,
437
- m13,
438
- m14,
439
- m21,
440
- m22,
441
- m23,
442
- m24,
443
- m31,
444
- m32,
445
- m33,
446
- m34,
447
- m41,
448
- m42,
449
- m43,
450
- m44
451
- ]);
438
+ m.m11 = m11;
439
+ m.a = m11;
440
+ m.m21 = m21;
441
+ m.c = m21;
442
+ m.m31 = m31;
443
+ m.m41 = m41;
444
+ m.e = m41;
445
+ m.m12 = m12;
446
+ m.b = m12;
447
+ m.m22 = m22;
448
+ m.d = m22;
449
+ m.m32 = m32;
450
+ m.m42 = m42;
451
+ m.f = m42;
452
+ m.m13 = m13;
453
+ m.m23 = m23;
454
+ m.m33 = m33;
455
+ m.m43 = m43;
456
+ m.m14 = m14;
457
+ m.m24 = m24;
458
+ m.m34 = m34;
459
+ m.m44 = m44;
460
+ return m;
461
+ };
462
+ /**
463
+ * Creates a new `CSSMatrix` resulted from the multiplication of two matrixes
464
+ * and returns it. Both matrixes are not changed.
465
+ *
466
+ * @param m1 the first matrix.
467
+ * @param m2 the second matrix.
468
+ * @return the resulted matrix.
469
+ */
470
+ const Multiply = (m1, m2) => {
471
+ return multiplyInto(m1, m2, new CSSMatrix());
452
472
  };
453
473
  /**
454
474
  * Creates and returns a new `DOMMatrix` compatible instance
@@ -497,6 +517,11 @@ var CSSMatrix = class {
497
517
  * * a 6/16 elements *Array*.
498
518
  */
499
519
  constructor(init) {
520
+ if (init) {
521
+ if (typeof init === "string" && init.length && init !== "none") return fromString(init);
522
+ if (Array.isArray(init) || init instanceof Float64Array || init instanceof Float32Array) return fromArray(init);
523
+ if (typeof init === "object") return fromMatrix(init);
524
+ }
500
525
  this.a = 1;
501
526
  this.b = 0;
502
527
  this.c = 0;
@@ -519,7 +544,6 @@ var CSSMatrix = class {
519
544
  this.m42 = 0;
520
545
  this.m43 = 0;
521
546
  this.m44 = 1;
522
- if (init) return this.setMatrixValue(init);
523
547
  return this;
524
548
  }
525
549
  /**
@@ -570,7 +594,31 @@ var CSSMatrix = class {
570
594
  * @return an *Array* representation of the matrix
571
595
  */
572
596
  toFloat32Array(is2D) {
573
- return Float32Array.from(toArray(this, is2D));
597
+ return is2D ? new Float32Array([
598
+ this.a,
599
+ this.b,
600
+ this.c,
601
+ this.d,
602
+ this.e,
603
+ this.f
604
+ ]) : new Float32Array([
605
+ this.m11,
606
+ this.m12,
607
+ this.m13,
608
+ this.m14,
609
+ this.m21,
610
+ this.m22,
611
+ this.m23,
612
+ this.m24,
613
+ this.m31,
614
+ this.m32,
615
+ this.m33,
616
+ this.m34,
617
+ this.m41,
618
+ this.m42,
619
+ this.m43,
620
+ this.m44
621
+ ]);
574
622
  }
575
623
  /**
576
624
  * Returns a *Float64Array* containing elements which comprise the matrix.
@@ -581,7 +629,31 @@ var CSSMatrix = class {
581
629
  * @return an *Array* representation of the matrix
582
630
  */
583
631
  toFloat64Array(is2D) {
584
- return Float64Array.from(toArray(this, is2D));
632
+ return is2D ? new Float64Array([
633
+ this.a,
634
+ this.b,
635
+ this.c,
636
+ this.d,
637
+ this.e,
638
+ this.f
639
+ ]) : new Float64Array([
640
+ this.m11,
641
+ this.m12,
642
+ this.m13,
643
+ this.m14,
644
+ this.m21,
645
+ this.m22,
646
+ this.m23,
647
+ this.m24,
648
+ this.m31,
649
+ this.m32,
650
+ this.m33,
651
+ this.m34,
652
+ this.m41,
653
+ this.m42,
654
+ this.m43,
655
+ this.m44
656
+ ]);
585
657
  }
586
658
  /**
587
659
  * Creates and returns a string representation of the matrix in `CSS` matrix syntax,
@@ -610,7 +682,28 @@ var CSSMatrix = class {
610
682
  toJSON() {
611
683
  const { is2D, isIdentity } = this;
612
684
  return {
613
- ...this,
685
+ a: this.a,
686
+ b: this.b,
687
+ c: this.c,
688
+ d: this.d,
689
+ e: this.e,
690
+ f: this.f,
691
+ m11: this.m11,
692
+ m12: this.m12,
693
+ m13: this.m13,
694
+ m14: this.m14,
695
+ m21: this.m21,
696
+ m22: this.m22,
697
+ m23: this.m23,
698
+ m24: this.m24,
699
+ m31: this.m31,
700
+ m32: this.m32,
701
+ m33: this.m33,
702
+ m34: this.m34,
703
+ m41: this.m41,
704
+ m42: this.m42,
705
+ m43: this.m43,
706
+ m44: this.m44,
614
707
  is2D,
615
708
  isIdentity
616
709
  };
@@ -638,7 +731,14 @@ var CSSMatrix = class {
638
731
  * @return The resulted matrix
639
732
  */
640
733
  translate(x, y, z) {
641
- return this.multiply(Translate(x, y ?? 0, z ?? 0));
734
+ const ty = y ?? 0;
735
+ const tz = z ?? 0;
736
+ const { m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44 } = this;
737
+ const n41 = x * m11 + ty * m21 + tz * m31 + m41;
738
+ const n42 = x * m12 + ty * m22 + tz * m32 + m42;
739
+ const n43 = x * m13 + ty * m23 + tz * m33 + m43;
740
+ const n44 = x * m14 + ty * m24 + tz * m34 + m44;
741
+ return fromValues(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, n41, n42, n43, n44);
642
742
  }
643
743
  /**
644
744
  * The scale method returns a new matrix which is this matrix post multiplied by
@@ -652,7 +752,10 @@ var CSSMatrix = class {
652
752
  * @return The resulted matrix
653
753
  */
654
754
  scale(x, y, z) {
655
- return this.multiply(Scale(x, y ?? x, z ?? 1));
755
+ const sy = y ?? x;
756
+ const sz = z ?? 1;
757
+ const { m41, m42, m43, m44 } = this;
758
+ return fromValues(this.m11 * x, this.m12 * x, this.m13 * x, this.m14 * x, this.m21 * sy, this.m22 * sy, this.m23 * sy, this.m24 * sy, this.m31 * sz, this.m32 * sz, this.m33 * sz, this.m34 * sz, m41, m42, m43, m44);
656
759
  }
657
760
  /**
658
761
  * The rotate method returns a new matrix which is this matrix post multiplied
@@ -707,7 +810,7 @@ var CSSMatrix = class {
707
810
  * @return The resulted matrix
708
811
  */
709
812
  skewX(angle) {
710
- return this.multiply(SkewX(angle));
813
+ return this.skew(angle, 0);
711
814
  }
712
815
  /**
713
816
  * Specifies a skew transformation along the `y-axis` by the given angle.
@@ -717,7 +820,7 @@ var CSSMatrix = class {
717
820
  * @return The resulted matrix
718
821
  */
719
822
  skewY(angle) {
720
- return this.multiply(SkewY(angle));
823
+ return this.skew(0, angle);
721
824
  }
722
825
  /**
723
826
  * Specifies a skew transformation along both the `x-axis` and `y-axis`.
@@ -728,7 +831,10 @@ var CSSMatrix = class {
728
831
  * @return The resulted matrix
729
832
  */
730
833
  skew(angleX, angleY) {
731
- return this.multiply(Skew(angleX, angleY));
834
+ const tX = angleX ? Math.tan(angleX * Math.PI / 180) : 0;
835
+ const tY = angleY ? Math.tan(angleY * Math.PI / 180) : 0;
836
+ const { m11, m12, m13, m14, m21, m22, m23, m24 } = this;
837
+ return fromValues(m11 + tY * m21, m12 + tY * m22, m13 + tY * m23, m14 + tY * m24, tX * m11 + m21, tX * m12 + m22, tX * m13 + m23, tX * m14 + m24, this.m31, this.m32, this.m33, this.m34, this.m41, this.m42, this.m43, this.m44);
732
838
  }
733
839
  /**
734
840
  * Modifies the current matrix by post-multiplying it with another matrix.
@@ -738,8 +844,7 @@ var CSSMatrix = class {
738
844
  * @return this matrix (modified)
739
845
  */
740
846
  multiplySelf(m2) {
741
- const result = Multiply(this, m2);
742
- Object.assign(this, result);
847
+ multiplyInto(this, m2, this);
743
848
  return this;
744
849
  }
745
850
  /**
@@ -752,7 +857,20 @@ var CSSMatrix = class {
752
857
  * @return this matrix (modified)
753
858
  */
754
859
  translateSelf(x, y, z) {
755
- return this.multiplySelf(Translate(x, y ?? 0, z ?? 0));
860
+ const ty = y ?? 0;
861
+ const tz = z ?? 0;
862
+ const { m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44 } = this;
863
+ const n41 = x * m11 + ty * m21 + tz * m31 + m41;
864
+ const n42 = x * m12 + ty * m22 + tz * m32 + m42;
865
+ const n43 = x * m13 + ty * m23 + tz * m33 + m43;
866
+ const n44 = x * m14 + ty * m24 + tz * m34 + m44;
867
+ this.m41 = n41;
868
+ this.e = n41;
869
+ this.m42 = n42;
870
+ this.f = n42;
871
+ this.m43 = n43;
872
+ this.m44 = n44;
873
+ return this;
756
874
  }
757
875
  /**
758
876
  * Modifies the current matrix by post-multiplying it with a scale matrix.
@@ -764,7 +882,25 @@ var CSSMatrix = class {
764
882
  * @return this matrix (modified)
765
883
  */
766
884
  scaleSelf(x, y, z) {
767
- return this.multiplySelf(Scale(x, y ?? x, z ?? 1));
885
+ const sy = y ?? x;
886
+ const sz = z ?? 1;
887
+ this.m11 *= x;
888
+ this.a *= x;
889
+ this.m12 *= x;
890
+ this.b *= x;
891
+ this.m13 *= x;
892
+ this.m14 *= x;
893
+ this.m21 *= sy;
894
+ this.c *= sy;
895
+ this.m22 *= sy;
896
+ this.d *= sy;
897
+ this.m23 *= sy;
898
+ this.m24 *= sy;
899
+ this.m31 *= sz;
900
+ this.m32 *= sz;
901
+ this.m33 *= sz;
902
+ this.m34 *= sz;
903
+ return this;
768
904
  }
769
905
  /**
770
906
  * Modifies the current matrix by post-multiplying it with a rotation matrix.
@@ -815,7 +951,7 @@ var CSSMatrix = class {
815
951
  * @return this matrix (modified)
816
952
  */
817
953
  skewXSelf(angle) {
818
- return this.multiplySelf(SkewX(angle));
954
+ return this.skewSelf(angle, 0);
819
955
  }
820
956
  /**
821
957
  * Modifies the current matrix by post-multiplying it with a skewY matrix.
@@ -825,7 +961,7 @@ var CSSMatrix = class {
825
961
  * @return this matrix (modified)
826
962
  */
827
963
  skewYSelf(angle) {
828
- return this.multiplySelf(SkewY(angle));
964
+ return this.skewSelf(0, angle);
829
965
  }
830
966
  /**
831
967
  * Modifies the current matrix by post-multiplying it with a skew matrix.
@@ -836,7 +972,30 @@ var CSSMatrix = class {
836
972
  * @return this matrix (modified)
837
973
  */
838
974
  skewSelf(angleX, angleY) {
839
- return this.multiplySelf(Skew(angleX, angleY));
975
+ const tX = angleX ? Math.tan(angleX * Math.PI / 180) : 0;
976
+ const tY = angleY ? Math.tan(angleY * Math.PI / 180) : 0;
977
+ const { m11, m12, m13, m14, m21, m22, m23, m24 } = this;
978
+ const n11 = m11 + tY * m21;
979
+ const n12 = m12 + tY * m22;
980
+ const n13 = m13 + tY * m23;
981
+ const n14 = m14 + tY * m24;
982
+ const n21 = tX * m11 + m21;
983
+ const n22 = tX * m12 + m22;
984
+ const n23 = tX * m13 + m23;
985
+ const n24 = tX * m14 + m24;
986
+ this.m11 = n11;
987
+ this.a = n11;
988
+ this.m12 = n12;
989
+ this.b = n12;
990
+ this.m13 = n13;
991
+ this.m14 = n14;
992
+ this.m21 = n21;
993
+ this.c = n21;
994
+ this.m22 = n22;
995
+ this.d = n22;
996
+ this.m23 = n23;
997
+ this.m24 = n24;
998
+ return this;
840
999
  }
841
1000
  /**
842
1001
  * Transforms a specified vector using the matrix, returning a new