Skip to content

Commit

Permalink
fix: 动画未播放完成时再次调用则清除计时器
Browse files Browse the repository at this point in the history
  • Loading branch information
ACERY1 committed Jun 1, 2021
1 parent 26d15d9 commit c5b4a9d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/components/src/canvas/animation/timelime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const requestAnimationFrame = typeof window === 'object' && window.requestAnimat
return setTimeout(fn, 16);
};

const cancelAnimationFrame = typeof window === 'object' && window.cancelAnimationFrame ? window.cancelAnimationFrame : function(number) {
return clearTimeout(number);
};

const clock = typeof performance === 'object' && performance.now ? performance : Date;

type UpdateCallback = (time: number) => void;
Expand All @@ -16,15 +20,17 @@ class Timeline {
pausedTime = 0;
// 动画持续时间
duration: number;
// 计时器id
animationFrameNumber;

play(duration: number, onUpdate: UpdateCallback, onEnd: EndCallback) {
if (duration <= 0) {
onEnd();
return;
}
// 上次动画未结束
// 上次动画未结束,则清除计时器,停止播放
if (this.playing) {
return;
this.abort();
}
this.duration = duration;
const {
Expand Down Expand Up @@ -57,9 +63,9 @@ class Timeline {
return;
}
onUpdate(time);
requestAnimationFrame(play);
this.animationFrameNumber = requestAnimationFrame(play);
};
requestAnimationFrame(play);
this.animationFrameNumber = requestAnimationFrame(play);
}

pause() {
Expand All @@ -69,6 +75,14 @@ class Timeline {
stop() {
this.playing = false;
}

abort() {
if(!this.animationFrameNumber) {
return;
}
cancelAnimationFrame(this.animationFrameNumber);
this.animationFrameNumber = null;
}
}

export default Timeline;

0 comments on commit c5b4a9d

Please sign in to comment.