Skip to content

Commit

Permalink
Added onTick event to animations. (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterlucas authored Oct 17, 2024
2 parents 4afb535 + 200ded5 commit 7c6f4c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/common/CommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,10 @@ export interface FrameTickPayload {
time: number;
delta: number;
}

/**
* Event payload for when a an animtion tick event is emitted
*/
export interface AnimationTickPayload {
progress: number;
}
4 changes: 4 additions & 0 deletions src/core/animations/CoreAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ export class CoreAnimation extends EventEmitter {
}
}

if (this.progress < 1) {
this.emit('tick', { progress: this.progress });
}

if (this.progress === 1) {
this.emit('finished', {});
}
Expand Down
12 changes: 12 additions & 0 deletions src/core/animations/CoreAnimationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { AnimationManager } from './AnimationManager.js';
import type { CoreAnimation } from './CoreAnimation.js';
import { assertTruthy } from '../../utils.js';
import { EventEmitter } from '../../common/EventEmitter.js';
import type { AnimationTickPayload } from '../../common/CommonTypes.js';

export class CoreAnimationController
extends EventEmitter
Expand All @@ -50,6 +51,7 @@ export class CoreAnimationController
// Bind event handlers
this.onAnimating = this.onAnimating.bind(this);
this.onFinished = this.onFinished.bind(this);
this.onTick = this.onTick.bind(this);
}

start(): IAnimationController {
Expand Down Expand Up @@ -93,6 +95,7 @@ export class CoreAnimationController
// Hook up event listeners
this.animation.once('finished', this.onFinished);
this.animation.on('animating', this.onAnimating);
this.animation.on('tick', this.onTick);
// Then register the animation
this.manager.registerAnimation(this.animation);
}
Expand All @@ -103,6 +106,7 @@ export class CoreAnimationController
// Then unhook event listeners
this.animation.off('finished', this.onFinished);
this.animation.off('animating', this.onAnimating);
this.animation.off('tick', this.onTick);
}

private makeStoppedPromise(): void {
Expand Down Expand Up @@ -142,4 +146,12 @@ export class CoreAnimationController
this.state = 'running';
this.emit('animating', this);
}

private onTick(
this: CoreAnimationController,
_animation: CoreAnimation,
data: AnimationTickPayload,
): void {
this.emit('tick', data);
}
}

0 comments on commit 7c6f4c3

Please sign in to comment.