-
Notifications
You must be signed in to change notification settings - Fork 364
[Image] Pause and play image gifs #3432
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
Open
catandthemachines
wants to merge
45
commits into
main
Choose a base branch
from
catjohnson/lems-3735
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 32 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
6c4d657
[catjohnson/lems-3735] Initial implementation for pause and play.
catandthemachines cc54bc1
[catjohnson/lems-3735] Fixing frame issues on load.
catandthemachines e8a3007
[catjohnson/lems-3735] Remove console log.
catandthemachines 47446b6
[catjohnson/lems-3735] Adding tests
catandthemachines aa13292
[catjohnson/lems-3735] Fixing linting errors.
catandthemachines 8800384
[catjohnson/lems-3735] Fixed a few more linting issues.
catandthemachines 0726379
[catjohnson/lems-3735] docs(changeset): [Image] New pause and play fu…
catandthemachines 9c11caa
[catjohnson/lems-3735] Update snapshots.
catandthemachines c85801a
[catjohnson/lems-3735] Adding gif image looping mechanism.
catandthemachines 0e0e21e
[catjohnson/lems-3735] Saving and storing gifImageElement.
catandthemachines e6573d5
[catjohnson/lems-3735] Adding a few more tests.
catandthemachines 3587e31
[catjohnson/lems-3735] Merge branch 'main' into catjohnson/lems-3735
catandthemachines af67d85
[catjohnson/lems-3735] Updating gif implementation.
catandthemachines 0b118d5
[catjohnson/lems-3735] update comment.
catandthemachines 1ba3f6f
[catjohnson/lems-3735] Hiding the canvas element from screen readers.
catandthemachines 2d3c622
[catjohnson/lems-3735] Fixing gif loading edgecase.
catandthemachines 3c3ce41
[catjohnson/lems-3735] Merge branch 'main' into catjohnson/lems-3735
catandthemachines 3004972
[catjohnson/lems-3735] A few tweeks.
catandthemachines f57d979
[catjohnson/lems-3735] Fixing comment.
catandthemachines 517aa6c
[catjohnson/lems-3735] Updating the name of the gif ref.
catandthemachines d141d86
[catjohnson/lems-3735] Fixing staggered gif playing in zoomed view.
catandthemachines 363b955
[catjohnson/lems-3735] Current Implementation for using gifuct-js to …
catandthemachines 56e321a
[catjohnson/lems-3735] Undoing testid for loading spinner. Can come b…
catandthemachines 962c3bf
[catjohnson/lems-3735] Removing unused image loader properties.
catandthemachines cdce7dd
[catjohnson/lems-3735] Updating and removing more stuff.
catandthemachines aa280bb
[catjohnson/lems-3735] Removing zoom from gif images.
catandthemachines 5759436
[catjohnson/lems-3735] Moving GifLogic to it's own file.
catandthemachines 8417f85
[catjohnson/lems-3735] changing logic around pausing and looping.
catandthemachines 3cacf72
[catjohnson/lems-3735] Removing unnecessary test utility additions.
catandthemachines 62f5aa7
[catjohnson/lems-3735] Update tests.
catandthemachines e90dc8a
[catjohnson/lems-3735] Cleaning up tests.
catandthemachines 81005e0
[catjohnson/lems-3735] Updating the name of the baseCanvas to better …
catandthemachines 6b8cc27
[catjohnson/lems-3735] Improving gif tests.
catandthemachines 5adb76c
[catjohnson/lems-3735] fixing all the canvas scalling to be cleaner a…
catandthemachines c2881a2
[catjohnson/lems-3735] Changing onPause, to onLoop. Much more aligned…
catandthemachines 1995ff2
[catjohnson/lems-3735] Updating comment to highlight where this duel …
catandthemachines b3ac72e
[catjohnson/lems-3735] Improving restart function.
catandthemachines b03c395
[catjohnson/lems-3735] Merge branch 'main' into catjohnson/lems-3735
catandthemachines 75a9672
[catjohnson/lems-3735] Cleaning up a few lines.
catandthemachines 16a9832
[catjohnson/lems-3735] Updating variable names and comments.
catandthemachines b9082ca
[catjohnson/lems-3735] Fixing -1 for no frames yet and trying to tamp…
catandthemachines feaa027
[catjohnson/lems-3735] Adding onload functionality.
catandthemachines de190f5
[catjohnson/lems-3735] A few fixes.
catandthemachines a09f12a
[catjohnson/lems-3735] removing cate statement.
catandthemachines 2fc1031
[catjohnson/lems-3735] Fixing some tests.
catandthemachines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@khanacademy/perseus": patch | ||
| --- | ||
|
|
||
| [Image] New pause and play functionality for image gifs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
326 changes: 326 additions & 0 deletions
326
packages/perseus/src/components/__tests__/gif-image.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,326 @@ | ||
| import {act, render, screen} from "@testing-library/react"; | ||
| import {parseGIF, decompressFrames} from "gifuct-js"; | ||
| import * as React from "react"; | ||
|
|
||
| import GifImage from "../gif-image"; | ||
|
|
||
| jest.mock("gifuct-js"); | ||
|
|
||
| const GIF_SRC = "https://cdn.kastatic.org/test.gif"; | ||
|
|
||
| // A minimal fake frame from gifuct-js with a 50ms delay. | ||
| const fakeFrame = { | ||
| patch: new Uint8ClampedArray(4), // 1x1 RGBA | ||
| delay: 50, | ||
| dims: {width: 1, height: 1, top: 0, left: 0}, | ||
| disposalType: 0, | ||
| }; | ||
|
|
||
| describe("GifImage", () => { | ||
| beforeEach(() => { | ||
| // Make gifuct-js return two fake frames (100ms total loop). | ||
| (parseGIF as jest.Mock).mockReturnValue({}); | ||
| (decompressFrames as jest.Mock).mockReturnValue([fakeFrame, fakeFrame]); | ||
| // jsdom doesn't implement canvas getContext or ImageData. | ||
| jest.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({ | ||
| putImageData: jest.fn(), | ||
| clearRect: jest.fn(), | ||
| drawImage: jest.fn(), | ||
| imageSmoothingEnabled: true, | ||
| } as Partial<CanvasRenderingContext2D> as CanvasRenderingContext2D); | ||
| global.fetch = jest.fn(() => | ||
| Promise.resolve({ | ||
| ok: true, | ||
| arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)), | ||
| }), | ||
| ) as jest.Mock; | ||
| // @ts-expect-error - jsdom doesn't have ImageData | ||
| global.ImageData = class ImageData { | ||
| data: Uint8ClampedArray; | ||
| width: number; | ||
| height: number; | ||
| constructor( | ||
| data: Uint8ClampedArray, | ||
| width: number, | ||
| height: number, | ||
| ) { | ||
| this.data = data; | ||
| this.width = width; | ||
| this.height = height; | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| it("renders a canvas when paused", () => { | ||
| // Arrange, Act | ||
| render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| // Assert | ||
| expect(screen.getByTestId("gif-canvas")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders a canvas when playing", () => { | ||
| // Arrange, Act | ||
| render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={true} | ||
| onPause={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| // Assert | ||
| expect(screen.getByTestId("gif-canvas")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| describe("auto-pause", () => { | ||
| it("calls onPause after all frames have been rendered", async () => { | ||
| // Arrange | ||
| const onPause = jest.fn(); | ||
| render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={true} | ||
| onPause={onPause} | ||
| />, | ||
| ); | ||
|
|
||
| // Flush the fetch → arrayBuffer → decodeGifFrames → .then() | ||
| // promise chain so playback starts. | ||
| // eslint-disable-next-line testing-library/no-unnecessary-act | ||
| await act(async () => { | ||
| // Need enough microtask ticks for the full async chain: | ||
| // fetch() → arrayBuffer() → async return → .then() | ||
| for (let i = 0; i < 10; i++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }); | ||
|
|
||
| // Act — advance enough for both 50ms frames to render. | ||
| // RAF fires at ~16ms intervals, so we need enough ticks | ||
| // for two frames plus the initial timestamp capture. | ||
| act(() => { | ||
| jest.advanceTimersByTime(200); | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(onPause).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does not call onPause again after pausing", async () => { | ||
| // Arrange | ||
| const onPause = jest.fn(); | ||
| const {rerender} = render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={true} | ||
| onPause={onPause} | ||
| />, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line testing-library/no-unnecessary-act | ||
| await act(async () => { | ||
| for (let i = 0; i < 10; i++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }); | ||
| act(() => { | ||
| jest.advanceTimersByTime(200); | ||
| }); | ||
| expect(onPause).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Act - pause the GIF | ||
| rerender( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={onPause} | ||
| />, | ||
| ); | ||
| act(() => { | ||
| jest.advanceTimersByTime(200); | ||
| }); | ||
|
|
||
| // Assert - onPause should not have been called again | ||
| expect(onPause).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| it("resets to frame 0 after loop completes and is replayed", async () => { | ||
| // Arrange — play through one full loop | ||
| const onPause = jest.fn(); | ||
| const {rerender} = render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={true} | ||
| onPause={onPause} | ||
| />, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line testing-library/no-unnecessary-act | ||
| await act(async () => { | ||
| for (let i = 0; i < 10; i++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }); | ||
| act(() => { | ||
| jest.advanceTimersByTime(200); | ||
| }); | ||
| expect(onPause).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Act — sync parent state to paused, then play again | ||
| rerender( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={onPause} | ||
| />, | ||
| ); | ||
| rerender( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={true} | ||
| onPause={onPause} | ||
| />, | ||
| ); | ||
|
|
||
| // Advance enough for both frames to render from frame 0. | ||
| act(() => { | ||
| jest.advanceTimersByTime(200); | ||
| }); | ||
| // The loop completed a second time — onPause fires again, | ||
| // confirming it played through both frames from the start. | ||
| expect(onPause).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("renders the hidden base canvas", () => { | ||
| // Arrange, Act | ||
| render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| // Assert | ||
| const patchCanvas = screen.getByTestId("gif-base-canvas"); | ||
| expect(patchCanvas).toBeInTheDocument(); | ||
| expect(patchCanvas).not.toBeVisible(); | ||
| }); | ||
|
|
||
| it("decodes gif frames on mount", async () => { | ||
| // Arrange, Act | ||
| render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line testing-library/no-unnecessary-act | ||
| await act(async () => { | ||
| for (let i = 0; i < 10; i++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(global.fetch).toHaveBeenCalledWith(GIF_SRC); | ||
| expect(parseGIF).toHaveBeenCalled(); | ||
| expect(decompressFrames).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("re-decodes frames when src changes", async () => { | ||
| // Arrange | ||
| const {rerender} = render( | ||
| <GifImage | ||
| src={GIF_SRC} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line testing-library/no-unnecessary-act | ||
| await act(async () => { | ||
| for (let i = 0; i < 10; i++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }); | ||
|
|
||
| // Act — change the src | ||
| const newSrc = "https://cdn.kastatic.org/other.gif"; | ||
| rerender( | ||
| <GifImage | ||
| src={newSrc} | ||
| alt="test gif" | ||
| width={500} | ||
| height={285} | ||
| scale={1} | ||
| isPlaying={false} | ||
| onPause={jest.fn()} | ||
| />, | ||
| ); | ||
|
|
||
| // eslint-disable-next-line testing-library/no-unnecessary-act | ||
| await act(async () => { | ||
| for (let i = 0; i < 10; i++) { | ||
| await Promise.resolve(); | ||
| } | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(global.fetch).toHaveBeenCalledWith(newSrc); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.