Skip to content

Commit

Permalink
feature: Add onAdvance hook into render loop to report advancing has …
Browse files Browse the repository at this point in the history
…occurred

Adding a hook to report back the end of a render loop (as requested [here](rive-app/rive-react#186)). This will add consistency with iOS and Android callbacks too.

Diffs=
2de1f249c feature: Add onAdvance hook into render loop to report advancing has occurred (#5520)

Co-authored-by: Zachary Plata <[email protected]>
  • Loading branch information
zplata and zplata committed Jul 14, 2023
1 parent 60f0a9f commit b55e640
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1292ebe56e5996ed2baec99732d630651f00cec0
2de1f249c61ee0d1beef0ff6e10752f0a4d4ad3a
18 changes: 17 additions & 1 deletion js/src/rive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,13 @@ class Animator {
});
}
}

public handleAdvancing(time: number) {
this.eventManager.fire({
type: EventType.Advance,
data: time,
});
}
}

// #endregion
Expand All @@ -778,13 +785,14 @@ export enum EventType {
Stop = "stop",
Loop = "loop",
Draw = "draw",
Advance = "advance",
StateChange = "statechange",
}

// Event fired by Rive
export interface Event {
type: EventType;
data?: string | string[] | LoopEvent;
data?: string | string[] | LoopEvent | number;
}

/**
Expand Down Expand Up @@ -944,6 +952,7 @@ export interface RiveParameters {
onStop?: EventCallback;
onLoop?: EventCallback;
onStateChange?: EventCallback;
onAdvance?: EventCallback;
/**
* @deprecated Use `onLoad()` instead
*/
Expand Down Expand Up @@ -1071,6 +1080,7 @@ export class Rive {
if (params.onLoop) this.on(EventType.Loop, params.onLoop);
if (params.onStateChange)
this.on(EventType.StateChange, params.onStateChange);
if (params.onAdvance) this.on(EventType.Advance, params.onAdvance);

/**
* @deprecated Use camelCase'd versions instead.
Expand Down Expand Up @@ -1382,6 +1392,9 @@ export class Rive {
// Check for any state machines that had a state change
this.animator.handleStateChanges();

// Report advanced time
this.animator.handleAdvancing(elapsedTime);

// Add duration to create frame to durations array
this.frameCount++;
const after = performance.now();
Expand Down Expand Up @@ -1511,6 +1524,9 @@ export class Rive {
});
return;
}
if (this.eventCleanup) {
this.eventCleanup();
}
this.animator.pause(animationNames);
}

Expand Down
49 changes: 49 additions & 0 deletions js/test/rive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,26 @@ test("Stop events are received", (done) => {
});
});

test("Advance events are received", (done) => {
const canvas = document.createElement("canvas");
let hasAdvancedOnce = false;
new rive.Rive({
canvas: canvas,
buffer: oneShotRiveFileBuffer,
autoplay: true,
onAdvance: (event) => {
expect(event.type).toBe(rive.EventType.Advance);
if (hasAdvancedOnce) {
expect(event.data).toBeGreaterThan(0);
done();
}
if (!hasAdvancedOnce) {
hasAdvancedOnce = true;
}
},
});
});

test("Events can be unsubscribed from", (done) => {
const canvas = document.createElement("canvas");

Expand Down Expand Up @@ -1025,6 +1045,35 @@ test("Playing state machines report when states have changed", (done) => {
});
});

test("Advance event is not triggered when state machine is paused", (done) => {
const canvas = document.createElement("canvas");
const hasAdvancedMock = jest.fn();

const r = new rive.Rive({
canvas: canvas,
buffer: stateMachineFileBuffer,
artboard: "MyArtboard",
stateMachines: "StateMachine",
autoplay: true,
onPlay: () => {
setTimeout(() => {
r.pause();
}, 100);
},
onPause: () => {
const advancedCallbackCount = hasAdvancedMock.mock.calls.length;
setTimeout(() => {
// Rive draws one more frame before pausing at the end of a render loop
expect(hasAdvancedMock.mock.calls.length).toBe(
advancedCallbackCount + 1
);
done();
}, 50);
},
onAdvance: hasAdvancedMock,
});
});

// #endregion

// #region scrubbing
Expand Down

0 comments on commit b55e640

Please sign in to comment.