Skip to content

Commit

Permalink
Merge pull request #859 from voxel51/fix/pause-animation-if-buffering (
Browse files Browse the repository at this point in the history
…#4870)

fix animation not pausing when buffering
  • Loading branch information
sashankaryal authored Sep 30, 2024
1 parent 7068e77 commit 169c3b3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/packages/core/src/components/Modal/ImaVidLooker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,23 @@ export const ImaVidLookerReact = React.memo(
return;
}

setPlayHeadState({ name: timelineName, state: "buffering" });

imaVidLookerRef.current.frameStoreController.enqueueFetch(
unprocessedBufferRange
);

imaVidLookerRef.current.frameStoreController.resumeFetch();

return new Promise<void>((resolve) => {
const fetchMoreListener = (e: CustomEvent) => {
if (
e.detail.id === imaVidLookerRef.current.frameStoreController.key
) {
if (storeBufferManager.containsRange(unprocessedBufferRange)) {
// todo: change playhead state in setFrameNumberAtom and not here
// if done here, store ref to last playhead status
setPlayHeadState({ name: timelineName, state: "paused" });
resolve();
window.removeEventListener(
"fetchMore",
Expand Down Expand Up @@ -250,6 +257,7 @@ export const ImaVidLookerReact = React.memo(
registerOnPauseCallback,
registerOnPlayCallback,
registerOnSeekCallbacks,
setPlayHeadState,
subscribe,
} = useCreateTimeline({
name: timelineName,
Expand Down Expand Up @@ -325,6 +333,7 @@ export const ImaVidLookerReact = React.memo(
position: "relative",
display: "flex",
flexDirection: "column",
overflowX: "hidden",
}}
>
<div
Expand Down
6 changes: 3 additions & 3 deletions app/packages/looker/src/elements/imavid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ export class ImaVidElement extends BaseElement<ImaVidState, HTMLImageElement> {
}

paintImageOnCanvas(image: HTMLImageElement) {
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
this.ctx?.setTransform(1, 0, 0, 1, 0, 0);

this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx?.clearRect(0, 0, this.canvas.width, this.canvas.height);

this.ctx.drawImage(image, 0, 0);
this.ctx?.drawImage(image, 0, 0);
}

async skipAndTryAgain(frameNumberToDraw: number, animate: boolean) {
Expand Down
1 change: 1 addition & 0 deletions app/packages/playback/src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "./constants";

export type PlayheadState =
| "buffering"
| "playing"
| "paused"
| "waitingToPlay"
Expand Down
12 changes: 12 additions & 0 deletions app/packages/playback/src/lib/use-create-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export const useCreateTimeline = (
return;
}

if (playHeadStateRef.current === "buffering") {
return;
}

setPlayHeadState({ name: timelineName, state: "playing" });
if (onPlayListenerRef.current) {
onPlayListenerRef.current();
Expand Down Expand Up @@ -364,6 +368,10 @@ export const useCreateTimeline = (
const key = e.key.toLowerCase();

if (key === " ") {
if (playHeadState === "buffering") {
return;
}

if (playHeadState === "paused") {
play();
} else {
Expand Down Expand Up @@ -448,6 +456,10 @@ export const useCreateTimeline = (
* Re-render all subscribers of the timeline with current frame number.
*/
refresh,
/**
* Set the playhead state of the timeline.
*/
setPlayHeadState,
/**
* Subscribe to the timeline.
*/
Expand Down
7 changes: 6 additions & 1 deletion app/packages/playback/src/views/svgs/buffering.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 169c3b3

Please sign in to comment.