From 623e96e4c3455081473a68dacbdeed321a791562 Mon Sep 17 00:00:00 2001 From: Michiel van der Geest Date: Tue, 15 Oct 2024 14:50:54 +0200 Subject: [PATCH 1/2] Added onTick event to animations. --- src/core/animations/CoreAnimation.ts | 4 ++++ src/core/animations/CoreAnimationController.ts | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/core/animations/CoreAnimation.ts b/src/core/animations/CoreAnimation.ts index af78737d..72879711 100644 --- a/src/core/animations/CoreAnimation.ts +++ b/src/core/animations/CoreAnimation.ts @@ -326,6 +326,10 @@ export class CoreAnimation extends EventEmitter { } } + if (this.progress < 1) { + this.emit('tick', {}); + } + if (this.progress === 1) { this.emit('finished', {}); } diff --git a/src/core/animations/CoreAnimationController.ts b/src/core/animations/CoreAnimationController.ts index ff424a9e..187456b2 100644 --- a/src/core/animations/CoreAnimationController.ts +++ b/src/core/animations/CoreAnimationController.ts @@ -50,6 +50,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 { @@ -93,6 +94,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); } @@ -103,6 +105,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 { @@ -142,4 +145,8 @@ export class CoreAnimationController this.state = 'running'; this.emit('animating', this); } + + private onTick(this: CoreAnimationController): void { + this.emit('tick'); + } } From 200ded5e12e29da7b7be6d6cd403316511facf59 Mon Sep 17 00:00:00 2001 From: Michiel van der Geest Date: Tue, 15 Oct 2024 22:02:49 +0200 Subject: [PATCH 2/2] Added progress to tick animation event. --- src/common/CommonTypes.ts | 7 +++++++ src/core/animations/CoreAnimation.ts | 2 +- src/core/animations/CoreAnimationController.ts | 9 +++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/common/CommonTypes.ts b/src/common/CommonTypes.ts index e2fa4c02..1de22d5b 100644 --- a/src/common/CommonTypes.ts +++ b/src/common/CommonTypes.ts @@ -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; +} diff --git a/src/core/animations/CoreAnimation.ts b/src/core/animations/CoreAnimation.ts index 72879711..6580b3ed 100644 --- a/src/core/animations/CoreAnimation.ts +++ b/src/core/animations/CoreAnimation.ts @@ -327,7 +327,7 @@ export class CoreAnimation extends EventEmitter { } if (this.progress < 1) { - this.emit('tick', {}); + this.emit('tick', { progress: this.progress }); } if (this.progress === 1) { diff --git a/src/core/animations/CoreAnimationController.ts b/src/core/animations/CoreAnimationController.ts index 187456b2..d9969dac 100644 --- a/src/core/animations/CoreAnimationController.ts +++ b/src/core/animations/CoreAnimationController.ts @@ -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 @@ -146,7 +147,11 @@ export class CoreAnimationController this.emit('animating', this); } - private onTick(this: CoreAnimationController): void { - this.emit('tick'); + private onTick( + this: CoreAnimationController, + _animation: CoreAnimation, + data: AnimationTickPayload, + ): void { + this.emit('tick', data); } }