Skip to content

Commit 50bd198

Browse files
committed
usePathValue
1 parent 2b5fac8 commit 50bd198

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

example/src/Examples/FrostedCard/FrostedCard.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ import {
77
BackdropFilter,
88
Fill,
99
Blur,
10-
notifyChange,
10+
usePathValue,
1111
} from "@shopify/react-native-skia";
1212
import React from "react";
1313
import { Dimensions, View } from "react-native";
1414
import { Gesture, GestureDetector } from "react-native-gesture-handler";
15-
import {
16-
useDerivedValue,
17-
useSharedValue,
18-
withSpring,
19-
} from "react-native-reanimated";
15+
import { useSharedValue, withSpring } from "react-native-reanimated";
2016

2117
const { width, height } = Dimensions.get("window");
2218
const CARD_WIDTH = width * 0.9;
@@ -47,7 +43,6 @@ export const FrostedCard = () => {
4743
const image = useImage(require("./dynamo.jpg"));
4844
const rotateX = useSharedValue(0);
4945
const rotateY = useSharedValue(0);
50-
const path = useSharedValue(Skia.Path.Make());
5146

5247
const gesture = Gesture.Pan()
5348
.onChange((event) => {
@@ -59,10 +54,9 @@ export const FrostedCard = () => {
5954
rotateY.value = withSpring(0, springConfig(velocityX / sf));
6055
});
6156

62-
useDerivedValue(() => {
63-
path.value.reset();
64-
path.value.addRRect(rrct);
65-
path.value.transform(
57+
const clip = usePathValue((path) => {
58+
path.addRRect(rrct);
59+
path.transform(
6660
processTransform3d([
6761
{ translate: [width / 2, height / 2] },
6862
{ perspective: 300 },
@@ -71,7 +65,6 @@ export const FrostedCard = () => {
7165
{ translate: [-width / 2, -height / 2] },
7266
])
7367
);
74-
notifyChange(path);
7568
});
7669

7770
return (
@@ -86,7 +79,7 @@ export const FrostedCard = () => {
8679
height={height}
8780
fit="cover"
8881
/>
89-
<BackdropFilter filter={<Blur blur={30} mode="clamp" />} clip={path}>
82+
<BackdropFilter filter={<Blur blur={30} mode="clamp" />} clip={clip}>
9083
<Fill color="rgba(255, 255, 255, 0.1)" />
9184
</BackdropFilter>
9285
</Canvas>

package/src/external/reanimated/interpolators.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
useAnimatedReaction,
1414
useFrameCallback,
1515
useSharedValue,
16+
useDerivedValue
1617
} from "./moduleWrapper";
1718

1819
export const notifyChange = (value: SharedValue<unknown>) => {
@@ -23,6 +24,17 @@ export const notifyChange = (value: SharedValue<unknown>) => {
2324
}
2425
};
2526

27+
export const usePathValue = (cb: (path: SkPath) => void) => {
28+
const pathInit = useMemo(() => Skia.Path.Make(), []);
29+
const path = useSharedValue(pathInit);
30+
useDerivedValue(() => {
31+
path.value.reset();
32+
cb(path.value);
33+
notifyChange(path);
34+
});
35+
return path;
36+
};
37+
2638
export const useClock = () => {
2739
const clock = useSharedValue(0);
2840
const callback = useRef((info: FrameInfo) => {

package/src/external/reanimated/moduleWrapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import type { DependencyList } from "react";
33
import type {
4+
DerivedValue,
45
FrameCallback,
56
FrameInfo,
67
SharedValue,
@@ -42,6 +43,9 @@ export const useSharedValue: <T>(
4243
oneWayReadsOnly?: boolean
4344
) => SharedValue<T> = Reanimated2?.useSharedValue || throwOnMissingReanimated;
4445

46+
47+
export const useDerivedValue: <T>(processor: () => T,dependencies?: DependencyList) => DerivedValue<T> = Reanimated2?.useDerivedValue || throwOnMissingReanimated;
48+
4549
export const useFrameCallback: (
4650
callback: (frameInfo: FrameInfo) => void,
4751
autostart?: boolean

0 commit comments

Comments
 (0)