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

feat: getFrameCount #2568

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion docs/docs/animated-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ import {useAnimatedImage} from "@shopify/react-native-skia";
const bird = useAnimatedImage(
require("../../assets/birdFlying.gif")
)!;
// SkAnimatedImage offers 3 methods: decodeNextFrame(), getCurrentFrame(), and currentFrameDuration()
// SkAnimatedImage offers 4 methods: decodeNextFrame(), getCurrentFrame(), currentFrameDuration(), and getFrameCount()
// getCurrentFrame() returns a regular SkImage
const image = bird.getCurrentFrame();
// decode the next frame
bird.decodeNextFrame();
// fetch the current frame number
const currentFrame = bird.currentFrameDuration();
// fetch the total number of frames
const frameCount = bird.getFrameCount();
```
7 changes: 6 additions & 1 deletion package/cpp/api/JsiSkAnimatedImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class JsiSkAnimatedImage
runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
}

JSI_HOST_FUNCTION(getFrameCount) {
return static_cast<int>(getObject()->getFrameCount());
}

JSI_HOST_FUNCTION(currentFrameDuration) {
return static_cast<int>(getObject()->currentFrameDuration());
}
Expand All @@ -43,9 +47,10 @@ class JsiSkAnimatedImage
return static_cast<int>(getObject()->decodeNextFrame());
}

EXPORT_JSI_API_TYPENAME(JsiSkAnimatedImage, "AnimatedImage")
EXPORT_JSI_API_TYPENAME(JsiSkAnimatedImage, AnimatedImage)
wcandillon marked this conversation as resolved.
Show resolved Hide resolved

JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImage, dispose),
JSI_EXPORT_FUNC(JsiSkAnimatedImage, getFrameCount),
JSI_EXPORT_FUNC(JsiSkAnimatedImage, getCurrentFrame),
JSI_EXPORT_FUNC(JsiSkAnimatedImage,
currentFrameDuration),
Expand Down
1 change: 1 addition & 0 deletions package/src/renderer/__tests__/e2e/AnimatedImages.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("Animated Images", () => {
if (!animatedImage) {
return false;
}
animatedImage.getFrameCount();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! maybe here instead of returning true you can return the frame count and then test its value in expect. I guess in this particular test it will be one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know exactly how many frames in this testing gif perhaps?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no but since it is test against both RN Skia and CanvasKit, if you put the result you get and all test pass it will be the correct result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so it has a single frame. added test for it

animatedImage.decodeNextFrame();
animatedImage.getCurrentFrame();
animatedImage.currentFrameDuration();
Expand Down
6 changes: 6 additions & 0 deletions package/src/skia/types/AnimatedImage/AnimatedImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ export interface SkAnimatedImage extends SkJSIInstance<"AnimatedImage"> {
*/
currentFrameDuration(): number;

/**
* Returns the number of frames in the animation.
*
*/
getFrameCount(): number;

// TODO - add the rest of the methods from the Skia API (see SkAnimatedImage.h)
}
4 changes: 4 additions & 0 deletions package/src/skia/web/JsiSkAnimatedImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class JsiSkAnimatedImage
return this.ref.currentFrameDuration();
}

getFrameCount() {
return this.ref.getFrameCount();
}

getCurrentFrame() {
const image = this.ref.makeImageAtCurrentFrame();
if (image === null) {
Expand Down
Loading