Skip to content

Commit f5894db

Browse files
ShaMan123chearon
authored andcommitted
fix(DOMMatrix/DOMPoint): spec compatibility
1 parent b6b2dc7 commit f5894db

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This release notably changes to using N-API. 🎉
4545
* Fix TextMetrics type to include alphabeticBaseline, emHeightAscent, and emHeightDescent properties
4646
* Fix class properties should have defaults as standard js classes (#2390)
4747
* Fixed Exif orientation in JPEG files being ignored (#1670)
48+
* Align DOMMatrix/DOMPoint to spec by adding missing methods
4849

4950
2.11.2
5051
==================

index.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,13 @@ export class DOMPoint {
400400
x: number;
401401
y: number;
402402
z: number;
403+
matrixTransform(matrix?: DOMMatrixInit): DOMPoint;
404+
toJSON(): any;
405+
static fromPoint(other?: DOMPointInit): DOMPoint;
403406
}
404407

405408
export class DOMMatrix {
406-
constructor(init: string | number[]);
409+
constructor(init?: string | number[]);
407410
toString(): string;
408411
multiply(other?: DOMMatrix): DOMMatrix;
409412
multiplySelf(other?: DOMMatrix): DOMMatrix;
@@ -414,6 +417,10 @@ export class DOMMatrix {
414417
scale3d(scale?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
415418
scale3dSelf(scale?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
416419
scaleSelf(scaleX?: number, scaleY?: number, scaleZ?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
420+
/**
421+
* @deprecated
422+
*/
423+
scaleNonUniform(scaleX?: number, scaleY?: number): DOMMatrix;
417424
rotateFromVector(x?: number, y?: number): DOMMatrix;
418425
rotateFromVectorSelf(x?: number, y?: number): DOMMatrix;
419426
rotate(rotX?: number, rotY?: number, rotZ?: number): DOMMatrix;
@@ -430,6 +437,7 @@ export class DOMMatrix {
430437
invertSelf(): DOMMatrix;
431438
setMatrixValue(transformList: string): DOMMatrix;
432439
transformPoint(point?: DOMPoint): DOMPoint;
440+
toJSON(): any;
433441
toFloat32Array(): Float32Array;
434442
toFloat64Array(): Float64Array;
435443
readonly is2D: boolean;

lib/DOMMatrix.js

+56
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ class DOMPoint {
1717
this.z = typeof z === 'number' ? z : 0
1818
this.w = typeof w === 'number' ? w : 1
1919
}
20+
21+
matrixTransform(init) {
22+
const m = init instanceof DOMMatrix ? init : new DOMMatrix(init)
23+
return m.transformPoint(this)
24+
}
25+
26+
toJSON() {
27+
return {
28+
x: this.x,
29+
y: this.y,
30+
z: this.z,
31+
w: this.w
32+
}
33+
}
34+
35+
static fromPoint(other) {
36+
return new this(other.x, other.y, other.z, other.w)
37+
}
2038
}
2139

2240
// Constants to index into _values (col-major)
@@ -163,6 +181,13 @@ class DOMMatrix {
163181
return this.scaleSelf(scale, scale, scale, originX, originY, originZ)
164182
}
165183

184+
/**
185+
* @deprecated
186+
*/
187+
scaleNonUniform(scaleX, scaleY) {
188+
return this.scale(scaleX, scaleY)
189+
}
190+
166191
scaleSelf (scaleX, scaleY, scaleZ, originX, originY, originZ) {
167192
// Not redundant with translate's checks because we need to negate the values later.
168193
if (typeof originX !== 'number') originX = 0
@@ -587,6 +612,37 @@ Object.defineProperties(DOMMatrix.prototype, {
587612
values[M31] === 0 && values[M32] === 0 && values[M33] === 1 && values[M34] === 0 &&
588613
values[M41] === 0 && values[M42] === 0 && values[M43] === 0 && values[M44] === 1)
589614
}
615+
},
616+
617+
toJSON: {
618+
value() {
619+
return {
620+
a: this.a,
621+
b: this.b,
622+
c: this.c,
623+
d: this.d,
624+
e: this.e,
625+
f: this.f,
626+
m11: this.m11,
627+
m12: this.m12,
628+
m13: this.m13,
629+
m14: this.m14,
630+
m21: this.m21,
631+
m22: this.m22,
632+
m23: this.m23,
633+
m23: this.m23,
634+
m31: this.m31,
635+
m32: this.m32,
636+
m33: this.m33,
637+
m34: this.m34,
638+
m41: this.m41,
639+
m42: this.m42,
640+
m43: this.m43,
641+
m44: this.m44,
642+
is2D: this.is2D,
643+
isIdentity: this.isIdentity,
644+
}
645+
}
590646
}
591647
})
592648

test/dommatrix.test.js

+64
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,68 @@ describe('DOMMatrix', function () {
586586
'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1)')
587587
})
588588
})
589+
590+
describe('toJSON', function () {
591+
it('works, 2d', function () {
592+
const x = new DOMMatrix()
593+
assert.deepStrictEqual(x.toJSON(), {
594+
a: 1,
595+
b: 0,
596+
c: 0,
597+
d: 1,
598+
e: 0,
599+
f: 0,
600+
m11: 1,
601+
m12: 0,
602+
m13: 0,
603+
m14: 0,
604+
m21: 0,
605+
m22: 1,
606+
m23: 0,
607+
m23: 0,
608+
m31: 0,
609+
m32: 0,
610+
m33: 1,
611+
m34: 0,
612+
m41: 0,
613+
m42: 0,
614+
m43: 0,
615+
m44: 1,
616+
is2D: true,
617+
isIdentity: true,
618+
})
619+
})
620+
621+
it('works, 3d', function () {
622+
const x = new DOMMatrix()
623+
x.m31 = 1
624+
assert.equal(x.is2D, false)
625+
assert.deepStrictEqual(x.toJSON(), {
626+
a: 1,
627+
b: 0,
628+
c: 0,
629+
d: 1,
630+
e: 0,
631+
f: 0,
632+
m11: 1,
633+
m12: 0,
634+
m13: 0,
635+
m14: 0,
636+
m21: 0,
637+
m22: 1,
638+
m23: 0,
639+
m23: 0,
640+
m31: 1,
641+
m32: 0,
642+
m33: 1,
643+
m34: 0,
644+
m41: 0,
645+
m42: 0,
646+
m43: 0,
647+
m44: 1,
648+
is2D: false,
649+
isIdentity: false,
650+
})
651+
})
652+
})
589653
})

0 commit comments

Comments
 (0)