首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为React-native-svg设置动画

为React-native-svg设置动画
EN

Stack Overflow用户
提问于 2020-09-17 16:41:07
回答 1查看 531关注 0票数 0

我正在尝试通过下面的代码使SVG具有动画效果:

代码语言:javascript
复制
//create animated path component

const AnimatedPath = Animated.createAnimatedComponent(Path);

  P1 = new Animated.ValueXY({x: 0, y: 1});
  P2 = new Animated.ValueXY({x: 1, y: 1});
  P3 = new Animated.ValueXY({x: 2, y: 1});
  path = `M ${this.P1._value} ${this.P1._value} S ${this.P2.x._value} ${this.P2.y._value} ${this.P3.x._value} ${this.P3.y._value}`;

            Animated.timing(this.P1, {
              toValue: {x: 3, y: 3},
              duration: 300,
              useNativeDriver: 'true',
            }).start();


        <Svg height="50%" width="50%" viewBox="0 0 4 4 " fill="blue">
          {/* <Path /> */}
          {console.log('this.P1.x: ', this.P1.x)}
          <AnimatedPath
            d={this.path}
            // d="M 0 1 S 1 1 2 1"
            fill="none"
            stroke="red"
          />
        </Svg>

但它不起作用。有没有办法让它变得有活力呢?

我们还应该使用react-native-reanimated2吗?

EN

回答 1

Stack Overflow用户

发布于 2020-10-16 15:13:54

查看useAnimatedProps的reanimated2文档。请注意,文档中的示例有一些缺陷,以下是一个演示:

代码语言:javascript
复制
import { StyleSheet } from 'react-native';
import {
  Animated,
  useSharedValue,
  useAnimatedProps,
  withTiming
} from 'react-native-reanimated';
import Svg, { Path } from 'react-native-svg';

const AnimatedPath = Animated.createAnimatedComponent(Path);

function App() {
  const radius = useSharedValue(50);

  const animatedProps = useAnimatedProps(() => {
    // draw a circle
    const path = `
    M 100, 100
    m -${radius}, 0
    a ${radius},${radius} 0 1,0 ${radius * 2},0
    a ${radius},${radius} 0 1,0 ${-radius * 2},0
    `;
    return {
      d: path
    };
  });

  // attach animated props to an SVG path using animatedProps
  return <Svg><AnimatedPath animatedProps={animatedProps} fill="black" onPress={() => radius.value = withTiming(Math.random() * 180)} /></Svg>
}

我知道你可能找到了一个解决方案,但我想为那些偶然发现这一点的人提供一个答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63934357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档