Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added optional onDraw method to play to run after the _render call. #266

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions spec/unit/movie/movie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ describe('Unit Tests ->', function () {
})
})

it('should call user provided `onDraw` after drawing', async function () {
mockTime(0, 500)

// Prepare options object with onDraw callback
let callCount = 0
await movie.play({
onStart: () => {
// The call count should be 0 at the start
expect(callCount).toBe(0)
expect(movie.currentTime).toBe(0)
},
onDraw: () => {
// Set the step to be 500 ms
// So we expect the currentTime to be 0.5 after the first draw
expect(movie.currentTime).toBe(0.5)
callCount++
}
})
// The call count should be 1 at the end
expect(callCount).toBe(1)
})

it('should have an active stream while streaming', async function () {
mockTime()

Expand Down
12 changes: 7 additions & 5 deletions src/movie/movie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ export class Movie {
* Plays the movie
*
* @param [options]
* @param [options.onDraw] Called when the current frame is drawn to the canvas
* @param [options.onStart] Called when the movie starts playing
* @param [options.duration] The duration of the movie to play in seconds
*
* @return Fulfilled when the movie is done playing, never fails
*/
async play (options: {
onDraw?: () => void,
clabe45 marked this conversation as resolved.
Show resolved Hide resolved
onStart?: () => void,
duration?: number,
} = {}): Promise<void> {
Expand All @@ -152,7 +153,7 @@ export class Movie {
await new Promise<void>(resolve => {
if (!this.renderingFrame) {
// Not rendering (and not playing), so play.
this._render(undefined, resolve)
this._render(undefined, resolve, options.onDraw)
}

// Stop rendering frame if currently doing so, because playing has higher
Expand Down Expand Up @@ -399,10 +400,10 @@ export class Movie {
* Processes one frame of the movie and draws it to the canvas
*
* @param [timestamp=performance.now()]
* @param [done=undefined] - Called when done playing or when the current
* frame is loaded
* @param [done=undefined] - Called when done playing or when the current frame is loaded
* @param [onFrameRender=undefined] - Called when the current frame is rendered
*/
private _render (timestamp = performance.now(), done = undefined) {
private _render (timestamp = performance.now(), done = undefined, onFrameRender = undefined) {
clearCachedValues(this)

if (!this.rendering) {
Expand Down Expand Up @@ -482,6 +483,7 @@ export class Movie {
this._renderBackground(timestamp)
this._renderLayers()
this._applyEffects()
onFrameRender?.()
} else {
// If we are recording, pause the media recorder until the movie is
// ready.
Expand Down
Loading