question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Controlling animation state within a button

See original GitHub issue

First of all, THANK YOU for creating this 😃 Super easy to plug into my react app and get lottie-ing 😃

Just wondering how best to implement a clickable ‘like’ button like the twitter example. Most things I see are all loops and i’m unsure how to get the animation to pause/stop.

I thought what i could do was start with loop: false, autoplay: false and my initial state isStopped: true. Then onClick: basically just set isStopped to false as well, so that way the heart would go from grey to pink, then stop.

The tricky bit next was how to either a) ‘reset’ it back to frame 1 or b) reverse the direction of the animation and play in reverse til it hits frame 1.

Any ideas?

Secondly, the documentation on the bodymovin repo for direction is a little vague, but from looking at your source code am i correct in assuming that -1 is reverse?

I have a rough sketch here,

export default class LottieControl extends React.Component {
    constructor(props) {
        super(props);
        this.state = { isPaused: true, isLiked: false };
    }

    handleClick = () => {
        const { isLiked } = this.state;
        if (!isLiked) {
            this.setState({ direction: 1, isPaused: false, isLiked: true });
        } else {
            this.setState({ direction: -1, isPaused: false, isLiked: false });
        }
    };

    render() {
        const buttonStyle = {
            display: 'block',
        };

        const defaultOptions = {
            loop: false,
            autoplay: false,
            animationData: animationData,
            direction: this.state.direction || 1,
        };

        return (
            <div>
                <button style={buttonStyle} onClick={this.handleClick}>
                    <Lottie
                        options={defaultOptions}
                        height={100}
                        width={100}
                        isPaused={this.state.isPaused}
                    />
                </button>
            </div>
        );
    }
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
fabio-Ccommented, Jan 10, 2020

@chenqingspring How can I see the code behind the toggle example? The link show me only the animation.

2reactions
chenqingspringcommented, Nov 6, 2017

I need to clarify your requirement, do you want to toggle your like button like this?

gray unlike -> click -> pink like -> click -> gray unlike

If so, I suggest you to use direction toggle with setting loop & autoplay to false and init isStopped to true

then, bind this clickHandler to your button

    const clickHandler = () => {
      const {isStopped, direction} = this.state;
      if (!isStopped) {
        this.setState({direction: direction * -1})
      }
      this.setState({isStopped: false})
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to control an animation state using ui button in unity?
I want to control an animation state through a ui button in unity arcore. I had tried these steps for controlling the "pose"...
Read more >
Controlling animation states :: Godot 3 Recipes - KidsCanCode
Use an AnimationTree to create an animation state machine. This will allow us to organize our animations and most importantly, control the transitions...
Read more >
Why can't I control an Animator using a UI Button?
Are there any parameters you have defined in the animator? Please show us transitions settings between two animations states. · Thanks Swati! I ......
Read more >
Animation States - Unity - Manual
A new state can be added by right-clicking on an empty space in the Animator Controller Window and selecting Create State->Empty from the...
Read more >
Video: Trigger an animation effect - Microsoft Support
When you want to click a specific thing on a slide to start an animation effect, use a trigger. Triggers give you specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found