question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Animated API rotation around the center, just like using originX and originY without animation

See original GitHub issue

Question

How can i use the animated API to rotate a line around an originX, originY point, just like i would do using:

 rotation: angle, originX: someX, originY: someY

where angle would be a continuously changing state to act like animation.

I have opened a more detailed question here in SO , but i didn’t have any luck yet. Any help is really appreciated!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
angelos3lexcommented, Jan 13, 2020

@msand Thanks a LOT! This is working. I’m trying to understand the reason of translating X and Y by (±)width / 2 and why this should be the case only on android. Is there some reference to it somewhere?

1reaction
msandcommented, Jan 12, 2020

This seems to work quite fine: https://snack.expo.io/@msand/vengeful-strawberries

import React, { Component } from 'react';
import { Text, View, StyleSheet, Animated, Platform } from 'react-native';
import { Svg, Path, Rect, G, Circle } from 'react-native-svg';

const AnimatedG = Animated.createAnimatedComponent(G);

export class TestLogo extends Component {
  state = {
    rotation: new Animated.Value(0),
    offset: 0,
  };

  componentDidMount() {
    Animated.loop(
      Animated.timing(this.state.rotation, {
        useNativeDriver: true,
        duration: 3000,
        toValue: 1,
      })
    ).start();
  }

  onLayout =
    Platform.OS === 'android'
      ? a => {
          this.setState({ offset: a.nativeEvent.layout.width / 2 });
        }
      : null;

  render() {
    const offsetAndroid = this.state.offset;
    const [pivotX, pivotY] = [25, 25];
    return (
      <Svg
        width="100%"
        height="100%"
        onLayout={this.onLayout}
        viewBox={`0 0 50 50`}>
        <Rect width="50" height="50" />
        <G transform={`translate(${pivotX}, ${pivotY})`}>
          <Circle r="5" fill="white" />
          <AnimatedG
            style={{
              transform: [
                { translateX: -offsetAndroid },
                {
                  rotate: this.state.rotation.interpolate({
                    inputRange: [0, 1],
                    outputRange: ['0deg', '360deg'], // I would like to set pivot point at 25 25
                  }),
                },
                { translateX: offsetAndroid },
              ],
            }}>
            <Path
              fill="#FFF"
              d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z"
              transform={`translate(-${pivotX} -${pivotY})`}
            />
          </AnimatedG>
        </G>
      </Svg>
    );
  }
}

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <TestLogo />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

Animated API rotation around the center, just like using ...
Question How can i use the animated API to rotate a line around an originX, originY point, just like i would do using:...
Read more >
transform-origin - CSS: Cascading Style Sheets | MDN
The transform origin is the point around which a transformation is applied. For example, the transform origin of the rotate() function is ......
Read more >
React Native Animated Rotation Anchor Point - Stack Overflow
But since there is no option to define the anchor point, the element will rotate around its center, which is the center of...
Read more >
Animating Items Around a Point - KIRUPA
When the origin point is in the middle of the element, our element will look to be spinning in-place: To rotate an element...
Read more >
The Animate Property - The Framer book
These transforms (scaling, rotating, and skewing) will happen from the frame's transformation origin. By default, this is the center of the frame, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found