Convert Figma logo to code with AI

animatedjs logoanimated

Declarative Animations Library for React and React Native

1,853
102
1,853
51

Top Related Projects

✌️ A spring physics based React animation library

23,338

Open source, production-ready animation and gesture library for React

A spring that solves your animation problems.

🎊 A collection of animations for inline style libraries

Quick Overview

Animated is a declarative animation library for React Native and React. It allows developers to create smooth, performant animations using a simple, declarative API. The library is designed to work seamlessly with React's component model and provides a powerful set of tools for creating complex animations with ease.

Pros

  • Easy-to-use declarative API for creating animations
  • High performance due to its use of native driver when possible
  • Seamless integration with React and React Native
  • Supports a wide range of animation types and interpolations

Cons

  • Limited documentation and examples compared to some other animation libraries
  • May have a steeper learning curve for developers new to animation concepts
  • Some advanced features require deeper understanding of the library's internals
  • Not as actively maintained as some other animation libraries

Code Examples

  1. Basic spring animation:
import { Animated } from 'react-native';

const springValue = new Animated.Value(0.3);

Animated.spring(springValue, {
  toValue: 1,
  friction: 1,
  tension: 1
}).start();

// Use springValue in your component's style
  1. Parallel animations:
const translateX = new Animated.Value(0);
const translateY = new Animated.Value(0);

Animated.parallel([
  Animated.timing(translateX, { toValue: 100, duration: 1000 }),
  Animated.timing(translateY, { toValue: 200, duration: 1000 })
]).start();
  1. Interpolation:
const animatedValue = new Animated.Value(0);

const interpolatedColor = animatedValue.interpolate({
  inputRange: [0, 1],
  outputRange: ['rgb(255,0,0)', 'rgb(0,0,255)']
});

// Use interpolatedColor in your component's style

Getting Started

To get started with Animated in a React Native project:

  1. Install React Native if you haven't already:

    npx react-native init MyAnimatedApp
    
  2. Animated is included in React Native, so no additional installation is needed.

  3. Import Animated in your component:

    import { Animated } from 'react-native';
    
  4. Create an animated value and use it in your component:

    const fadeAnim = new Animated.Value(0);
    
    Animated.timing(fadeAnim, {
      toValue: 1,
      duration: 1000,
    }).start();
    
    return (
      <Animated.View style={{ opacity: fadeAnim }}>
        {/* Your content here */}
      </Animated.View>
    );
    

This sets up a basic fade-in animation for your component.

Competitor Comparisons

✌️ A spring physics based React animation library

Pros of react-spring

  • More active development and larger community support
  • Wider range of animation capabilities, including physics-based animations
  • Better integration with React hooks and functional components

Cons of react-spring

  • Steeper learning curve for beginners
  • Larger bundle size due to more features

Code Comparison

react-spring:

import { useSpring, animated } from 'react-spring'

function AnimatedComponent() {
  const props = useSpring({ opacity: 1, from: { opacity: 0 } })
  return <animated.div style={props}>I will fade in</animated.div>
}

Animated:

import Animated from 'animated/lib/targets/react-dom'

class AnimatedComponent extends React.Component {
  state = { anim: new Animated.Value(0) }
  componentDidMount() {
    Animated.timing(this.state.anim, { toValue: 1 }).start()
  }
  render() {
    return <Animated.div style={{ opacity: this.state.anim }}>I will fade in</Animated.div>
  }
}

The code comparison shows that react-spring offers a more modern, hook-based approach, while Animated uses a class-based component with imperative animation setup. react-spring's syntax is generally more concise and easier to read, especially for developers familiar with React hooks.

23,338

Open source, production-ready animation and gesture library for React

Pros of Motion

  • More comprehensive animation and gesture support
  • Better TypeScript integration and type definitions
  • Larger community and more frequent updates

Cons of Motion

  • Steeper learning curve due to more features
  • Larger bundle size, which may impact performance

Code Comparison

Motion:

import { motion } from "framer-motion"

<motion.div
  initial={{ opacity: 0 }}
  animate={{ opacity: 1 }}
  transition={{ duration: 0.5 }}
>
  Hello World
</motion.div>

Animated:

import Animated from "animated/lib/targets/react-dom"

<Animated.div style={{ opacity: this.state.opacity }}>
  Hello World
</Animated.div>

Summary

Motion offers a more feature-rich and actively maintained solution for React animations, with better TypeScript support. However, it comes with a steeper learning curve and larger bundle size. Animated provides a simpler API but lacks some advanced features and community support. The choice between the two depends on project requirements, with Motion being more suitable for complex animations and Animated for simpler use cases.

A spring that solves your animation problems.

Pros of react-motion

  • Uses spring physics for more natural animations
  • Provides a declarative API for defining animations
  • Supports interrupting and chaining animations seamlessly

Cons of react-motion

  • Steeper learning curve due to its physics-based approach
  • May require more configuration for precise control over animations
  • Performance can be impacted with complex animations on lower-end devices

Code Comparison

react-motion:

<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}>
  {({x}) => <div style={{transform: `translateX(${x}px)`}} />}
</Motion>

Animated:

const animatedStyle = Animated.spring(this.state.anim, {
  toValue: {x: 10},
}).start();

<Animated.View style={[styles.box, animatedStyle]} />

Both libraries offer powerful animation capabilities for React applications. react-motion focuses on physics-based animations with a declarative API, while Animated provides a more imperative approach with a wider range of animation types. The choice between them depends on the specific project requirements, desired animation complexity, and developer preferences.

🎊 A collection of animations for inline style libraries

Pros of react-animations

  • Simpler API with predefined animations, making it easier for beginners
  • Lightweight and focused specifically on React applications
  • Seamless integration with CSS-in-JS libraries like styled-components

Cons of react-animations

  • Limited customization options compared to Animated's more flexible approach
  • Lacks the ability to create complex, multi-stage animations
  • No built-in support for native mobile animations

Code Comparison

react-animations:

import { fadeIn } from 'react-animations';
import styled, { keyframes } from 'styled-components';

const fadeInAnimation = keyframes`${fadeIn}`;
const FadeInDiv = styled.div`animation: 1s ${fadeInAnimation};`;

Animated:

import { Animated } from 'react-native';

const fadeAnim = new Animated.Value(0);
Animated.timing(fadeAnim, { toValue: 1, duration: 1000 }).start();

<Animated.View style={{ opacity: fadeAnim }}>
  {/* Content */}
</Animated.View>

react-animations offers a more straightforward approach for basic animations in React web applications, while Animated provides greater flexibility and power for complex animations across web and mobile platforms. The choice between the two depends on the specific project requirements and the developer's familiarity with animation concepts.

Pros of Motion One

  • Smaller bundle size and better performance
  • More modern API with a focus on simplicity
  • Better support for CSS variables and keyframe animations

Cons of Motion One

  • Less mature ecosystem and community support
  • Fewer advanced features for complex animations
  • Limited support for older browsers compared to Animated

Code Comparison

Motion One:

animate(
  element,
  { opacity: 1, x: 100 },
  { duration: 1, easing: "ease-out" }
);

Animated:

Animated.timing(
  this.state.fadeAnim,
  {
    toValue: 1,
    duration: 1000,
    easing: Easing.easeOut
  }
).start();

Motion One offers a more concise and intuitive API, while Animated requires more setup and uses a different approach to animation configuration. Motion One's syntax is closer to modern web standards, making it easier for developers familiar with CSS animations to adopt. However, Animated provides more fine-grained control over animations, which can be beneficial for complex use cases.

Both libraries have their strengths, with Motion One focusing on simplicity and performance for modern web applications, while Animated offers a robust solution with broader browser support and a more established ecosystem, particularly in the React Native community.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Animated

Declarative Animations Library for React and React Native

See the interactive docs.

Goal

The goal of this repo is to provide an implementation of the Animated library that is currently provided by React Native that can also be used by React in a web context. At some point, React Native will itself depend on this library.

Additionally, it would be ideal if this library would be compatible with future potential "targets" of React where animation makes sense.

Usage (Still Theoretical)

Right now the main export of this library is essentially just what is in the Animated namespace in React Native, minus the View, Image, and Text namespace. Additionally, it includes an inject namespace (explained below).

Ideally, I'd like to make it so that View, Image, and Text are exported, and just do the "right thing" depending on whether or not they are being used in the context of React Native or React Web. I'm not quite sure how we can do this yet without declaring dependencies on react native. Perhaps the platform specific file extensions can be used for this?

Injectables

There are several parts of this library that need to have slightly different implementations for react-dom than for react-native. At the moment, I've just made these things "injectable" so that this library can stay dependent on only react.

Some of these I am implementing as "injectable", even though right now it would technically work for both platforms. This doesn't hurt anything, and attempts to make this library more compatible with future "targets" for react.

The injectable modules are available off of the Animated.inject namespace, and include:

  • ApplyAnimatedValues
  • FlattenStyle
  • InteractionManager
  • RequestAnimationFrame
  • CancelAnimationFrame

Each of these modules can be injected by passing in the implementation. For example, a naive FlattenStyle could be passed in as:

Animated.inject.FlattenStyle(
  styles => Array.isArray(styles)
    ? Object.assign.apply(null, styles)
    : styles
);

Sample Code

Below are simple examples for using animated in React.

import React from "react";
import ReactDOM from "react-dom";
import Animated from "animated/lib/targets/react-dom";

class App extends React.Component {
  state = { anim: new Animated.Value(0) };
  click = () => {
    Animated.timing(this.state.anim, { 
      toValue: 100, 
      duration: 500 
    }).start();
  };

  render() {
    return (
      <div className="App">
        <Animated.div
          className="box"
          style={{ left: this.state.anim }}
          onClick={this.click}
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

The above code will move the div element with the class of box by 100px when clicked.

import React from "react";
import ReactDOM from "react-dom";
import Animated from "animated/lib/targets/react-dom";

class App extends React.Component {
  state = { anim: new Animated.Value(1) };
  handleMouseDown = () =>
    Animated.timing(this.state.anim, { toValue: 0.5 }).start();
  handleMouseUp = () =>
    Animated.timing(this.state.anim, { toValue: 1 }).start();

  render() {
    return (
      <div className="App">
        <Animated.div
          className="box"
          style={{ transform: [{ scale: this.state.anim }] }}
          onMouseDown={this.handleMouseDown}
          onMouseUp={this.handleMouseUp}
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

The above code will scale the div element with the class of box by in and then out when pressed.

NPM DownloadsLast 30 Days