Skip to content

Commit

Permalink
fix(🏞️): add getFrameCount to animated images (#2568)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrysahaidak authored Aug 7, 2024
1 parent 30c1284 commit 682b32a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
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)

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
5 changes: 5 additions & 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,11 @@ describe("Animated Images", () => {
if (!animatedImage) {
return false;
}

if (animatedImage.getFrameCount() !== 1) {
return false;
}

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

0 comments on commit 682b32a

Please sign in to comment.