simplify helper

pull/10798/head
Rich Harris 2 years ago
parent f2e583260f
commit b62ad7bbb5

@ -36,11 +36,8 @@ class Animation {
#keyframes; #keyframes;
#duration; #duration;
#timeline_offset; #timeline_offset;
#reversed;
#target; #target;
#paused;
#offset = 0;
#finished = () => {}; #finished = () => {};
#cancelled = () => {}; #cancelled = () => {};
@ -57,8 +54,6 @@ class Animation {
this.#duration = options.duration || 0; this.#duration = options.duration || 0;
this.#timeline_offset = 0; this.#timeline_offset = 0;
this.#reversed = false;
this.#paused = false;
this.onfinish = () => {}; this.onfinish = () => {};
this.pending = true; this.pending = true;
this.currentTime = 0; this.currentTime = 0;
@ -69,8 +64,6 @@ class Animation {
} }
}; };
this.#offset = raf.time;
// Promise-like semantics, but call callbacks immediately on raf.tick // Promise-like semantics, but call callbacks immediately on raf.tick
this.finished = { this.finished = {
/** @param {() => void} callback */ /** @param {() => void} callback */
@ -88,22 +81,14 @@ class Animation {
} }
play() { play() {
this.#paused = false;
raf.animations.add(this); raf.animations.add(this);
this.playState = 'running'; this.playState = 'running';
this._update(); this._update();
} }
_update() { _update() {
if (this.#reversed) { console.log('_update', raf.time);
if (this.#timeline_offset === 0) {
this.currentTime = this.#duration - raf.time;
} else {
this.currentTime = this.#timeline_offset + (this.#timeline_offset - raf.time);
}
} else {
this.currentTime = raf.time - this.#timeline_offset; this.currentTime = raf.time - this.#timeline_offset;
}
const target_frame = this.currentTime / this.#duration; const target_frame = this.currentTime / this.#duration;
this._applyKeyFrame(target_frame); this._applyKeyFrame(target_frame);
@ -120,19 +105,12 @@ class Animation {
const keyframes = this.#keyframes; const keyframes = this.#keyframes;
const keyframes_size = keyframes.length - 1; const keyframes_size = keyframes.length - 1;
const frame = keyframes[Math.min(keyframes_size, Math.floor(keyframes.length * target_frame))]; const frame = keyframes[Math.min(keyframes_size, Math.floor(keyframes.length * target_frame))];
for (let prop in frame) { for (let prop in frame) {
// @ts-ignore // @ts-ignore
this.#target.style[prop] = frame[prop]; this.#target.style[prop] = frame[prop];
} }
if (this.#reversed) {
if (this.currentTime <= 0) {
this.finish();
for (let prop in frame) {
// @ts-ignore
this.#target.style[prop] = null;
}
}
} else {
if (this.currentTime >= this.#duration) { if (this.currentTime >= this.#duration) {
this.finish(); this.finish();
for (let prop in frame) { for (let prop in frame) {
@ -141,21 +119,16 @@ class Animation {
} }
} }
} }
}
finish() { finish() {
this.onfinish(); this.onfinish();
this.currentTime = this.#reversed ? 0 : this.#duration; this.currentTime = this.#duration;
if (this.#reversed) {
raf.animations.delete(this);
}
this.playState = 'idle'; this.playState = 'idle';
} }
cancel() { cancel() {
this.#paused = true;
if (this.currentTime > 0 && this.currentTime < this.#duration) { if (this.currentTime > 0 && this.currentTime < this.#duration) {
this._applyKeyFrame(this.#reversed ? this.#keyframes.length - 1 : 0); this._applyKeyFrame(0);
} }
this.#cancelled(); this.#cancelled();
@ -163,18 +136,8 @@ class Animation {
} }
pause() { pause() {
this.#paused = true;
this.playState = 'paused'; this.playState = 'paused';
} }
reverse() {
if (this.#paused && !raf.animations.has(this)) {
raf.animations.add(this);
}
this.#timeline_offset = this.currentTime;
this.#reversed = !this.#reversed;
this.playState = 'running';
}
} }
/** /**

Loading…
Cancel
Save