Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SVG adapter #1623

Merged
merged 1 commit into from
Jan 14, 2021
Merged

Fix SVG adapter #1623

merged 1 commit into from
Jan 14, 2021

Conversation

karol-bisztyga
Copy link
Contributor

@karol-bisztyga karol-bisztyga commented Jan 14, 2021

Description

There were two bugs in the SVG adapter's implementation:

  • overwriting translateX
  • lack of parsing translate values to numbers

Fixes #1593

code
import React, { useEffect } from 'react';
import { StyleSheet, View } from 'react-native';
import Animated, {
  useSharedValue,
  withTiming,
  useAnimatedProps,
  Easing,
  interpolate,
  SVGAdapter,
} from 'react-native-reanimated';
import { G, Svg, Rect } from 'react-native-svg';

const AnimatedG = Animated.createAnimatedComponent(G);

export default function AnimatedSVGPropsUpdateExample(props) {
  const valueToAnimate = useSharedValue(0);

  useEffect(() => {
    const intervalId = setInterval(() => {
      valueToAnimate.value = withTiming(Math.floor(Math.random() * 15000), {
        duration: 1000,
        easing: Easing.linear,
      });
    }, 1000);
    return () => clearInterval(intervalId);
  }, []);

  const failingTransform = useAnimatedProps(
    () => {
      const yTranslate = Math.trunc(
        interpolate(valueToAnimate.value, [0, 15000], [-150, 150], {
          extrapolateLeft: 'extend',
          extrapolateRight: 'extend',
        })
      );
      return { transform: `translate(0 ${yTranslate})` };
    },
    null,
    SVGAdapter
  );

  return (
    <View
      style={[
        StyleSheet.absoluteFill,
        { alignItems: 'center', justifyContent: 'center' },
      ]}>
      <Svg style={{ position: 'absolute' }}>
        <AnimatedG animatedProps={failingTransform}>
          <Rect
            x="50"
            y="200"
            width="70"
            height="70"
            stroke="red"
            strokeWidth="2"
            fill="yellow"
          />
        </AnimatedG>
      </Svg>
    </View>
  );
}

Copy link
Member

@jakub-gonet jakub-gonet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

@karol-bisztyga karol-bisztyga merged commit ccff046 into master Jan 14, 2021
@karol-bisztyga karol-bisztyga deleted the @karol/fix-svg-adapter branch January 14, 2021 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useAnimatedProps with SVG transform crash
2 participants