Does A-Frame document a cancel method for in-progress animations?
0 reputation · 28 Jul 2021, 14:47 UTC
0 reputation · 28 Jul 2021, 14:47 UTC
The goal is to stop an A-Frame animation mid-playback and return the animated entity to its initial attribute values without destroying or re-adding the entity.
The animation component exposes documented pause() and play() methods, but a cancel() method that aborts the current tween and resets progress is not part of the documented public API. Pausing freezes the animation at its current value, while removing the entity may leave pending timers or animation frames unresolved unless the component explicitly clears them. Cancellation behavior may differ across A-Frame releases and should be verified against the target version.
Assuming standard A-Frame 1.x behavior and browser APIs such as requestAnimationFrame, what is the supported pattern for cancelling an animation and resetting its progress?
pause() on an animation component reset the animated property to its starting value?A-Frame's animation component does not document a cancel(), stop(), or reset() method in any 1.x release. The only supported way to abort an in-progress animation and reset the target attribute to its starting value is to remove the animation component via el.removeAttribute('animation__<name>'), then restore the attribute manually if the entity must persist.
pause() freezes, does not reset. The animated property stays at the interpolated value where it was paused.pause() and play() only. Documented events are animationbegin, animationcomplete, animationloop, and animationpause — none expose forced cancellation.remove() lifecycle hook (called automatically on removeAttribute or entity removal) clears the internal anime.js instance and cancels pending requestAnimationFrame callbacks, preventing orphaned timers.from property if explicitly set).el.removeAttribute('animation__<name>') to stop and clean up the animation.el.setAttribute('position', initialPos) (or whichever attribute was animated).el.setAttribute('animation__<name>', { ... }) if you need to restart with different parameters.// Example: cancel a named animation "move" on an entity
const el = document.querySelector('#box');
const initialPos = el.getAttribute('position'); // capture before or at start
el.removeAttribute('animation__move'); // triggers component.remove(), clears RAF
el.setAttribute('position', initialPos); // reset to start value
animation__foo, animation__bar; target the specific name.setAttribute in a setTimeout(..., 0) or requestAnimationFrame to avoid race conditions.el.components['animation__move'].tween exposes the internal anime.js instance, but its shape changes across anime.js versions and is not a supported API.packages/core/src/components/animation.js and inspect pause(), play(), and remove().animation__move, call pause(), observe the attribute value, then call removeAttribute('animation__move') and verify cleanup.Diagnostic detail needed only if behavior differs: Which exact A-Frame version (e.g., 1.4.2, 1.6.0) are you targeting? The component removal pattern is stable across 1.x, but private internals shift with anime.js upgrades.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 29 Jul 2021, 02:03 UTC
A-Frame's remove() lifecycle hook runs when the animation component is detached, but it stops the tween without restoring the attribute to its pre-animation value. If you remove animation__move and immediately re-add it with setAttribute, the new component may autoplay unless autoplay: false is included in the config. To verify cancellation in your target build, animate a long-duration property, call el.removeAttribute('animation__name'), then read the attribute in the next frame: it should hold the frozen value, not continue interpolating. Also confirm that animationcomplete does not fire after removal, since relying on that event for cleanup would skip the reset step entirely.