Skip to content

Commit 8ae339b

Browse files
committed
Accept Matrix4 in path.transform
1 parent c7ef69e commit 8ae339b

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

package/src/renderer/__tests__/e2e/Matrix4.spec.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@ import {
99
processTransform3d,
1010
rotateX,
1111
translate,
12+
toMatrix3,
1213
} from "../../../skia/types";
1314

14-
/**
15-
* @worklet
16-
*/
17-
export const toCKMatrix3 = (m: number[]) => {
18-
"worklet";
19-
return [m[0], m[1], m[3], m[4], m[5], m[7], m[12], m[13], m[15]];
20-
};
21-
2215
const ckPerspective = (d: number) => [
2316
1,
2417
0,
@@ -43,7 +36,7 @@ const concat = (a: number[], b: number[]) => CanvasKit.M44.multiply(a, b);
4336
describe("Matrix4", () => {
4437
it("should be a row major matix", () => {
4538
const m4 = CanvasKit.M44.identity();
46-
expect(processTransform3d([])).toEqual(toCKMatrix3(m4));
39+
expect(processTransform3d([])).toEqual(m4);
4740
});
4841
it("should match identity matrix", () => {
4942
const m4 = CanvasKit.M44.identity();
@@ -54,7 +47,7 @@ describe("Matrix4", () => {
5447
{ translateX: -256 / 2 },
5548
{ translateY: -256 / 2 },
5649
])
57-
).toEqual(toCKMatrix3(m4));
50+
).toEqual(m4);
5851
});
5952
it("Identity should match identity matrix", () => {
6053
const m4 = CanvasKit.M44.identity();
@@ -65,7 +58,7 @@ describe("Matrix4", () => {
6558
{ translateX: -256 / 2 },
6659
{ translateY: -256 / 2 },
6760
])
68-
).toEqual(toCKMatrix3(m4));
61+
).toEqual(m4);
6962
});
7063
it("should match perspective matrix", () => {
7164
let m4 = CanvasKit.M44.identity();
@@ -80,15 +73,15 @@ describe("Matrix4", () => {
8073
{ translateX: -256 / 2 },
8174
{ translateY: -256 / 2 },
8275
])
83-
).toEqual(toCKMatrix3(m4));
76+
).toEqual(m4);
8477
});
8578
it("should match rotation matrix (1)", () => {
8679
let m4 = CanvasKit.M44.identity();
8780
m4 = concat(m4, CanvasKit.M44.rotated([1, 0, 0], 1));
88-
expect(processTransform3d([{ rotateX: 1 }])).toEqual(toCKMatrix3(m4));
81+
expect(processTransform3d([{ rotateX: 1 }])).toEqual(m4);
8982
m4 = CanvasKit.M44.identity();
9083
m4 = concat(m4, CanvasKit.M44.rotated([0, 1, 0], Math.PI));
91-
expect(processTransform3d([{ rotateY: Math.PI }])).toEqual(toCKMatrix3(m4));
84+
expect(processTransform3d([{ rotateY: Math.PI }])).toEqual(m4);
9285
});
9386
it("should match rotation matrix (2)", () => {
9487
let m4 = CanvasKit.M44.identity();
@@ -103,7 +96,7 @@ describe("Matrix4", () => {
10396
{ translateX: -256 / 2 },
10497
{ translateY: -256 / 2 },
10598
])
106-
).toEqual(toCKMatrix3(m4));
99+
).toEqual(m4);
107100
});
108101
it("Should do a perspective transformation (1)", async () => {
109102
const { width, height } = surface;
@@ -221,12 +214,14 @@ describe("Matrix4", () => {
221214
);
222215
// eslint-disable-next-line @typescript-eslint/no-explicit-any, jest/valid-expect
223216
(expect(result) as any).toBeApproximatelyEqual(
224-
processTransform3d([
225-
{ translate: [width / 2, height / 2] },
226-
{ perspective: 300 },
227-
{ rotateX: 1 },
228-
{ translate: [-width / 2, -height / 2] },
229-
]),
217+
toMatrix3(
218+
processTransform3d([
219+
{ translate: [width / 2, height / 2] },
220+
{ perspective: 300 },
221+
{ rotateX: 1 },
222+
{ translate: [-width / 2, -height / 2] },
223+
])
224+
),
230225
0.1
231226
);
232227
});

package/src/skia/types/Canvas.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { SkImageFilter } from "./ImageFilter";
1313
import type { SkVertices } from "./Vertices";
1414
import type { SkTextBlob } from "./TextBlob";
1515
import type { SkPicture } from "./Picture";
16+
import type { Matrix4, Matrix3 } from "./Matrix4";
1617

1718
export enum ClipOp {
1819
Difference,
@@ -485,7 +486,7 @@ export interface SkCanvas {
485486
* Replaces current matrix with m premultiplied with the existing matrix.
486487
* @param m
487488
*/
488-
concat(m: SkMatrix | number[]): void;
489+
concat(m: SkMatrix | number[] | Matrix4 | Matrix3): void;
489490

490491
/**
491492
* Draws the given picture using the current clip, current matrix, and the provided paint.

package/src/skia/types/Matrix4.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ export const rotateY = (value: number, p?: Point) => {
278278
*/
279279
export const processTransform3d = (transforms: Transforms3d) => {
280280
"worklet";
281-
return toMatrix3(
282-
transforms.reduce((acc, val) => {
281+
return transforms.reduce((acc, val) => {
283282
const key = Object.keys(val)[0] as Transform3dName;
284283
const transform = val as Pick<Transformations, typeof key>;
285284
if (key === "translateX") {
@@ -339,6 +338,5 @@ export const processTransform3d = (transforms: Transforms3d) => {
339338
return multiply4(acc, value);
340339
}
341340
return exhaustiveCheck(key);
342-
}, Matrix4())
343-
);
341+
}, Matrix4());
344342
};

package/src/skia/types/Path/Path.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { SkRRect } from "../RRect";
44
import type { StrokeJoin, StrokeCap } from "../Paint";
55
import type { SkMatrix } from "../Matrix";
66
import type { SkJSIInstance } from "../JsiInstance";
7+
import { Matrix3, Matrix4 } from "../Matrix4";
78

89
/**
910
* Options used for Path.stroke(). If an option is omitted, a sensible default will be used.
@@ -540,7 +541,7 @@ export interface SkPath extends SkJSIInstance<"Path"> {
540541
/**
541542
* Transforms the path by the specified matrix.
542543
*/
543-
transform(m3: SkMatrix | number[]): SkPath;
544+
transform(m3: SkMatrix | number[] | Matrix3 | Matrix4): SkPath;
544545

545546
/**
546547
* Interpolates between Path with point array of equal size.

0 commit comments

Comments
 (0)