Giter VIP home page Giter VIP logo

react.animate's Introduction

React.Animate

A simple state animation mixin for React.js

Philosophy

React.Animate is a different approach to animate based on state rather than direct DOM mutation using $.animate or similar.

While it's great that you can use refs to get DOM nodes after render, the biggest benefit to using react is that there is always a direct, observable, and testable relationship between component props, state, and the rendered output.

Mutating the dom directly is an antipattern.

What we really want to animate is not the DOM, it's component state.

If you think about animation as a transition from one state value from another, you can just interpolate state over an interval, and your component can rerender precisely in response to the current component state at every step.

At it's most simple, React.Animate allows you to transition between one state and another over a set interval. The implementation supports the same syntax as $.animate.

you can pass either

this.animate(properties [, duration ] [, easing ] [, complete ] );

or

this.animate(key, value [, duration ] [, easing ] [, complete ] );

Example

React.Animate can be included in any React class by adding it to the mixins array

By animating state instead of the DOM directly, we can define logic that acts during certain parts of our animations.

var component = React.createClass({
  mixins: [React.Animate],
  getInitialState: function() {
    return {
      width: 100
    };
  },
  render: function() {
    var heightBounds = [50, 100];

    return React.DOM.div({
      style: {
        width: this.state.width,
        height: Math.min(heightBounds[1], Math.max(heightBounds[0], this.state.width / 2))
      },
      onClick: this.randomSize
    });
  },
  randomSize: function() {
    this.animate({
      width: _.random(20, 300)
    }, 500, function() {
      console.log("random size reached!");
    });
  }
});

view in jsfiddle

Installation

React.Animate can be installed with bower using

bower install react.animate --save

React.Animate can be installed with npm using

npm install react.animate --save

Both will automatically pull the required React and Underscore dependencies.

to use React.Animate, include it in your page or build process after React and Underscore

Dependencies

easing functions.

react.animate's People

Contributors

chpio avatar djdeath avatar elierotenberg avatar lmatiolis avatar mlunoe avatar pleasetrythisathome avatar v-rr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react.animate's Issues

"cubic-in-out" is undefined

Hi,

react.animate uses "cubic-in-out" as the default ease. My problem is: there is no "cubic-in-out" defined in djdeath/ease.

maybe, we should use another ease as the default.

D3/underscore dependencies (docs)

Instructions mention D3 and underscore as dependencies and discusses limitations of D3 transitions but they don't seem to be dependencies.

animating SVG?

I'm a newbie at react and trying to use this code to animate some SVG.

however I'm getting this error

TypeError: Cannot read property 'x' of null
    at React.Animate.animate (react.animate.js?34a905b567ee8ab0bb7fa42a052b245ab62b236b:105)
    at ReactMeteor.createClass.randomPos (slabs.jsx.js?7b132e1bb29717be246f93af3f33642314167c77:39)
    at ReactMeteor.createClass.getInitialState (slabs.jsx.js?7b132e1bb29717be246f93af3f33642314167c77:48)
    at new ReactClass.createClass.Constructor (react-with-addons-0.13.0.js:6248)
    at ReactCompositeComponentMixin.mountComponent (react-with-addons-0.13.0.js:6763)
    at ReactPerf.measure.wrapper [as mountComponent] (react-with-addons-0.13.0.js:13909)
    at Object.ReactReconciler.mountComponent (react-with-addons-0.13.0.js:14720)
    at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (react-with-addons-0.13.0.js:13353)
    at ReactDOMComponent.Mixin._createContentMarkup (react-with-addons-0.13.0.js:8153)
    at ReactDOMComponent.Mixin.mountComponent (react-with-addons-0.13.0.js:8063)

based on the code (snippet) below
without this the SVG renders fine.


var Dot = ReactMeteor.createClass({
  mixins: [React.Animate],

  randomPos: function() {
    console.log("randomPos");
    this.animate({
      x: _.random(20, 300)
    }, 500, function() {
      console.log("random size reached!");
    });

  },

  getInitialState: function() {
    this.randomPos();
    return {
      x: 20,
      final: 0,
      width: 100
    };
  },
  render: function() {
    return <circle r="20" cy="20" cx={this.state.x + 20}/>
  }

})

I'm also using this with meteor but that doesn't otherwise seem to have caused problem.s

Warning: ReactClass: You're attempting to include a mixin that is either null or not an object.

Hello, I'm trying to include react.animate to my react component

var React = require('react');
...
var MyComponent = React.createClass({
    mixins: [React.Animate],
...

Get warning on component load:

Warning: ReactClass: You're attempting to include a mixin that is either null or not an object. Check the mixins included by the component, as well as any mixins they include themselves. Expected object but got undefined.

And then error when try to make animation:

Uncaught TypeError: this.animate is not a function

If I try to make something like

var React = require('react');
React.Animate = require('react.animate');
...

I get

Uncaught TypeError: Cannot set property 'Animate' of undefined

in react.animate.js at

  React.Animate = {
...

I have my react js component in a separate js file, run JS app on Electron platform.

Similar implementation with velocity.js

I like how this is implemented but I'd need to use velocity.js versus D3. Should I just fork it or do you think it would make sense to make the animation library configurable?

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.