2点之间的动画路径,始终在SVG React native,animated3中更改

xbp102n0  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(50)

在svgMap上有2个像素点,我可以很容易地在这2个点之间画一条线或路径。我想在2秒内动画绘制路径。
这是我的代码

const [pointAPixels, setPointAPixels] = useState({ x: 0, y: 0 });
const [pointBPixels, setPointBPixels] = useState({ x: 0, y: 0 });

const AnimatedPath = Animated.createAnimatedComponent(Path);
const animationProgress = useSharedValue(0);

useEffect(() => {
animationProgress.value = 0;
            animationProgress.value = withTiming(1, {
                duration: 1500,
                easing: Easing.linear,
                // useNativeDriver: false, // Enlevez cette ligne si elle n'est pas nécessaire
            });
}, [pointAGPS, pointBGPS]);

const animatedProps = useAnimatedProps(() => {
        const length = interpolate(animationProgress.value, [0, 1], [100, 0]);

        console.log(length, animationProgress.value);
        return {
strokeDasharray: "10",
            strokeDashoffset: length,
};
    });

return (
<View>
<SvgFrance width={320} height={320} fill={black} />
<AnimatedPath
                        d={`M ${pointAPixels.x} ${pointAPixels.y} L ${pointBPixels.x} ${pointBPixels.y}`}
                        // d={pathData}
                        stroke="black"
                        strokeWidth="2"

                        // strokeOpacity={animationProgress}
                        // strokeDasharray="5" // Configurer ici la valeur de strokeDasharray
                        animatedProps={animatedProps}

                    />
</Svg>
</View>
);
}

字符串
我把最重要的代码贴上去了。
我经常和

strokeDasharray: "10",
        strokeDashoffset: length,


但我不知道路径的长度因为它总是在变...

lawou6xi

lawou6xi1#

我是这样解决的:

const animatedProps = useAnimatedProps(() => {
        const x = interpolate(animationProgress.value, [0, 1], [pointAPixels.x, pointBPixels.x]);
        const y = interpolate(animationProgress.value, [0, 1], [pointAPixels.y, pointBPixels.y]);
        // console.log(x, y, animationProgress.value);
        return {
            d: `M ${pointAPixels.x} ${pointAPixels.y} L ${x} ${y}`,
        };
    });

字符串

相关问题