React.js: Non-CSS animations

前端 未结 4 703
暗喜
暗喜 2021-02-07 04:20

React documentation doesn’t have anything about handling animations that are not CSS transitions, such as animation of scroll position and SVG attributes.

As for CSS tra

相关标签:
4条回答
  • 2021-02-07 04:30

    I successfully used this project in my react-hammer integration project there are some examples with hammer events and react animation.

    Here code of animated "BlinkingThing":

    var BlinkingThing = React.createClass({
        mixins: [React.Animate],
        blink: function () {
            var that = this;
            var animateAfter = function () {
                that.animate({
                    color: 'green'
                }, that.props.blinkBack);
            };
            this.animate({
                color: 'yellow'
            }, this.props.blinkTo, animateAfter);
        },
        componentDidReceiveProps: function () {
            this.setState({color: this.props.color})
        },
        componentDidMount: function () {
            this.setState({color: this.props.color})
        },
        receiveHammerEvent: function (ev) {
            if (ev) {
                var value = ev.type;
    
                switch (value) {
                    case 'tap':
                        this.blink();
                        break;
                }
            }
        },
        getInitialState: function () {
            return {};
        },
        render: function () {
            var style = {
                display: 'inline-block',
                backgroundColor: this.state.color
            };
    
            return (<div style={style}>{this.props.children}</div>);
        }
    });
    

    You need some parent component which will propagate side effects(touch events, for example) to trigger changes of state in BlinkingThing component (animation rely on state changing when you call this.animate func), I made a HitArea component to do that. It calls receiveHammerEvent func from its children passing hammer event when it occurs.

    0 讨论(0)
  • 2021-02-07 04:32

    Been having the same exact problems myself, until recently I've found out about Rekapi. This library provides state-based animations tools. Check out the tutorial https://github.com/jeremyckahn/rekapi/blob/master/docs/getting_started.md

    The trick is, the context doesn't have to be a canvas or a DOM element, it can be a plain object, i.e. a component instance or a mixin, so this opens the possibility either to perform some logic in your actor's render method and then setState on your component(context), or simply write a "one trick actor" that always forwards its state to the component each frame.

    0 讨论(0)
  • 2021-02-07 04:35

    This is what I came up so far: http://jsfiddle.net/NV/NtP7n/.

    I rewrote Dot to leverage React’s draw loop:

    var Dot = React.createClass({
    
        getInitialState: function() {
            return {
                start: 0,
                x: 0,
                final: 0,
                startTime: 0
            };
        },
    
        render: function() {
            var state = this.state;
            var props = this.props;
            var amount = 0;
    
            if (state.final !== props.x) {
                state.final = props.x;
                state.start = state.x;
                state.startTime = Date.now();
            } else {
                amount = (Date.now() - state.startTime) / this.props.duration;
            }
    
            if (amount <= 1) {
                var x = state.start + amount * (props.x - state.start);
                setTimeout(function() {
                    this.setState({x: x});
                }.bind(this), 1);
            } else {
                state.final = state.x = x = props.x;
            }
    
            return <circle r="10" cy="10" cx={x + 10}/>
        }
    });
    

    I have to call:

    setTimeout(function() {
        this.setState({current: x});
    }.bind(this), 1);
    

    just to force update on the next tick. I have to use setTimeout because setState isn’t allowed in render method. I wonder if I can queue an update on the next tick without using setTimeout.

    0 讨论(0)
  • 2021-02-07 04:47

    It seems react.animate is an actively maintained and often used React animation libraries.

    (it was already mentioned above, but would be easy to miss in all the links)

    Note: it comes with/requires underscore.js.

    0 讨论(0)
提交回复
热议问题