Skip to content

Commit

Permalink
Merge pull request #635 from DarkAce65/instance-remove
Browse files Browse the repository at this point in the history
"remove" method on instance
  • Loading branch information
juliangarnier committed Oct 12, 2020
2 parents 225fadd + 351866c commit 4ea2ca2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
7 changes: 5 additions & 2 deletions documentation/index.html
Expand Up @@ -3798,7 +3798,10 @@ <h3 class="demo-title">remove</h3>
Removes targets from a running animation or timeline.<br>
The <code>targets</code> parameters accepts the same values as the <a class="color-targets" href="#cssSelector">targets</a> property.
</p>
<p>Removes targets from all active animations.</p>
<pre><code>anime.remove(targets)</code></pre>
<p>Removes targets from the a single animation or timeline.</p>
<pre><code>animation.remove(targets)</code></pre>
</div>
<div class="demo-content remove-demo">
<div class="line">
Expand All @@ -3819,7 +3822,7 @@ <h3 class="demo-title">remove</h3>
</div>
<script>var remove = function() {
/*DEMO*/
anime({
var animation = anime({
targets: '.remove-demo .el',
translateX: 270,
direction: 'alternate',
Expand All @@ -3828,7 +3831,7 @@ <h3 class="demo-title">remove</h3>
});

document.querySelector('.remove-el-button').addEventListener('click', function() {
anime.remove('.remove-demo .line:nth-child(2) .el');
animation.remove('.remove-demo .line:nth-child(2) .el');
});
/*DEMO*/
}
Expand Down
28 changes: 16 additions & 12 deletions lib/anime.es.js
Expand Up @@ -1178,20 +1178,24 @@ function removeTargetsFromAnimations(targetsArray, animations) {
}
}

function removeTargets(targets) {
function removeTargetsFromInstance(targetsArray, instance) {
var animations = instance.animations;
var children = instance.children;
removeTargetsFromAnimations(targetsArray, animations);
for (var c = children.length; c--;) {
var child = children[c];
var childAnimations = child.animations;
removeTargetsFromAnimations(targetsArray, childAnimations);
if (!childAnimations.length && !child.children.length) { children.splice(c, 1); }
}
if (!animations.length && !children.length) { instance.pause(); }
}

function removeTargetsFromActiveInstances(targets) {
var targetsArray = parseTargets(targets);
for (var i = activeInstances.length; i--;) {
var instance = activeInstances[i];
var animations = instance.animations;
var children = instance.children;
removeTargetsFromAnimations(targetsArray, animations);
for (var c = children.length; c--;) {
var child = children[c];
var childAnimations = child.animations;
removeTargetsFromAnimations(targetsArray, childAnimations);
if (!childAnimations.length && !child.children.length) { children.splice(c, 1); }
}
if (!animations.length && !children.length) { instance.pause(); }
removeTargetsFromInstance(targetsArray, instance);
}
}

Expand Down Expand Up @@ -1286,7 +1290,7 @@ anime.speed = 1;
// TODO:#review: naming, documentation
anime.suspendWhenDocumentHidden = true;
anime.running = activeInstances;
anime.remove = removeTargets;
anime.remove = removeTargetsFromActiveInstances;
anime.get = getOriginalTargetValue;
anime.set = setTargetsValue;
anime.convertPx = convertPxToUnit;
Expand Down

0 comments on commit 4ea2ca2

Please sign in to comment.