Skip to content

Commit 1a2f4e5

Browse files
committed
Update example
1 parent ab3fbf0 commit 1a2f4e5

File tree

20 files changed

+175
-156
lines changed

20 files changed

+175
-156
lines changed

example/src/Examples/API/PathEffect.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
sub,
88
Canvas,
99
Circle,
10-
translate,
1110
Skia,
1211
PaintStyle,
1312
DiscretePathEffect,
@@ -22,6 +21,8 @@ import {
2221
processTransform2d,
2322
} from "@shopify/react-native-skia";
2423

24+
import { translate } from "../../components/Animations";
25+
2526
import { Title } from "./components/Title";
2627

2728
const path = Skia.Path.MakeFromSVGString(

example/src/Examples/API/components/drawings/backface.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
Group,
66
Path,
77
Rect,
8-
translate,
98
} from "@shopify/react-native-skia";
109

10+
import { translate } from "../../../../components/Animations";
11+
1112
const aspectRatio = 757 / 492;
1213
const center = { x: 492 / 2, y: 757 / 2 };
1314
export const CARD_WIDTH = 300;

example/src/Examples/Gooey/Gooey.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
Canvas,
66
Fill,
77
Skia,
8-
translate,
98
vec,
109
Group,
1110
PathOp,
@@ -25,6 +24,8 @@ import Animated, {
2524
} from "react-native-reanimated";
2625
import { Gesture, GestureDetector } from "react-native-gesture-handler";
2726

27+
import { translate } from "../../components/Animations";
28+
2829
import { Icon, R } from "./components/Icon";
2930
import { Hamburger } from "./components/Hamburger";
3031
import { BG, FG } from "./components/Theme";

example/src/Examples/Neumorphism/Dashboard/components/Button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
Group,
55
LinearGradient,
66
RadialGradient,
7-
translate,
87
vec,
98
} from "@shopify/react-native-skia";
109
import type { ReactNode } from "react";
1110
import React from "react";
1211

12+
import { translate } from "../../../../components/Animations";
13+
1314
export const BUTTON_SIZE = 62;
1415
const PADDING = 6;
1516

example/src/Examples/Neumorphism/Dashboard/components/Control.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
Shadow,
33
vec,
44
Group,
5-
translate,
65
Text,
76
Circle,
87
LinearGradient,
@@ -38,9 +37,9 @@ export const Control = ({
3837
if (font === null) {
3938
return null;
4039
}
41-
const labelWidth = font.getTextWidth(label);
40+
const labelWidth = font.measureText(label).width;
4241
return (
43-
<Group transform={translate({ x: x + 30, y: y + 30 })}>
42+
<Group transform={[{ translate: [x + 30, y + 30] }]}>
4443
<Text
4544
x={2 * r - labelWidth - 16}
4645
y={r + font.getSize() / 2}
@@ -68,7 +67,7 @@ export const Control = ({
6867
strokeWidth={1}
6968
/>
7069
</Group>
71-
<Group transform={translate({ x: r / 2, y: r / 2 })}>
70+
<Group transform={[{ translate: [r / 2, r / 2] }]}>
7271
<Group color="rgba(235, 235, 245, 0.6)">
7372
{active && (
7473
<LinearGradient

example/src/Examples/Neumorphism/Dashboard/components/ProgressBar.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
rrect,
55
Group,
66
LinearGradient,
7-
translate,
87
Circle,
98
Skia,
109
vec,
@@ -41,9 +40,9 @@ export const ProgressBar = ({ progress }: ProgressBarProps) => {
4140
if (font === null) {
4241
return null;
4342
}
44-
const textWidth = font.getTextWidth("00°C");
43+
const textWidth = font.measureText("00°C").width;
4544
return (
46-
<Group transform={translate({ x: 100, y: 223 })}>
45+
<Group transform={[{ translate: [100, 223] }]}>
4746
<Group>
4847
<LinearGradient
4948
start={vec(12, 12)}

example/src/Examples/Neumorphism/Dashboard/components/Slider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
rrect,
66
RoundedRect,
77
Group,
8-
translate,
98
LinearGradient,
109
vec,
1110
} from "@shopify/react-native-skia";
@@ -28,7 +27,7 @@ export const Slider = ({ x, y, progress }: SliderProps) => {
2827
[progress]
2928
);
3029
return (
31-
<Group transform={translate({ x, y })}>
30+
<Group transform={[{ translate: [x, y] }]}>
3231
<Box box={rrect(rect(0, 3.5, 192, 8), 25, 25)} color="#1B1B1D">
3332
<BoxShadow
3433
dx={-1.25}

example/src/Examples/Stickers/GestureHandler.tsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import type { SkMatrix, SkRect } from "@shopify/react-native-skia";
2-
import { Skia } from "@shopify/react-native-skia";
1+
import type { SkRect } from "@shopify/react-native-skia";
2+
import {
3+
Matrix4,
4+
multiply4,
5+
rotateZ,
6+
scale,
7+
translate,
8+
} from "@shopify/react-native-skia";
39
import React from "react";
410
import { Gesture, GestureDetector } from "react-native-gesture-handler";
511
import type { SharedValue } from "react-native-reanimated";
@@ -8,10 +14,21 @@ import Animated, {
814
useSharedValue,
915
} from "react-native-reanimated";
1016

11-
import { rotateZ, toM4, translate, scale } from "./MatrixHelpers";
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+
};
1229

1330
interface GestureHandlerProps {
14-
matrix: SharedValue<SkMatrix>;
31+
matrix: SharedValue<Matrix4>;
1532
dimensions: SkRect;
1633
debug?: boolean;
1734
}
@@ -23,30 +40,31 @@ export const GestureHandler = ({
2340
}: GestureHandlerProps) => {
2441
const { x, y, width, height } = dimensions;
2542
const origin = useSharedValue({ x: 0, y: 0 });
26-
const offset = useSharedValue(Skia.Matrix());
43+
const offset = useSharedValue(Matrix4());
2744

2845
const pan = Gesture.Pan().onChange((e) => {
29-
translate(matrix, e.changeX, e.changeY);
46+
matrix.value = multiply4(translate(e.changeX, e.changeY), matrix.value);
3047
});
3148

3249
const rotate = Gesture.Rotation()
3350
.onBegin((e) => {
3451
origin.value = { x: e.anchorX, y: e.anchorY };
35-
offset.value.identity();
36-
offset.value.concat(matrix.value);
52+
offset.value = matrix.value;
3753
})
3854
.onChange((e) => {
39-
rotateZ(matrix, offset.value, e.rotation, origin.value);
55+
matrix.value = multiply4(offset.value, rotateZ(e.rotation, origin.value));
4056
});
4157

4258
const pinch = Gesture.Pinch()
4359
.onBegin((e) => {
4460
origin.value = { x: e.focalX, y: e.focalY };
45-
offset.value.identity();
46-
offset.value.concat(matrix.value);
61+
offset.value = matrix.value;
4762
})
4863
.onChange((e) => {
49-
scale(matrix, offset.value, e.scale, origin.value);
64+
matrix.value = multiply4(
65+
offset.value,
66+
scale(e.scale, e.scale, 1, origin.value)
67+
);
5068
});
5169

5270
const style = useAnimatedStyle(() => {
@@ -61,7 +79,7 @@ export const GestureHandler = ({
6179
{ translateX: -width / 2 },
6280
{ translateY: -height / 2 },
6381
{
64-
matrix: toM4(matrix.value),
82+
matrix: convertToColumnMajor(matrix.value),
6583
},
6684
{ translateX: width / 2 },
6785
{ translateY: height / 2 },

example/src/Examples/Stickers/HelloSticker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-len */
2-
import type { SkMatrix } from "@shopify/react-native-skia";
2+
import type { Matrix4 } from "@shopify/react-native-skia";
33
import {
44
fitbox,
55
Group,
@@ -22,7 +22,7 @@ export const HelloStickerDimensions = rect(
2222
(300 * bounds.height) / bounds.width
2323
);
2424
interface HelloStickerProps {
25-
matrix: SharedValue<SkMatrix>;
25+
matrix: SharedValue<Matrix4>;
2626
}
2727

2828
export const HelloSticker = ({ matrix }: HelloStickerProps) => {

example/src/Examples/Stickers/LocationSticker.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-len */
22
import React from "react";
3-
import type { SkFont, SkMatrix } from "@shopify/react-native-skia";
3+
import type { Matrix4, SkFont } from "@shopify/react-native-skia";
44
import {
55
Skia,
66
vec,
@@ -11,8 +11,10 @@ import {
1111
Path,
1212
rect,
1313
Text,
14+
toMatrix3,
1415
} from "@shopify/react-native-skia";
1516
import type { SharedValue } from "react-native-reanimated";
17+
import { useDerivedValue } from "react-native-reanimated";
1618

1719
const x = 32;
1820
const y = 200;
@@ -25,7 +27,7 @@ const path = Skia.Path.MakeFromSVGString(
2527
const bounds = path.computeTightBounds();
2628

2729
interface LocationStickerProps {
28-
matrix: SharedValue<SkMatrix>;
30+
matrix: SharedValue<Matrix4>;
2931
font: SkFont;
3032
}
3133

0 commit comments

Comments
 (0)