GithubHelp home page GithubHelp logo

Comments (5)

czystyl avatar czystyl commented on September 25, 2024

Hey @MauriceNino, I'll try to find some time to check it out.

from react-native-reanimated-flip.

MauriceNino avatar MauriceNino commented on September 25, 2024

Thanks a lot!

from react-native-reanimated-flip.

buryo avatar buryo commented on September 25, 2024

I guess this package doesn't work anymore, I get the same error.

from react-native-reanimated-flip.

felipecorbee avatar felipecorbee commented on September 25, 2024

Same issue here

from react-native-reanimated-flip.

finnp avatar finnp commented on September 25, 2024

Here's a fixed version for react-native-reanimated 3.6.0. I also created a PR for this: #58

import Animated, {
  Easing,
  interpolate,
  useAnimatedStyle,
  useDerivedValue,
  withTiming,
} from 'react-native-reanimated';
import {StyleSheet, ViewStyle} from 'react-native';
import React from 'react';

export enum FlipSide {
  FRONT,
  BACK,
}

export enum RotateAxis {
  Y = 'Y',
  X = 'X',
}

type Props = {
  perspective?: number;
  side: FlipSide;
  rotate?: RotateAxis;
  style?: ViewStyle;
  front: React.ReactElement;
  back: React.ReactElement;
};

const ReanimatedFlip = ({
  perspective = 1200,
  rotate = RotateAxis.Y,
  side,
  front,
  back,
  style,
}: Props) => {
  const rotatePosition = interpolate(side, [0, 1], [180, 360]);

  const rotateValue = useDerivedValue(() => {
    return withTiming(rotatePosition, {
      duration: 500,
      easing: Easing.inOut(Easing.ease),
    });
  });

  const rotationFlip = useDerivedValue(() => {
    if (rotate === RotateAxis.Y) {
      return {
        rotateY: `${rotateValue.value}deg`,
      };
    }

    return {
      rotateX: `${rotateValue.value}deg`,
    };
  }, [rotate, rotateValue]);

  const rotationFlipBack = useDerivedValue(() => {
    if (rotate === RotateAxis.Y) {
      return {
        rotateY: '180deg',
      };
    }

    return {
      rotateX: '180deg',
    };
  }, [rotate]);

  const opacityFront = useDerivedValue(() => {
    return withTiming(side, {
      duration: 500,
      easing: Easing.inOut(Easing.ease),
    });
  }, [side]);

  const opacityBack = useDerivedValue(() => {
    return withTiming(side === 0 ? 1 : 0, {
      duration: 500,
      easing: Easing.inOut(Easing.ease),
    });
  }, [side]);

  const animatedStyleFront = useAnimatedStyle(() => {
    return {
      opacity: opacityFront.value,
      transform: [{perspective}, {...rotationFlip.value}],
    };
  }, [rotate, side, rotationFlip]);

  const animatedStyleBack = useAnimatedStyle(() => {
    return {
      opacity: opacityBack.value,
      transform: [
        {perspective},
        {...rotationFlipBack.value},
        {...rotationFlip.value},
      ],
    };
  }, [rotate, side]);

  return (
    <Animated.View style={StyleSheet.flatten([style, styles.container])}>
      <Animated.View style={[styles.side, animatedStyleFront]}>
        {front}
      </Animated.View>
      <Animated.View style={[styles.side, animatedStyleBack]}>
        {back}
      </Animated.View>
    </Animated.View>
  );
};

const styles = StyleSheet.create({
  container: {
    justifyContent: 'center',
    alignItems: 'center',
    height: '100%',
    width: '100%'
  },
  side: {
    width: '100%',
    height: '100%',
    position: 'absolute',
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default ReanimatedFlip;

from react-native-reanimated-flip.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.