Skip to content

Commit e34819b

Browse files
authored
Merge pull request #2056 from Shopify/blur-depth
Accept Matrix4 in path.transform
2 parents c7ef69e + 70f9333 commit e34819b

File tree

7 files changed

+91
-98
lines changed

7 files changed

+91
-98
lines changed

package/src/dom/types/Common.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import type { ReactNode } from "react";
33
import type {
44
BlendMode,
55
Color,
6-
Matrix3,
7-
Matrix4,
6+
InputMatrix,
87
PaintStyle,
9-
SkMatrix,
108
SkPaint,
119
SkPath,
1210
SkRect,
@@ -68,7 +66,7 @@ export type CircleDef = PointCircleDef | ScalarCircleDef;
6866
export interface TransformProps {
6967
transform?: Transforms3d;
7068
origin?: Vector;
71-
matrix?: SkMatrix | Matrix4 | Matrix3 | number[];
69+
matrix?: InputMatrix;
7270
}
7371

7472
export interface PaintProps extends ChildrenProps {

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SkColor } from "./Color";
88
import type { SkRRect } from "./RRect";
99
import type { BlendMode } from "./Paint/BlendMode";
1010
import type { SkPoint, PointMode } from "./Point";
11-
import type { SkMatrix } from "./Matrix";
11+
import type { InputMatrix } from "./Matrix";
1212
import type { SkImageFilter } from "./ImageFilter";
1313
import type { SkVertices } from "./Vertices";
1414
import type { SkTextBlob } from "./TextBlob";
@@ -485,7 +485,7 @@ export interface SkCanvas {
485485
* Replaces current matrix with m premultiplied with the existing matrix.
486486
* @param m
487487
*/
488-
concat(m: SkMatrix | number[]): void;
488+
concat(m: InputMatrix): void;
489489

490490
/**
491491
* Draws the given picture using the current clip, current matrix, and the provided paint.

package/src/skia/types/Matrix.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const isMatrix = (obj: unknown): obj is SkMatrix =>
77
obj !== null && (obj as SkJSIInstance<string>).__typename__ === "Matrix";
88

99
export interface SkMatrix extends SkJSIInstance<"Matrix"> {
10-
concat: (matrix: SkMatrix | Matrix4 | Matrix3 | number[]) => SkMatrix;
10+
concat: (matrix: InputMatrix) => SkMatrix;
1111
translate: (x: number, y: number) => SkMatrix;
1212
scale: (x: number, y?: number) => SkMatrix;
1313
skew: (x: number, y: number) => SkMatrix;
@@ -20,6 +20,8 @@ export interface SkMatrix extends SkJSIInstance<"Matrix"> {
2020
get: () => number[];
2121
}
2222

23+
export type InputMatrix = SkMatrix | Matrix3 | Matrix4 | number[];
24+
2325
export interface TransformProp {
2426
transform?: Transforms3d;
2527
}

package/src/skia/types/Matrix4.ts

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -278,67 +278,65 @@ export const rotateY = (value: number, p?: Point) => {
278278
*/
279279
export const processTransform3d = (transforms: Transforms3d) => {
280280
"worklet";
281-
return toMatrix3(
282-
transforms.reduce((acc, val) => {
283-
const key = Object.keys(val)[0] as Transform3dName;
284-
const transform = val as Pick<Transformations, typeof key>;
285-
if (key === "translateX") {
286-
const value = transform[key];
287-
return multiply4(acc, translate(value, 0, 0));
288-
}
289-
if (key === "translate") {
290-
const [x, y, z = 0] = transform[key];
291-
return multiply4(acc, translate(x, y, z));
292-
}
293-
if (key === "translateY") {
294-
const value = transform[key];
295-
return multiply4(acc, translate(0, value, 0));
296-
}
297-
if (key === "translateZ") {
298-
const value = transform[key];
299-
return multiply4(acc, translate(0, 0, value));
300-
}
301-
if (key === "scale") {
302-
const value = transform[key];
303-
return multiply4(acc, scale(value, value, 1));
304-
}
305-
if (key === "scaleX") {
306-
const value = transform[key];
307-
return multiply4(acc, scale(value, 1, 1));
308-
}
309-
if (key === "scaleY") {
310-
const value = transform[key];
311-
return multiply4(acc, scale(1, value, 1));
312-
}
313-
if (key === "skewX") {
314-
const value = transform[key];
315-
return multiply4(acc, skewX(value));
316-
}
317-
if (key === "skewY") {
318-
const value = transform[key];
319-
return multiply4(acc, skewY(value));
320-
}
321-
if (key === "rotateX") {
322-
const value = transform[key];
323-
return multiply4(acc, rotate([1, 0, 0], value));
324-
}
325-
if (key === "rotateY") {
326-
const value = transform[key];
327-
return multiply4(acc, rotate([0, 1, 0], value));
328-
}
329-
if (key === "perspective") {
330-
const value = transform[key];
331-
return multiply4(acc, perspective(value));
332-
}
333-
if (key === "rotate" || key === "rotateZ") {
334-
const value = transform[key];
335-
return multiply4(acc, rotate([0, 0, 1], value));
336-
}
337-
if (key === "matrix") {
338-
const value = transform[key];
339-
return multiply4(acc, value);
340-
}
341-
return exhaustiveCheck(key);
342-
}, Matrix4())
343-
);
281+
return transforms.reduce((acc, val) => {
282+
const key = Object.keys(val)[0] as Transform3dName;
283+
const transform = val as Pick<Transformations, typeof key>;
284+
if (key === "translateX") {
285+
const value = transform[key];
286+
return multiply4(acc, translate(value, 0, 0));
287+
}
288+
if (key === "translate") {
289+
const [x, y, z = 0] = transform[key];
290+
return multiply4(acc, translate(x, y, z));
291+
}
292+
if (key === "translateY") {
293+
const value = transform[key];
294+
return multiply4(acc, translate(0, value, 0));
295+
}
296+
if (key === "translateZ") {
297+
const value = transform[key];
298+
return multiply4(acc, translate(0, 0, value));
299+
}
300+
if (key === "scale") {
301+
const value = transform[key];
302+
return multiply4(acc, scale(value, value, 1));
303+
}
304+
if (key === "scaleX") {
305+
const value = transform[key];
306+
return multiply4(acc, scale(value, 1, 1));
307+
}
308+
if (key === "scaleY") {
309+
const value = transform[key];
310+
return multiply4(acc, scale(1, value, 1));
311+
}
312+
if (key === "skewX") {
313+
const value = transform[key];
314+
return multiply4(acc, skewX(value));
315+
}
316+
if (key === "skewY") {
317+
const value = transform[key];
318+
return multiply4(acc, skewY(value));
319+
}
320+
if (key === "rotateX") {
321+
const value = transform[key];
322+
return multiply4(acc, rotate([1, 0, 0], value));
323+
}
324+
if (key === "rotateY") {
325+
const value = transform[key];
326+
return multiply4(acc, rotate([0, 1, 0], value));
327+
}
328+
if (key === "perspective") {
329+
const value = transform[key];
330+
return multiply4(acc, perspective(value));
331+
}
332+
if (key === "rotate" || key === "rotateZ") {
333+
const value = transform[key];
334+
return multiply4(acc, rotate([0, 0, 1], value));
335+
}
336+
if (key === "matrix") {
337+
const value = transform[key];
338+
return multiply4(acc, value);
339+
}
340+
return exhaustiveCheck(key);
341+
}, Matrix4());
344342
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SkRect } from "../Rect";
22
import type { SkPoint } from "../Point";
33
import type { SkRRect } from "../RRect";
44
import type { StrokeJoin, StrokeCap } from "../Paint";
5-
import type { SkMatrix } from "../Matrix";
5+
import type { InputMatrix, SkMatrix } from "../Matrix";
66
import type { SkJSIInstance } from "../JsiInstance";
77

88
/**
@@ -540,7 +540,7 @@ export interface SkPath extends SkJSIInstance<"Path"> {
540540
/**
541541
* Transforms the path by the specified matrix.
542542
*/
543-
transform(m3: SkMatrix | number[]): SkPath;
543+
transform(m3: InputMatrix): SkPath;
544544

545545
/**
546546
* Interpolates between Path with point array of equal size.

package/src/skia/web/JsiSkMatrix.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { CanvasKit, Matrix3x3 } from "canvaskit-wasm";
22

3-
import { toMatrix3, type Matrix3, type Matrix4, type SkMatrix } from "../types";
3+
import { toMatrix3 } from "../types";
4+
import type { SkMatrix, InputMatrix, Matrix4 } from "../types";
45

56
import { HostObject } from "./Host";
67

7-
const isMatrixHostObject = (
8-
obj: SkMatrix | number[] | Matrix4 | Matrix3
9-
): obj is SkMatrix => !Array.isArray(obj);
8+
const isMatrixHostObject = (obj: InputMatrix): obj is SkMatrix =>
9+
!Array.isArray(obj);
1010

1111
export class JsiSkMatrix
1212
extends HostObject<Matrix3x3, "Matrix">
@@ -28,7 +28,7 @@ export class JsiSkMatrix
2828
// Do nothing - the matrix is represenetd by a Float32Array
2929
};
3030

31-
concat(matrix: SkMatrix | number[] | Matrix4 | Matrix3) {
31+
concat(matrix: InputMatrix) {
3232
this.preMultiply(
3333
// eslint-disable-next-line no-nested-ternary
3434
isMatrixHostObject(matrix)

0 commit comments

Comments
 (0)