As shown in the figure, how to make the green ball move along the black path? And it can apply animation effects such as React Native Reanimated with Spring.
In React Native Skia, I try to do things like this:
const progress = useSharedValue(0);
const geo = new PathGeometry(path);
const totalLength = geo.getTotalLength();
const transform = useDerivedValue(() => {
const currentLen = interpolate(progress.value, [0, 1], [0, totalLength]);
const pos = geo.getPointAtLength(currentLen);
return translate({x:pos.x,y:pos.y});
});
const handlePress = () => {
progress.value = withSpring(1);
};
But received an error message:
ReanimatedError: [Reanimated] Trying to access property getPointAtLength of an object which cannot be sent to the UI runtime. js engine: reanimated
Later, I used:
const geo = new PathGeometry(path);
const totalLength = geo.getTotalLength();
const pos = geo.getPointAtLength(0);
const posX = useSharedValue(pos.x);
const posY = useSharedValue(pos.y);
let i = 0;
setInterval(() => {
const nPos = geo.getPointAtLength(i);
posX.value = nPos.x;
posY.value = nPos.y;
i++;
}, 16);
It can work stiffly, but I know this is not a good solution because we cannot apply animation effects such as React Native Reanimated with Spring
May I know how to implement it? I am a beginnerenter image description here
YIWEI LIU is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.