diff --git a/docs/docs/animated-images.md b/docs/docs/animated-images.md index f2fe67a2f8..c2c54561ef 100644 --- a/docs/docs/animated-images.md +++ b/docs/docs/animated-images.md @@ -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(); ``` diff --git a/package/cpp/api/JsiSkAnimatedImage.h b/package/cpp/api/JsiSkAnimatedImage.h index 0a1aeeeae5..0e7efe55f0 100644 --- a/package/cpp/api/JsiSkAnimatedImage.h +++ b/package/cpp/api/JsiSkAnimatedImage.h @@ -35,6 +35,10 @@ class JsiSkAnimatedImage runtime, std::make_shared(getContext(), std::move(image))); } + JSI_HOST_FUNCTION(getFrameCount) { + return static_cast(getObject()->getFrameCount()); + } + JSI_HOST_FUNCTION(currentFrameDuration) { return static_cast(getObject()->currentFrameDuration()); } @@ -43,9 +47,10 @@ class JsiSkAnimatedImage return static_cast(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), diff --git a/package/src/renderer/__tests__/e2e/AnimatedImages.spec.tsx b/package/src/renderer/__tests__/e2e/AnimatedImages.spec.tsx index 1295b95fe1..80b533ed74 100644 --- a/package/src/renderer/__tests__/e2e/AnimatedImages.spec.tsx +++ b/package/src/renderer/__tests__/e2e/AnimatedImages.spec.tsx @@ -15,6 +15,11 @@ describe("Animated Images", () => { if (!animatedImage) { return false; } + + if (animatedImage.getFrameCount() !== 1) { + return false; + } + animatedImage.decodeNextFrame(); animatedImage.getCurrentFrame(); animatedImage.currentFrameDuration(); diff --git a/package/src/skia/types/AnimatedImage/AnimatedImage.ts b/package/src/skia/types/AnimatedImage/AnimatedImage.ts index 057386fc42..9d3ff0cfbc 100644 --- a/package/src/skia/types/AnimatedImage/AnimatedImage.ts +++ b/package/src/skia/types/AnimatedImage/AnimatedImage.ts @@ -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) } diff --git a/package/src/skia/web/JsiSkAnimatedImage.ts b/package/src/skia/web/JsiSkAnimatedImage.ts index a31eeae92c..7466dca6b6 100644 --- a/package/src/skia/web/JsiSkAnimatedImage.ts +++ b/package/src/skia/web/JsiSkAnimatedImage.ts @@ -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) {