Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DOMMatrix): add toFloat32Array and toFloat64Array #70

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
]);
}
}