Skip to content

Commit

Permalink
feat(DOMMatrix): add toFloat32Array and toFloat64Array (#70)
Browse files Browse the repository at this point in the history
* feat(dommatrix): add toFloat32Array and toFloat64Array

* style: lint
  • Loading branch information
FeliciousX authored Sep 29, 2020
1 parent df7a9fb commit 340de22
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions __tests__/classes/DOMMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ describe('DOMMatrix class', () => {
expect(matrix).toBeInstanceOf(DOMMatrix);
});

it('should return Float32Array', () => {
const matrix32 = new DOMMatrix().toFloat32Array();
expect(matrix32).toBeInstanceOf(Float32Array);
expect(matrix32).toStrictEqual(
new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
);
});

it('should return Float64Array', () => {
const matrix64 = new DOMMatrix().toFloat64Array();
expect(matrix64).toBeInstanceOf(Float64Array);
expect(matrix64).toStrictEqual(
new Float64Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
);
});

it('should know if a 2d matrix is an identity matrix', () => {
const matrix = new DOMMatrix([1, 0, 0, 1, 0, 0]);
expect(matrix.isIdentity).toBeTruthy();
Expand Down
42 changes: 42 additions & 0 deletions src/classes/DOMMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,46 @@ export default class DOMMatrix {
get is2D() {
return this._is2D;
}

toFloat32Array() {
return new Float32Array([
this.m11,
this.m12,
this.m13,
this.m14,
this.m21,
this.m22,
this.m23,
this.m24,
this.m31,
this.m32,
this.m33,
this.m34,
this.m41,
this.m42,
this.m43,
this.m44,
]);
}

toFloat64Array() {
return new Float64Array([
this.m11,
this.m12,
this.m13,
this.m14,
this.m21,
this.m22,
this.m23,
this.m24,
this.m31,
this.m32,
this.m33,
this.m34,
this.m41,
this.m42,
this.m43,
this.m44,
]);
}
}

0 comments on commit 340de22

Please sign in to comment.