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

Generalize withTiming/withSpring to animate objects #4393

Merged
merged 5 commits into from
May 5, 2023

Conversation

mstach60161
Copy link
Contributor

@mstach60161 mstach60161 commented Apr 21, 2023

Summary

This PR adds possibility to animate objects with withTiming/withSpring functions.

  • Added AnimatableValueObject type
  • Added objectOnStart
  • Added objectOnFrame

Test plan

import Animated, {
  useSharedValue,
  withTiming,
  useAnimatedStyle,
  Easing,
} from 'react-native-reanimated';
import { View, Button } from 'react-native';
import React from 'react';

export default function AnimatedStyleUpdateExample(): React.ReactElement {
  function makeColor(x: number) {
    'worklet';
    return `hsl(${Math.round(x * 240)}, 100%, 50%)`;
  }
  const randomObject = useSharedValue({width: 100, height: 150, backgroundColor: makeColor(50) });

  const config = {
    duration: 2000,
    easing: Easing.bezier(0.5, 0.01, 0, 1),
  };

  const style = useAnimatedStyle(() => {
    return {
      width: randomObject.value.width,
      height: randomObject.value.height,
      backgroundColor: randomObject.value.backgroundColor,
    }
  });

  const onButtonPress = () => {
    const value = Math.random() * 400;
    const value2 = Math.random() * 400;
    const value3 = Math.random() * 400;
    const value4 = Math.random() * 400;
    randomObject.value = withSequence(withTiming({
        width: value, 
        height: value2, 
        backgroundColor: makeColor(value*value2)
      }, config
    ), 
    withDelay(1000, withTiming({
      width: value3, 
      height: value4, 
      backgroundColor: makeColor(value3*value4)
    }, config
  )))
  };

  return (
    <View
      style={{
        flex: 1,
        flexDirection: 'column',
      }}>
      <Animated.View
        style={[
          { width: 100, height: 80, backgroundColor: 'black', margin: 30 },
          style,
        ]}
      />
      <Button
        title="toggle"
        onPress={onButtonPress}
      />
    </View>
  );
}

@mstach60161 mstach60161 marked this pull request as ready for review April 21, 2023 12:02
Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

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

Generally looks good! Did you tested it with more complex animation like withSequence and withDelay?

react-native-reanimated.d.ts Outdated Show resolved Hide resolved
src/reanimated2/commonTypes.ts Outdated Show resolved Hide resolved
@mstach60161 mstach60161 added this pull request to the merge queue May 5, 2023
Merged via the queue into main with commit 1474835 May 5, 2023
6 checks passed
@mstach60161 mstach60161 deleted the @mstach60161/withTiming-withSpring-for-objects branch May 5, 2023 09:08
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
…#4393)

## Summary

This PR adds possibility to animate objects with
`withTiming`/`withSpring` functions.

- Added `AnimatableValueObject` type
- Added `objectOnStart`
- Added `objectOnFrame`

## Test plan

```
import Animated, {
  useSharedValue,
  withTiming,
  useAnimatedStyle,
  Easing,
} from 'react-native-reanimated';
import { View, Button } from 'react-native';
import React from 'react';

export default function AnimatedStyleUpdateExample(): React.ReactElement {
  function makeColor(x: number) {
    'worklet';
    return `hsl(${Math.round(x * 240)}, 100%, 50%)`;
  }
  const randomObject = useSharedValue({width: 100, height: 150, backgroundColor: makeColor(50) });

  const config = {
    duration: 2000,
    easing: Easing.bezier(0.5, 0.01, 0, 1),
  };

  const style = useAnimatedStyle(() => {
    return {
      width: randomObject.value.width,
      height: randomObject.value.height,
      backgroundColor: randomObject.value.backgroundColor,
    }
  });

  const onButtonPress = () => {
    const value = Math.random() * 400;
    const value2 = Math.random() * 400;
    const value3 = Math.random() * 400;
    const value4 = Math.random() * 400;
    randomObject.value = withSequence(withTiming({
        width: value, 
        height: value2, 
        backgroundColor: makeColor(value*value2)
      }, config
    ), 
    withDelay(1000, withTiming({
      width: value3, 
      height: value4, 
      backgroundColor: makeColor(value3*value4)
    }, config
  )))
  };

  return (
    <View
      style={{
        flex: 1,
        flexDirection: 'column',
      }}>
      <Animated.View
        style={[
          { width: 100, height: 80, backgroundColor: 'black', margin: 30 },
          style,
        ]}
      />
      <Button
        title="toggle"
        onPress={onButtonPress}
      />
    </View>
  );
}
```
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.

None yet

2 participants