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.

Rotation via setNativeProps transform doesn't center

See original GitHub issue

Great library. I’m trying to work with 7.1.2 on iOS (no expo) to animate the rotation of a circle. The circle is definitely moving, but seems to be rotating around 0,0 - I need it to rotate around the center point (24, 24). Looks like originX/Y aren’t being applied in this scenario.

The code I’m currently using is below. Is this a way I can achieve center point rotation?

Happy to write a PR if the native code needs extending to support this.

Many thanks Tom

react-native-svg@7.1.2 react-native@0.57.1 xcode@10.0

import * as React from 'react'
import Svg, { Path, Circle } from 'react-native-svg'
import { Animated } from 'react-native'

const radius = 22.8
const circumference = radius * 2 * Math.PI
const offset = circumference - (40 / 100) * circumference

export class MicrophoneActiveIcon extends React.Component {
  public circleRef: any

  public state = {
    rotation: new Animated.Value(-90)
  }

  public componentDidMount() {
    Animated.loop(
      Animated.timing(this.state.rotation, {
        toValue: 270,
        duration: 1000,
        useNativeDriver: true
      })
    ).start()

    this.state.rotation.addListener(({ value }) => {
      this.circleRef.setNativeProps({
        transform: [{ rotate: value.toString() }]
      })
    })
  }

  public render() {
    return (
      <Svg width="48" height="48">
        <Circle
          ref={ref => {
            this.circleRef = ref
          }}
          cx="24"
          cy="24"
          r={radius.toString()}
          stroke="black"
          strokeWidth={2}
          fill="none"
          originX="24"
          originY="24"
          strokeLinecap="round"
          strokeDasharray={`${circumference.toString()} ${circumference.toString()}`}
          strokeDashoffset={offset.toString()}
        />
      </Svg>
    )
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
riglarcommented, Oct 17, 2018

Thanks for your detailed response @msand

I’ve tried all your suggestions and seem to be getting decent behaviour using this transform:

 { translateX: 24 },
 { translateY: 24 },
 { rotate: value.toString() },
 { translateX: -24 },
 { translateY: -24 },

I’ve unset useNativeDriver as you suggest.

Favour owed, hit me up if you need PR work doing.

2reactions
msandcommented, Oct 17, 2018

Or perhaps something like this:

import React, { Component } from "react";
import Svg, { Circle } from "react-native-svg";
import extractStroke from "react-native-svg/lib/extract/extractStroke";
import { Animated, Easing } from "react-native";

const radius = 22.8;
const circumference = radius * 2 * Math.PI;

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

  componentDidMount() {
    Animated.loop(
      Animated.timing(this.state.rotation, {
        toValue: circumference,
        duration: 10000,
        easing: Easing.linear
      }),
    ).start();

    this.state.rotation.addListener(({ value }) => {
      this.circleRef.setNativeProps(
        extractStroke({
          stroke: "black",
          strokeWidth: 2,
          strokeLinecap: "round",
          strokeDasharray: circumference,
          strokeDashoffset: value,
        }, []),
      );
    });
  }

  render() {
    return (
      <Svg width="48" height="48">
        <Circle
          ref={ref => {
            this.circleRef = ref;
          }}
          cx="24"
          cy="24"
          r={radius}
          fill="none"
        />
      </Svg>
    );
  }
}

Seems there’s something a bit unexpected with the transforms, will have to look into it later on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rotation via setNativeProps transform doesn't center #815
Great library. I'm trying to work with 7.1.2 on iOS (no expo) to animate the rotation of a circle. The circle is definitely...
Read more >
CSS rotation not centered - Stack Overflow
So I've got this circle that is rotating on hover but it's not centered and I don't know why (I did add the...
Read more >
rotate() - CSS: Cascading Style Sheets - MDN Web Docs
The rotate() CSS function defines a transformation that rotates an element around a fixed point on the 2D plane, without deforming it.
Read more >
CSS3 Transform not rotating from center - SitePoint
Hey guys! Anyone care to tell me why this link doesn't rotate from the center like the default specification states…. I'm stumped.
Read more >
rotate | CSS-Tricks
While CSS already has another way to rotate elements using the rotate() function in the transform property, like this:
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