Skip to content

Commit

Permalink
feature(AnimationPlayer): add a callback ran at each animation frame
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie committed Sep 14, 2021
1 parent 36f0f40 commit 1280ce0
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Core/AnimationPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class AnimationPlayer extends THREE.EventDispatcher {
this.duration = 0;
this.state = PLAYER_STATE.STOP;
this.waitTimer = null;
this.callback = () => {};
}

isPlaying() {
Expand All @@ -71,6 +72,15 @@ class AnimationPlayer extends THREE.EventDispatcher {

// Public functions

/**
* Set the Player `callback` property. This callback is executed at each animation frame.
*
* @param {function} callback - The callback to execute at each animation frame.
*/
setCallback(callback) {
this.callback = callback;
}

/**
* Play one animation.
* If another animation is playing, it's stopped and the new animation is played.
Expand Down Expand Up @@ -118,6 +128,7 @@ class AnimationPlayer extends THREE.EventDispatcher {
if (this.keyframe < this.duration) {
this.keyframe++;
this.dispatchEvent({ type: 'animation-frame' });
this.callback();
} else {
this.state = PLAYER_STATE.END;
finishAnimation(this);
Expand Down

0 comments on commit 1280ce0

Please sign in to comment.