Skip to content

armgit / anime

Public Imported from GitHub
0

JavaScript animation engine

FIx waapi .then() promise not firing if animation is paused an manually seeked #1001

Julian Garnier58e723b292eb
diff --git a/lib/anime.esm.js b/lib/anime.esm.js
index 532bfb2a..d591806b 100644
--- a/lib/anime.esm.js
+++ b/lib/anime.esm.js
@@ -4123,7 +4123,13 @@ class WAAPIAnimation {
/** @param {Number} time */
set currentTime(time) {
const t = time * (globals.timeScale === 1 ? 1 : K);
- this.forEach(anim => anim.currentTime = t);
+ this.forEach(anim => {
+ // Make sure the animation playState is not 'paused' in order to properly trigger an onfinish callback.
+ // The "paused" play state supersedes the "finished" play state; if the animation is both paused and finished, the "paused" state is the one that will be reported.
+ // https://developer.mozilla.org/en-US/docs/Web/API/Animation/finish_event
+ if (t >= this.duration) anim.play();
+ anim.currentTime = t;
+ });
}
get progress() {
diff --git a/src/waapi.js b/src/waapi.js
index 1bb73380..e04b8418 100644
--- a/src/waapi.js
+++ b/src/waapi.js
@@ -36,6 +36,7 @@ import {
import {
isBrowser,
+ doc,
K,
noop,
emptyString,
@@ -471,7 +472,13 @@ export class WAAPIAnimation {
/** @param {Number} time */
set currentTime(time) {
const t = time * (globals.timeScale === 1 ? 1 : K);
- this.forEach(anim => anim.currentTime = t);
+ this.forEach(anim => {
+ // Make sure the animation playState is not 'paused' in order to properly trigger an onfinish callback.
+ // The "paused" play state supersedes the "finished" play state; if the animation is both paused and finished, the "paused" state is the one that will be reported.
+ // https://developer.mozilla.org/en-US/docs/Web/API/Animation/finish_event
+ if (t >= this.duration) anim.play();
+ anim.currentTime = t;
+ });
}
get progress() {
diff --git a/tests/suites/waapi.test.js b/tests/suites/waapi.test.js
index d4b68866..3e084cc4 100644
--- a/tests/suites/waapi.test.js
+++ b/tests/suites/waapi.test.js
@@ -11,8 +11,6 @@ import {
suite('WAAPI', () => {
- // Test fore
-
CSS.registerProperty({
name: '--translateX',
syntax: '<length-percentage>',
@@ -31,6 +29,21 @@ suite('WAAPI', () => {
})
});
+ test('Calling .complete() on an animation should trigger the .then() Promise', resolve => {
+ const instance = waapi.animate('#target-id', {
+ scale: 1.5,
+ duration: 500,
+ autoplay: false,
+ });
+ instance.then(self => {
+ expect(self.completed).to.equal(true);
+ resolve();
+ });
+ setTimeout(() => {
+ instance.complete();
+ }, 10);
+ });
+
test('Animate multiple elements', resolve => {
const targets = utils.$('.target-class');
const animation = waapi.animate(targets, {
@@ -99,24 +112,24 @@ suite('WAAPI', () => {
test('Seek an animation', () => {
const targets = utils.$('.target-class');
const animation = waapi.animate(targets, {
- transform: `translateX(100px)`,
+ translate: `100px`,
duration: 10,
autoplay: false,
ease: 'linear',
});
expect(animation.currentTime).to.equal(0);
animation.seek(5).commitStyles();
- expect(utils.get(targets[0], 'x')).to.equal('50px');
+ expect(utils.get(targets[0], 'translate')).to.equal('50px');
expect(animation.currentTime).to.equal(5);
animation.seek(animation.duration).commitStyles();
expect(animation.currentTime).to.equal(animation.duration);
- expect(utils.get(targets[0], 'x')).to.equal('100px');
+ expect(utils.get(targets[0], 'translate')).to.equal('100px');
});
test('Set and get progress on an animation', () => {
const targets = utils.$('.target-class');
const animation = waapi.animate(targets, {
- transform: `translateX(100px)`,
+ translate: `100px`,
duration: 10,
autoplay: false,
ease: 'linear',
@@ -124,11 +137,11 @@ suite('WAAPI', () => {
expect(animation.progress).to.equal(0);
animation.progress = .5;
animation.commitStyles();
- expect(utils.get(targets[0], 'x')).to.equal('50px');
+ expect(utils.get(targets[0], 'translate')).to.equal('50px');
expect(animation.progress).to.equal(.5);
animation.progress = 1;
animation.commitStyles();
- expect(utils.get(targets[0], 'x')).to.equal('100px');
+ expect(utils.get(targets[0], 'translate')).to.equal('100px');
});
test('Individual transforms', resolve => {
diff --git a/types/index.js b/types/index.js
index cc4294e0..671cfef2 100644
--- a/types/index.js
+++ b/types/index.js
@@ -3518,7 +3518,14 @@ class WAAPIAnimation {
/** @param {Number} time */
set currentTime(time) {
const t = time * (globals.timeScale === 1 ? 1 : K);
- this.forEach(anim => anim.currentTime = t);
+ this.forEach(anim => {
+ // Make sure the animation playState is not 'paused' in order to properly trigger an onfinish callback.
+ // The "paused" play state supersedes the "finished" play state; if the animation is both paused and finished, the "paused" state is the one that will be reported.
+ // https://developer.mozilla.org/en-US/docs/Web/API/Animation/finish_event
+ if (t >= this.duration)
+ anim.play();
+ anim.currentTime = t;
+ });
}
get progress() {
return this.currentTime / this.duration;