anime icon indicating copy to clipboard operation
anime copied to clipboard

Stop an animation

Open Alf21 opened this issue 7 years ago • 30 comments

Is it possible to stop an animation from inside of another animation?

I created two timelines. The first one is infinite and the second one has two animations. After finishing animation 2, i want to stop animation 1 and go to the next step.

Thats currently not possible i think, so i tried another thing: I set the autoplay of anim1 to false and started it with 'anim1.play()' when anim2.begin callback is called. Then im waiting for callback anim2.complete to create a third animation.

But it is also buggy, if i set an animation to autoplay = false, i can't start it from any event or anything else...

Any solutions?

Alf21 avatar Jun 12 '17 17:06 Alf21

Please provide some code.

juliangarnier avatar Jun 15 '17 07:06 juliangarnier

I have a similar "issue" -- is there a way to "cancel" a currently-playing animation? I didn't notice anything in the docs, or on looking at the properties of the return value of anim(...).

(Separately, awesome library -- many thanx!)

pakx avatar Jul 02 '17 22:07 pakx

@pakx You can call pause() on the return value of anim(...).

snc avatar Jul 03 '17 05:07 snc

@snc, yes, I saw pause() ... but I assume that only "pauses" an animation, possibly to be continued later, correct? What if I want to cancel/kill it completely, including having it release whatever resources it's holding (timers, memory, etc)?

pakx avatar Jul 03 '17 05:07 pakx

I'm pretty much amazed that there is no option for this. I'm converting my animation from TweenMax. It had TweenMax.killTweensOf(el);. I have multiple hover animations and i want to stop all the non-hovered elements from animating.

wintercounter avatar Aug 18 '17 09:08 wintercounter

@pakx @wintercounter See anime.remove(el) https://github.com/juliangarnier/anime/#animeremovetarget

juliangarnier avatar Aug 21 '17 11:08 juliangarnier

@juliangarnier what if I only want to stop one of the animations and not all of them? pause() will only pause the animation and let it hanging around in memory.

ericmorand avatar Nov 05 '17 21:11 ericmorand

I don't think it's a problem. Have you noticed performance issues using .pause()?

juliangarnier avatar Nov 06 '17 03:11 juliangarnier

pause() doesn't work for me after i start the animation

akash-pal avatar Apr 03 '18 06:04 akash-pal

If you trigger play after pausing will it resume from the point where it was paused? This indicates to me that there is a distinct use for a cancel.

garrettmaring avatar Nov 06 '18 18:11 garrettmaring

Any news about this? There is no animation.stop() or animation.destroy() ?

Cristy94 avatar Jan 10 '19 13:01 Cristy94

It seems that removing animations from the anime.running array can effectively cancel them.

Setting anime.running.length to 0 seems to stop all animations. It seems you can also pop animations from the array and they will stop. Beware though, as anime.running = [] seems to do nothing. You must modify the existing array, not replace it.

It appears that for popped animations animation.completed will remain false and animation.finished will remain unresolved, likely because animation.currentTime, and animation.progress become frozen. The animation seems effectively dead, AKA cancelled or stopped.

I don't know if this is the intended way to do this, but I'm pretty sure its effects are what we are all looking for here. Hopefully there are no unseen side effects.

@juliangarnier If this is how we are supposed to do this could you add it to the docs? :)

AaronBeaudoin avatar Aug 17 '19 05:08 AaronBeaudoin

If you want to cancel an animation with only the animation instance, I wrote a function that will gather all the targets, to then call anime.remove on each one. However, it would be much easier with an api as suggested above like instance.cancel().

function getTargets(ani) {
  return ani.children.reduce(
    (all, one) => all.concat(getTargets(one)),
    ani.animatables.map((a) => a.target)
  )
}

function cancel(ani) {
  getTargets(ani).forEach(anime.remove)
}
let ani = anime.timeline(...).add(...).add(...)

// Later on...

cancel(ani)

jameskerr avatar Nov 11 '19 18:11 jameskerr

I'm surprised that this is not implemented yet. It's 2 years old issue and solution is pretty simple. @juliangarnier please implement this feature

lukejagodzinski avatar Nov 27 '19 13:11 lukejagodzinski

I stumbled upon this looking for something else. I just started using anime.js for a class and needed to stop an animation. Eventually this animation will be triggered off of a check box. RemoveAnim

I remove the animation by essentially removing the object that is being animated. Sort of hacky, but it works like a charm. image

SupahSprinkle avatar Nov 28 '19 03:11 SupahSprinkle

Actually solution with anime.remove(el) kinda works but from API perspective it's ugly. It should be done on the instance level as this one removes element from all the instances which for some people might be wrong

lukejagodzinski avatar Nov 29 '19 22:11 lukejagodzinski

You can use .pause() I had to update to new version also here's the CDN: https://cdn.jsdelivr.net/npm/[email protected]/lib/anime.min.js https://animejs.com/documentation/#playPause

justanotherkevin avatar Dec 11 '19 15:12 justanotherkevin

Any news on a destroy like method? I just can't remove the target element.

dangelion avatar Dec 16 '19 10:12 dangelion

The issue with anime.remove(el) is that an attached callback such as change will continue running even with no targets. So it's not a true stop.

mracette avatar Feb 08 '20 18:02 mracette

This still keeps popping up in my life, now with a React project. It's causing huge memory leaks. Simply having setState in any animejs callback will force the browser to keep my component in memory and there is just no way to clean up created timelines...

wintercounter avatar Mar 26 '20 09:03 wintercounter

Formalizing and bumping @AaronBeaudoin 's https://github.com/juliangarnier/anime/issues/188#issuecomment-522206934, which seems to be the best method we have right now:

function cancelAnimation (animation) {
  let activeInstances = anime.running;
  let index = activeInstances.indexOf(animation);
  activeInstances.splice(index, 1);
}

Caveats:

  • Will only work for playing animations, not paused ones.
  • complete will stay false, and the finished promise will not resolve.

akbr avatar Apr 30 '20 03:04 akbr

I really hope we have a destroy method on the anime instance. Is there any possibility?

yellow1912 avatar Jul 24 '20 16:07 yellow1912

anime.destory() is necessary.

xJkit avatar Nov 19 '20 10:11 xJkit

Why is this closed if the issue is not solved?

Cristy94 avatar Nov 19 '20 16:11 Cristy94

This is pretty essential for properly integrating this into frameworks like React, isn't it?

baba43 avatar Feb 26 '21 18:02 baba43

+1, this would be great to have

Gameghostify avatar Mar 17 '21 14:03 Gameghostify

Finding when a React component remounts, the animation timeline gets glitchy. Having a destroy option like GSAP would help a lot.

Then I could destroy the animation or timeline on unmount.

yougotashovel avatar Mar 17 '21 14:03 yougotashovel

Will work on that for the next release!

juliangarnier avatar Mar 19 '21 17:03 juliangarnier

Looking forward toanime.destory

xiziliang avatar Feb 21 '22 03:02 xiziliang

This is the only thing that finally worked for me:

const runningAnims = anime.running; while (runningAnims.length > 0) { runningAnims.pop(); }

angelafox777 avatar Jan 09 '23 12:01 angelafox777