Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate an SVG in React Native

Tags:

react-native

I have an SVG that I've created in react native, and I'd simply like to rotate this 360 degrees continuously in the most efficient way possible.

Thanks.

like image 992
George Kemp Avatar asked Jun 16 '18 19:06

George Kemp


People also ask

How do you rotate something in react native?

add in style transform: [{ rotate: '14deg' }], if you make view position absolute it will not affect another view! the rotate start from 0 to 360 deg .

How use SVG in react native?

Open up the project in your favorite editor and start by importing the Svg and Circle components from react-native-svg, as shown below. import Svg, { Circle } from 'react-native-svg'; The <Svg> component is a parent component that is needed to render any SVG shape.

What does transform do in react native?

Transforms are style properties that will help you modify the appearance and position of your components using 2D or 3D transformations. However, once you apply transforms, the layouts remain the same around the transformed component hence it might overlap with the nearby components.


2 Answers

Just use transform. It works perfectly.

<YourSvg
    height={20}
    width={20}
    style={{ transform: [{ rotateY: '180deg' }] }}
/>
like image 97
Amit Avatar answered Sep 22 '22 19:09

Amit


Just wrap your SVG in a View component and make use of the Animated API. Your code would be something in the lines of this:

class YourComponent extends React.Component {

  constructor(props) {
    super(props);
    this.animation = new Animated.Value(0);
  }

  render() {

    const rotation = this.animation.interpolate({
      inputRange: [0, 1],
      outputRange: ['0deg', '360deg']
    });

    return (
      <Animated.View
        style={{transform: [{rotate: rotation}] }}
      >
        <YourSVG />
      </Animated.View>
    );


  componentDidMount() {

    Animated.loop(
      Animated.timing(this.animation, {toValue: 1, duration: 2000})
    ).start();    
  }
}
like image 34
dentemm Avatar answered Sep 22 '22 19:09

dentemm