Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Native ❤ 60FPS Improving React Native animations - ReactEurope 2016

React Native ❤ 60FPS Improving React Native animations - ReactEurope 2016

Animations are one of the most important elements of modern, well-crafted mobile apps. Animation effects possible to achieve using native SDKs exceed by far the capabilities of mobile browsers, hence based on the quality of animations it is easy to tell the difference between truly native apps and ones that run in a webview.

React Native provides several APIs for handling animations (layout animations, native navigator, Animated.js library). Some of them lets you directly leverage native SDK capabilities, others require round-trips over native-to-JS bridge. We will dive a little into implementation details of each of those methods to explain their limitations. I will also discuss native-animated-driver (the project I've been recently working on), and give some tips on how to make your React-Native app stay at 60FPS.

Krzysztof Magiera

June 02, 2016
Tweet

More Decks by Krzysztof Magiera

Other Decks in Technology

Transcript

  1. NATIVE COMPONENTS ANIMATIONS PROS CONS • maximum performance • platform-specific

    behaviour “by default” • very little work • very limited set of components • often impossible to customise
  2. LAYOUT ANIMATIONS JS Thread LayoutAnimation.spring(); UIManager.createView(...); UIManager.addChildren(...); Native Thread UI

    Thread applyUpdates(); runLayout(); ANIMATED_BLOCK_START; updateX(from: 180, to: 250); ANIMATED_BLOCK_END;
  3. LAYOUT ANIMATIONS PROS CONS • native performance • very easy

    API • works for layout properties only • non interruptible
  4. ANIMATED.JS (basics) var someValue = new Animated.Value(0); // can be

    kept in the component state return ( <Animated.View style={{opacity: someValue}}> <Text>Hello!</Text> </Animated.View> ); Animated.spring( someValue, { toValue: 1, friction: 0.5, } ).start();
  5. ANIMATED.JS (basics) var anotherValue = new Animated.Value(-200); var product =

    Animated.multiply(interpolatedValue, anotherValue); var interpolatedValue = someValue.interpolate({ inputRange: [0, 100], outputRange: [0, 1], });
  6. ANIMATED.JS PROS CONS • very flexible • allows for defining

    gesture interaction • best performance when not animating layout properties • difficult to express layout animations – complementary API is needed • gesture reaction latency • overloading JS thread
  7. ASYNC FATE Thread Thread UI I’m free and can do

    whatever I want but I won’t be blocking the UI Render some stuff with React and then send render commands to the UI Thread RENDER, RENDERRR..
  8. ASYNC FATE Thread Thread UI Hey! Touch event! Handle touch

    – maybe render sth RENDER, RENDERRR..
  9. ASYNC FATE Thread Thread UI Handle touch – maybe render

    sth RENDER, RENDERRR.. f1 f2 f3 input compose sorry man, have to compose the frame … input compose input VSYNC
  10. Animated.spring( someValue, { toValue: 1, friction: 0.5, useNativeDriver: true, }

    ).start(); OFFLOADING ANIMATIONS LIMITATIONS • implemented on Android only • only a subset of Animated.js has been implemented • animating layout properties is not supported • using value update handlers will still require JS roundtrip • single value cannot be used for animating both using native driver and using JS driver https://github.com/facebook/react-native/pull/7884