Skip to content

Commit e5cbb51

Browse files
bodymovinbodymovin
bodymovin
andcommitted
Nnnn use advance and apply in js runtime
two separate commits: - use advanceAndApply - fix a memory leak with audio for not destroying the event listener Diffs= 9942df6a1d Nnnn use advance and apply in js runtime (#8728) Co-authored-by: hernan <[email protected]>
1 parent fec7d19 commit e5cbb51

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

.rive_head

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f9355c5d844b041f7ea6475ef99be7288f74404c
1+
9942df6a1dd563f388199860930f19a7ca31d299

js/src/rive.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@ class StateMachine {
407407
this.instance.advance(time);
408408
}
409409

410+
/**
411+
* Advances the state machine instance by a given time and apply changes to artboard.
412+
* @param time - the time to advance the animation by in seconds
413+
*/
414+
public advanceAndApply(time: number) {
415+
this.instance.advanceAndApply(time);
416+
}
417+
410418
/**
411419
* Returns the number of events reported from the last advance call
412420
* @returns Number of events reported
@@ -1564,6 +1572,9 @@ export class Rive {
15641572
// Whether the canvas element's size is 0
15651573
private _hasZeroSize = false;
15661574

1575+
// Audio event listener
1576+
private _audioEventListener: EventListener | null = null;
1577+
15671578
// Durations to generate a frame for the last second. Used for performance profiling.
15681579
public durations: number[] = [];
15691580
public frameTimes: number[] = [];
@@ -1774,11 +1785,12 @@ export class Rive {
17741785
private initializeAudio() {
17751786
// Initialize audio if needed
17761787
if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
1777-
if (this.artboard?.hasAudio) {
1778-
audioManager.add({
1788+
if (this.artboard?.hasAudio && this._audioEventListener === null) {
1789+
this._audioEventListener = {
17791790
type: EventType.AudioStatusChange,
17801791
callback: () => this.onSystemAudioChanged(),
1781-
});
1792+
};
1793+
audioManager.add(this._audioEventListener);
17821794
audioManager.establishAudio();
17831795
}
17841796
}
@@ -2009,13 +2021,15 @@ export class Rive {
20092021
}
20102022
}
20112023
}
2012-
stateMachine.advance(elapsedTime);
2024+
stateMachine.advanceAndApply(elapsedTime);
20132025
// stateMachine.instance.apply(this.artboard);
20142026
}
20152027

20162028
// Once the animations have been applied to the artboard, advance it
20172029
// by the elapsed time.
2018-
this.artboard.advance(elapsedTime);
2030+
if (this.animator.stateMachines.length == 0) {
2031+
this.artboard.advance(elapsedTime);
2032+
}
20192033

20202034
const { renderer } = this;
20212035
// Canvas must be wiped to prevent artifacts
@@ -2125,6 +2139,10 @@ export class Rive {
21252139
this.riveFile = null;
21262140
this.file = null;
21272141
this.deleteRiveRenderer();
2142+
if (this._audioEventListener !== null) {
2143+
audioManager.remove(this._audioEventListener);
2144+
this._audioEventListener = null;
2145+
}
21282146
}
21292147

21302148
/**

js/src/rive_advanced.mjs.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,12 @@ export declare class StateMachineInstance {
730730
* @param sec - Scrub the state machine instance by a number of seconds
731731
*/
732732
advance(sec: number): boolean;
733+
/**
734+
* Advances/scrubs the StateMachineInstance by the set amount of seconds. Note that this will
735+
* apply changes to the properties of objects in the Artboard.
736+
* @param sec - Scrub the state machine instance by a number of seconds
737+
*/
738+
advanceAndApply(sec: number): boolean;
733739
/**
734740
* Returns the number of states changed while the state machine played
735741
* @returns Number of states changed in the duration of the state machine played

wasm/src/bindings.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,11 @@ EMSCRIPTEN_BINDINGS(RiveWASM)
727727
optional_override([](rive::StateMachineInstance& self, float elapsedTime) -> bool {
728728
return self.advance(elapsedTime);
729729
}))
730+
.function("advanceAndApply",
731+
optional_override([](rive::StateMachineInstance& self, double seconds) -> bool {
732+
return self.advanceAndApply(seconds);
733+
}),
734+
allow_raw_pointers())
730735
.function("inputCount", &rive::StateMachineInstance::inputCount)
731736
.function("input", &rive::StateMachineInstance::input, allow_raw_pointers())
732737
.function("pointerDown",

0 commit comments

Comments
 (0)