包详细信息

react-tween-state

chenglou147.8kBSD0.1.5

React animation.

react, animation, tween, transition

自述文件

React Tween State

The equivalent of React's this.setState, but for animated tweens: this.tweenState.

Live demo and source.

Npm:

npm install react-tween-state

Bower:

bower install react-tween-state

API

Example usage:

var tweenState = require('react-tween-state');
var React = require('react');

var App = React.createClass({
  mixins: [tweenState.Mixin],
  getInitialState: function() {
    return {left: 0};
  },
  handleClick: function() {
    this.tweenState('left', {
      easing: tweenState.easingTypes.easeInOutQuad,
      duration: 500,
      endValue: this.state.left === 0 ? 400 : 0
    });
  },
  render: function() {
    var style = {
      position: 'absolute',
      width: 50,
      height: 50,
      backgroundColor: 'lightblue',
      left: this.getTweeningValue('left')
    };
    return <div style={style} onClick={this.handleClick} />;
  }
});

The library exports Mixin, easingTypes and stackBehavior.

this.tweenState(path: String | Array<String>, configuration: Object)

This first calls setState and puts your fields straight to their final value. Under the hood, it creates a layer that interpolates from the old value to the new. You can retrieve that tweening value using getTweeningValue below.

path is the name of the state field you want to tween. If it's deeply nested, e.g. to animate c in {a: {b: {c: 1}}}, provide the path as ['a', 'b', 'c']

configuration is of the following format:

{
  easing: easingFunction,
  duration: timeInMilliseconds,
  delay: timeInMilliseconds,
  beginValue: aNumber,
  endValue: aNumber,
  onEnd: endCallback,
  stackBehavior: behaviorOption
}
  • easing (default: easingTypes.easeInOutQuad): the interpolation function used. react-tween-state provides frequently used interpolation (exposed under easingTypes). To plug in your own, the function signature is: (currentTime: Number, beginValue: Number, endValue: Number, totalDuration: Number): Number.
  • duration (default: 300).
  • delay (default: 0). *
  • beginValue (default: the current value of the state field).
  • endValue.
  • onEnd: the callback to trigger when the animation's done. **
  • stackBehavior (default: stackBehavior.ADDITIVE). Subsequent tween to the same state value will be stacked (added together). This gives a smooth tween effect that is iOS 8's new default. This blog post describes it well. The other option is stackBehavior.DESTRUCTIVE, which replaces all current animations of that state value by this new one.

* For a destructive animation, starting the next one with a delay still immediately kills the previous tween. If that's not your intention, try setTimeout or additive animation. DESTRUCTIVE + duration 0 effectively cancels all in-flight animations, skipping the easing function.

** For an additive animation, since the tweens stack and never get destroyed, the end callback is effectively fired at the end of duration.

this.getTweeningValue(path: String | Array<String>)

Get the current tweening value of the state field. Typically used in render.

License

BSD.

更新日志

Legend:

  • [I]: improvement
  • [F]: fix

0.1.5 (April 28th 2016)

  • [I] Move React and React-DOM dependencies to devDependencies. React-tween-state doesn't require React.

0.1.4 (November 21th 2015)

  • [I] Support for IE9 through requestAnimationFrame polyfill. a0b4fb5.
  • [I] Used to use this.isMounted() internally. Removed that since React might deprecate this. #49.
  • [F] If duration's 0, we jump to config.endValue immediately, rather than calling the easing function. #52.

0.1.3 (July 4th 2015)

  • [I] Export everything as default.

0.1.1 (June 21th 2015)

Note: this version is not compatible with React < 0.13.

  • [I] Better API! See the updated examples.
  • [I] Library converted to ES7 with Babel.
  • [I] Compatible with UMD.
  • [F] See the note here.

0.0.5 (March 17th 2015)

Note: this is the last release compatible with React < 0.13!

  • [I] Small optimizations.
  • [F] #24: Prevent error when onEnd unmounts the component (through parent).

0.0.4 (November 14th 2014)

  • [I] Bower support!
  • [F] The passed-in config object is no longer mutated.

0.0.3 (August 2nd 2014)

  • [F] Stop animation after the component's unmounted.

0.0.2 (July 25th 2014)

  • [I] Delay option. See the updated README.
  • [F] Default param not initialized correctly.
  • [F] Animation not starting.

0.0.0 (July 15th 2014)

  • Initial public release.