Skip to content

Unexpected behavior with removeTargets(targets) function #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gphelps opened this issue Apr 18, 2020 · 0 comments
Open

Unexpected behavior with removeTargets(targets) function #677

gphelps opened this issue Apr 18, 2020 · 0 comments

Comments

@gphelps
Copy link

gphelps commented Apr 18, 2020

Tried example animation https://codepen.io/juliangarnier/pen/rGjMyW and then called removeTargets on a second animation.

removeTargets is pausing the seamless animation, since removeTargets will remove the children of the main instance. It may make sense to check that things are removed before removing a child or pausing an instance - I may be missing some limiting cases here, though...

THANKS!

I fixed the problem with the following code:

// Remove targets from animation
function removeTargetsFromAnimations(targetsArray, animations) {
var count = 0;
for (var a = animations.length; a--;) {
if (arrayContains(targetsArray, animations[a].animatable.target)) {
animations.splice(a, 1);
count++;
}
}
return count;
}

function removeTargets(targets) {

var targetsArray = parseTargets(targets);
for (var i = activeInstances.length; i--;) {
var instance = activeInstances[i];
var animations = instance.animations;
var children = instance.children;
var animationsRemoved = removeTargetsFromAnimations(targetsArray, animations);
var childAnimationsRemoved = 0;
for (var c = children.length; c--;) {
var child = children[c];
var childAnimations = child.animations;
childAnimationsRemoved = removeTargetsFromAnimations(targetsArray, childAnimations);
//Remove child only if child animations are removed and no children or animations exist
if (childAnimationsRemoved > 0 && !childAnimations.length && !child.children.length) {
children.splice(c, 1);
}
}
//Pause only if animations or child animations have been removed and no animations or children exist
if ((animationsRemoved > 0 || childAnimationsRemoved > 0) && !animations.length && !children.length) {
instance.pause();
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant