React-native-animatable: Animate Svg Components ?

Created on 24 May 2018  路  2Comments  路  Source: oblador/react-native-animatable

Hi ! I would like to know if this lib can work with SVG components from react-native-svg. I tried to use createAnimatableComponent but nothing is working.

The svg components display well but no animations. (I tried with Svg.Text, Svg.Polygon & Svg.G)

This is basically what I'm doing:

import { Svg } from 'expo'

const AnimatableSvgG = Animatable.createAnimatableComponent(Svg.G)

<AnimatableSvgG
  delay={1000}
  animation="fadeInUp"
  useNativeDriver>
    // ....Svg components
</AnimatableSvgG>

Most helpful comment

I succeed to animate Svg with this lib 馃槂
I'm doing like this for the moment :

I build a generic Animatable SVG component:

import React, { PureComponent } from 'react'
import { createAnimatableComponent } from 'react-native-animatable'

class AnimatedSvg extends PureComponent {
  setNativeProps = props => {
    const { component } = this.refs
    // Need to convert these props into string to animate
    if (props.style && props.style.opacity) {
      const opacityString = props.style.opacity.toString()
      props.fillOpacity = opacityString
      props.strokeOpacity = opacityString
    }
    component && component.root && component.root.setNativeProps(props)
  }

  render() {
    const { component: Component } = this.props
    return <Component ref="component" {...this.props} />
  }
}

export default createAnimatableComponent(AnimatedSvg)

In my render function:

for Text

                  <AnimatedSvg
                    component={Svg.Text}
                    fill="white"
                    animation="fadeIn"
                    fillOpacity={0} // I have to set here the initial opacity, don't know why...
                  >
                    {label}
                  </AnimatedSvg>

for Polygon

                <AnimatedSvg
                  component={Svg.Polygon}
                  points={polygon.map(points => `${points[0]},${points[1]}`).join(' ')}
                  animation="fadeIn"
                  stroke="white"
                  fillOpacity={0} // initial value
                  strokeOpacity={0} // initial value
                />

I don't know much about this lib, I'm sure that it can be improve ! But it can be good to implement this or to add this in the docs 鉂わ笍

All 2 comments

I succeed to animate Svg with this lib 馃槂
I'm doing like this for the moment :

I build a generic Animatable SVG component:

import React, { PureComponent } from 'react'
import { createAnimatableComponent } from 'react-native-animatable'

class AnimatedSvg extends PureComponent {
  setNativeProps = props => {
    const { component } = this.refs
    // Need to convert these props into string to animate
    if (props.style && props.style.opacity) {
      const opacityString = props.style.opacity.toString()
      props.fillOpacity = opacityString
      props.strokeOpacity = opacityString
    }
    component && component.root && component.root.setNativeProps(props)
  }

  render() {
    const { component: Component } = this.props
    return <Component ref="component" {...this.props} />
  }
}

export default createAnimatableComponent(AnimatedSvg)

In my render function:

for Text

                  <AnimatedSvg
                    component={Svg.Text}
                    fill="white"
                    animation="fadeIn"
                    fillOpacity={0} // I have to set here the initial opacity, don't know why...
                  >
                    {label}
                  </AnimatedSvg>

for Polygon

                <AnimatedSvg
                  component={Svg.Polygon}
                  points={polygon.map(points => `${points[0]},${points[1]}`).join(' ')}
                  animation="fadeIn"
                  stroke="white"
                  fillOpacity={0} // initial value
                  strokeOpacity={0} // initial value
                />

I don't know much about this lib, I'm sure that it can be improve ! But it can be good to implement this or to add this in the docs 鉂わ笍

Thanks Martinez, I was looking for this too!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zr0n picture zr0n  路  6Comments

matheuscastroweb picture matheuscastroweb  路  6Comments

TrustDec picture TrustDec  路  3Comments

ssomnoremac picture ssomnoremac  路  5Comments

sundayhd picture sundayhd  路  5Comments