Skip to content

Commit 3154f31

Browse files
authored
Merge pull request #2059 from Shopify/web
Fix regression on RN Web
2 parents e34819b + 076f360 commit 3154f31

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

example/src/Examples/Stickers/GestureHandler.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,18 @@ import {
55
rotateZ,
66
scale,
77
translate,
8+
convertToColumnMajor,
9+
convertToAffineMatrix,
810
} from "@shopify/react-native-skia";
911
import React from "react";
12+
import { Platform } from "react-native";
1013
import { Gesture, GestureDetector } from "react-native-gesture-handler";
1114
import type { SharedValue } from "react-native-reanimated";
1215
import Animated, {
1316
useAnimatedStyle,
1417
useSharedValue,
1518
} from "react-native-reanimated";
1619

17-
const convertToColumnMajor = (rowMajorMatrix: Matrix4) => {
18-
"worklet";
19-
20-
const colMajorMatrix = new Array(16);
21-
const size = 4;
22-
for (let row = 0; row < size; row++) {
23-
for (let col = 0; col < size; col++) {
24-
colMajorMatrix[col * size + row] = rowMajorMatrix[row * size + col];
25-
}
26-
}
27-
return colMajorMatrix;
28-
};
29-
3020
interface GestureHandlerProps {
3121
matrix: SharedValue<Matrix4>;
3222
dimensions: SkRect;
@@ -68,6 +58,7 @@ export const GestureHandler = ({
6858
});
6959

7060
const style = useAnimatedStyle(() => {
61+
const m4 = convertToColumnMajor(matrix.value);
7162
return {
7263
position: "absolute",
7364
left: x,
@@ -79,7 +70,7 @@ export const GestureHandler = ({
7970
{ translateX: -width / 2 },
8071
{ translateY: -height / 2 },
8172
{
82-
matrix: convertToColumnMajor(matrix.value),
73+
matrix: Platform.OS === "web" ? convertToAffineMatrix(m4) : m4,
8374
},
8475
{ translateX: width / 2 },
8576
{ translateY: height / 2 },

package/src/renderer/Canvas.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import type {
1414
} from "react";
1515
import type { LayoutChangeEvent } from "react-native";
1616

17-
import { SkiaDomView, SkiaPictureView } from "../views";
17+
import { SkiaDomView } from "../views";
18+
import { SkiaDomView as SkiaDomViewWeb } from "../views/SkiaDomView.web";
1819
import { Skia } from "../skia/Skia";
1920
import type { TouchHandler, SkiaBaseViewProps } from "../views";
2021
import type { SkiaValue } from "../values/types";
21-
import { JsiDrawingContext } from "../dom/types";
2222

2323
import { SkiaRoot } from "./Reconciler";
2424
import { NATIVE_DOM } from "./HostComponents";
@@ -116,23 +116,16 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
116116
/>
117117
);
118118
} else {
119-
// This is for debugging
120-
const recorder = Skia.PictureRecorder();
121-
const canvas = recorder.beginRecording(
122-
Skia.XYWHRect(0, 0, 2_000_000, 2_000_000)
123-
);
124-
const ctx = new JsiDrawingContext(Skia, canvas);
125-
root.dom.render(ctx);
126-
const picture = recorder.finishRecordingAsPicture();
127119
return (
128-
<SkiaPictureView
120+
<SkiaDomViewWeb
129121
// eslint-disable-next-line @typescript-eslint/no-explicit-any
130122
ref={ref as any}
131123
style={style}
124+
root={root.dom}
125+
onTouch={onTouch}
126+
onLayout={onLayout}
132127
mode={mode}
133128
debug={debug}
134-
picture={picture}
135-
onLayout={onLayout}
136129
{...props}
137130
/>
138131
);

package/src/skia/types/Matrix4.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,36 @@ export const processTransform3d = (transforms: Transforms3d) => {
340340
return exhaustiveCheck(key);
341341
}, Matrix4());
342342
};
343+
344+
/**
345+
* @worklet
346+
*/
347+
export const convertToColumnMajor = (rowMajorMatrix: Matrix4) => {
348+
"worklet";
349+
350+
const colMajorMatrix = new Array<number>(16);
351+
const size = 4;
352+
for (let row = 0; row < size; row++) {
353+
for (let col = 0; col < size; col++) {
354+
colMajorMatrix[col * size + row] = rowMajorMatrix[row * size + col];
355+
}
356+
}
357+
return colMajorMatrix;
358+
};
359+
360+
/**
361+
* @worklet
362+
*/
363+
export const convertToAffineMatrix = (m4: number[]) => {
364+
"worklet";
365+
// Extracting the relevant components from the 4x4 matrix
366+
const a = m4[0]; // Scale X
367+
const b = m4[1]; // Skew Y
368+
const c = m4[4]; // Skew X
369+
const d = m4[5]; // Scale Y
370+
const tx = m4[12]; // Translate X
371+
const ty = m4[13]; // Translate Y
372+
373+
// Returning the 6-element affine transformation matrix
374+
return [a, b, c, d, tx, ty];
375+
};

0 commit comments

Comments
 (0)