Skip to content

Commit

Permalink
reviseFormat
Browse files Browse the repository at this point in the history
reviseFormat
  • Loading branch information
neverbiasu committed May 17, 2024
1 parent 1217e41 commit a057228
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/living/helpers/babylonjs/InteractiveDynamicTexture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import { describe, it, expect } from '@jest/globals'
describe('InteractiveDynamicTexture', () => {
it('should return correct matrix', () => {
const transformStr = "rotate(90deg) translateX(10px)"
const expectedMatrix = new DOMMatrix([0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 10, 0, 1]);
const expectedMatrix = new DOMMatrix([
0, 1, 0, 0,
-1, 0, 0, 0,
0, 0, 1, 0,
0, 10, 0, 1
]);
const result = InteractiveDynamicTexture._parserTransform(transformStr);

expect(result).toEqual(expectedMatrix);
})
it('should return identity matrix when no transform is applied', () => {
const transformStr = " "
const expectedMatrix = new DOMMatrix([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
const expectedMatrix = new DOMMatrix([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
const result = InteractiveDynamicTexture._parserTransform(transformStr);

expect(result).toEqual(expectedMatrix);
Expand Down
21 changes: 18 additions & 3 deletions src/living/helpers/babylonjs/InteractiveDynamicTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,34 @@ export class InteractiveDynamicTexture extends BABYLON.DynamicTexture {
value: match[2],
unit: match[3],
}));
let matrix: DOMMatrix = new DOMMatrixImpl([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
let matrix: DOMMatrix = new DOMMatrixImpl([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
transforms.forEach(transform => {
if (transform.type === 'translateX') {
const x = parseFloat(transform.value);
const translateMatrix: DOMMatrix = new DOMMatrixImpl([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, 0, 0, 1]);
const translateMatrix: DOMMatrix = new DOMMatrixImpl([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, 0, 0, 1
]);
matrix = postMultiply(matrix, translateMatrix);
}

if (transform.type === 'rotate') {
const angle = parseFloat(transform.value);
const cosValue = Number(Math.cos(angle * Math.PI / 180).toFixed(2));
const sinValue = Number(Math.sin(angle * Math.PI / 180).toFixed(2));
const rotateMatrix: DOMMatrix = new DOMMatrixImpl([cosValue, sinValue, 0, 0, -sinValue, cosValue, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
const rotateMatrix: DOMMatrix = new DOMMatrixImpl([
cosValue, sinValue, 0, 0,
-sinValue, cosValue, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
matrix = postMultiply(matrix, rotateMatrix) as DOMMatrixImpl;
}
});
Expand Down

0 comments on commit a057228

Please sign in to comment.