Shared values doesn't animate when using the same progress value as usePathInterpolation
Description
Hello,
I'm trying to animate the translation and opacity of a couple of Groups using the same progress value that I use to interpolate two paths. I noticed that when the interpolated path are used, the other props are not correctly animated. I've debugged into the useDerivedValue and it does update properly, but it doesn't propagate to the Group (or at least doesn't animate).
The workaround is to create another progress that runs in parallel to the first one. Is that the intended behaviour?
Version
0.1.216
Steps to reproduce
- Create an interpolated path using
usePathInterpolation. - Use the same progress value to animate the opacity of a group.
- See that path interpolation works but the other animation doesn't.
- Use a fixed path instead of the interpolated one.
- See that now the animation works as expected.
Snack, code example, screenshot, or link to a repository
type MenuFabProps = {
pointerEvents: 'none' | 'auto';
style: ViewStyle | ViewStyle[];
isBackButton: boolean;
color: string;
onPress: () => any;
};
const burgerTopPath = Skia.Path.MakeFromSVGString(
'M24 1.85C24 2.48513 23.4851 3 22.85 3H1.15C0.514873 3 0 2.48513 0 1.85V1.85C0 1.21487 0.514872 0.700001 1.15 0.700001H22.85C23.4851 0.700001 24 1.21487 24 1.85V1.85Z',
)!;
const burgerMidPath = Skia.Path.MakeFromSVGString(
'M16 1.85C16 2.48513 15.4851 3 14.85 3H1.15C0.514873 3 0 2.48513 0 1.85V1.85C0 1.21487 0.514872 0.700001 1.15 0.700001H14.85C15.4851 0.700001 16 1.21487 16 1.85V1.85Z',
)!;
const burgerBottomPath = Skia.Path.MakeFromSVGString(
'M10 1.85C10 2.48513 9.48513 3 8.85 3H1.15C0.514873 3 0 2.48513 0 1.85V1.85C0 1.21487 0.514872 0.700001 1.15 0.700001H8.85C9.48513 0.700001 10 1.21487 10 1.85V1.85Z',
)!;
const chevronTopPath = Skia.Path.MakeFromSVGString(
'M9.26886 0.782158C9.82425 1.21413 9.9243 2.01455 9.49233 2.56995L2.88556 11.0644C2.45359 11.6198 1.65317 11.7198 1.09777 11.2878V11.2878C0.542377 10.8559 0.442324 10.0554 0.874299 9.50005L7.48106 1.00563C7.91304 0.450236 8.71346 0.350184 9.26886 0.782158V0.782158Z',
)!;
const chevronBottomPath = Skia.Path.MakeFromSVGString(
'M1.00564 0.447497C1.56103 0.0155232 2.36145 0.115576 2.79343 0.670971L9.49234 9.28385C9.92431 9.83925 9.82426 10.6397 9.26886 11.0716V11.0716C8.71347 11.5036 7.91305 11.4036 7.48107 10.8482L0.782161 2.23529C0.350187 1.67989 0.45024 0.879471 1.00564 0.447497V0.447497Z',
)!;
const MenuFab = ({
pointerEvents,
style,
isBackButton,
color,
onPress,
}: MenuFabProps) => {
const progress = useSharedValue(0);
useEffect(() => {
progress.value = withSpring(isBackButton ? 0 : 1, {
stiffness: 0.5,
damping: 0.2,
mass: 0.01,
});
}, [isBackButton]);
const topPath = usePathInterpolation(
progress,
[0, 1],
[chevronBottomPath, burgerTopPath],
);
const bottomPath = usePathInterpolation(
progress,
[0, 1],
[chevronTopPath, burgerBottomPath],
);
const topTransform = useDerivedValue(() => [
{translateX: interpolate(progress.value, [0, 1], [21, 16])},
{translateY: interpolate(progress.value, [0, 1], [27, 20])},
]);
const bottomTransform = useDerivedValue(() => [
{translateX: interpolate(progress.value, [0, 1], [21, 16])},
{translateY: interpolate(progress.value, [0, 1], [17, 34])},
]);
const midOpacity = useDerivedValue(() =>
interpolate(progress.value, [0.5, 1], [0, 1], Extrapolation.CLAMP),
);
return (
<Canvas style={{width: FAB_SIZE, height: FAB_SIZE}}>
<Group transform={topTransform}>
<Path path={topPath} color={color} />
</Group>
<Group
transform={[{translateX: 16}, {translateY: 27}]}
opacity={midOpacity}
color={color}
>
<Path path={burgerMidPath} />
</Group>
<Group transform={bottomTransform}>
<Path path={bottomPath} color={color} />
</Group>
</Canvas>
);
};
The example appears to work as expected, this the code I have based on the example you provided:
/* eslint-disable max-len */
import {
Canvas,
Group,
Skia,
Path,
usePathInterpolation,
} from "@shopify/react-native-skia";
import React, { useEffect } from "react";
import { View, type ViewStyle } from "react-native";
import {
withSpring,
useDerivedValue,
interpolate,
Extrapolation,
useSharedValue,
withRepeat,
} from "react-native-reanimated";
const burgerTopPath = Skia.Path.MakeFromSVGString(
"M24 1.85C24 2.48513 23.4851 3 22.85 3H1.15C0.514873 3 0 2.48513 0 1.85V1.85C0 1.21487 0.514872 0.700001 1.15 0.700001H22.85C23.4851 0.700001 24 1.21487 24 1.85V1.85Z"
)!;
const burgerMidPath = Skia.Path.MakeFromSVGString(
"M16 1.85C16 2.48513 15.4851 3 14.85 3H1.15C0.514873 3 0 2.48513 0 1.85V1.85C0 1.21487 0.514872 0.700001 1.15 0.700001H14.85C15.4851 0.700001 16 1.21487 16 1.85V1.85Z"
)!;
const burgerBottomPath = Skia.Path.MakeFromSVGString(
"M10 1.85C10 2.48513 9.48513 3 8.85 3H1.15C0.514873 3 0 2.48513 0 1.85V1.85C0 1.21487 0.514872 0.700001 1.15 0.700001H8.85C9.48513 0.700001 10 1.21487 10 1.85V1.85Z"
)!;
const chevronTopPath = Skia.Path.MakeFromSVGString(
"M9.26886 0.782158C9.82425 1.21413 9.9243 2.01455 9.49233 2.56995L2.88556 11.0644C2.45359 11.6198 1.65317 11.7198 1.09777 11.2878V11.2878C0.542377 10.8559 0.442324 10.0554 0.874299 9.50005L7.48106 1.00563C7.91304 0.450236 8.71346 0.350184 9.26886 0.782158V0.782158Z"
)!;
const chevronBottomPath = Skia.Path.MakeFromSVGString(
"M1.00564 0.447497C1.56103 0.0155232 2.36145 0.115576 2.79343 0.670971L9.49234 9.28385C9.92431 9.83925 9.82426 10.6397 9.26886 11.0716V11.0716C8.71347 11.5036 7.91305 11.4036 7.48107 10.8482L0.782161 2.23529C0.350187 1.67989 0.45024 0.879471 1.00564 0.447497V0.447497Z"
)!;
export const Demo = () => {
const progress = useSharedValue(0);
useEffect(() => {
progress.value = withRepeat(
withSpring(1, {
stiffness: 0.5,
damping: 0.2,
mass: 0.01,
}),
-1,
true
);
}, [progress]);
const topPath = usePathInterpolation(
progress,
[0, 1],
[chevronBottomPath, burgerTopPath]
);
const bottomPath = usePathInterpolation(
progress,
[0, 1],
[chevronTopPath, burgerBottomPath]
);
const topTransform = useDerivedValue(() => [
{ translateX: interpolate(progress.value, [0, 1], [21, 16]) },
{ translateY: interpolate(progress.value, [0, 1], [27, 20]) },
]);
const bottomTransform = useDerivedValue(() => [
{ translateX: interpolate(progress.value, [0, 1], [21, 16]) },
{ translateY: interpolate(progress.value, [0, 1], [17, 34]) },
]);
const midOpacity = useDerivedValue(() =>
interpolate(progress.value, [0.5, 1], [0, 1], Extrapolation.CLAMP)
);
return (
<View style={{ flex: 1 }}>
<Canvas style={{ width: 200, height: 200 }}>
<Group transform={topTransform}>
<Path path={topPath} color={"red"} />
</Group>
<Group
transform={[{ translateX: 16 }, { translateY: 27 }]}
opacity={midOpacity}
color={"blue"}
>
<Path path={burgerMidPath} />
</Group>
<Group transform={bottomTransform}>
<Path path={bottomPath} color={"green"} />
</Group>
</Canvas>
</View>
);
};
Let me know if this doesn't produce the expected result, provide me with information on what the expected result would be and I will look at it again.
This is a small detail but if you can merge the top and bottom path as a single path, it will be slightly faster (here it doesn't make a difference but imagine you have tousands of such path animation on screen).
Yes, I'm aware that could be optimised, but in this case just wanted a quick way to implement it. Thanks anyway!
About the issue, I still manage to consistently reproduce it on my end (Android and RN 78.1). See the result I get when using the same progress.value in this gif, so no opacity change and no translation:
If I modify the code to use another parallel progress value, then works as expected (chevron arrow to hamburger menu icon conversion). In this case, I use progress.value for the paths, and secondProgress.value for derived values.
useEffect(() => {
progress.value = withSpring(isBackButton ? 0 : 1, {
stiffness: 0.5,
damping: 0.2,
mass: 0.01,
});
secondProgress.value = withSpring(isBackButton ? 0 : 1, {
stiffness: 0.5,
damping: 0.2,
mass: 0.01,
});
}, [isBackButton]);
This is a bit to follow. If you provide me an example that I can easily copy paste in my project (like the one I wrote above) and a description of the result you are seeing as well as a description of what would be the expected result, I would take a lot.
I've been investigating a bit more, and looks like is not a Skia problem but RN hot reload that messes it up. I only manage to reproduce it when editing that file, so not a big deal and that's why you couldn't reproduce. Thanks for your help!
@orca-adria this is interesting. This is the hook: https://github.com/Shopify/react-native-skia/blob/a55310a72a49dbdec0b5e6b013227fbfe2b4b4e8/package/src/external/reanimated/interpolators.ts#L39 Maybe if I were to add factory in the useEffect, it would be more robust with hot reload? Or I'm doing something wrong with memoization? This can probably be optimized. I'm not sure what are the rules for hot reload (doing it correctly for regular React is complicated enough 😅)