From 38bd34b01fb4b1c5dab7aa2d9cd1fa6dfbc32625 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 13 May 2024 12:22:55 +0900 Subject: [PATCH] animation/lottie: improved the precision of frame values. Refined the logic for updating frame numbers to ensure greater accuracy in value precision. issue: https://github.com/thorvg/thorvg/issues/2266 --- inc/thorvg.h | 1 + src/bindings/capi/thorvg_capi.h | 1 + src/loaders/lottie/tvgLottieLoader.cpp | 16 +++++++++------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 5b152810..d56c8ada 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -1805,6 +1805,7 @@ public: * * @note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value * is less than 0.001. In such cases, it returns @c Result::InsufficientCondition. + * Values less than 0.001 may be disregarded and may not be accurately retained by the Animation. * * @see totalFrame() * diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index 52dd5518..da740f06 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -2254,6 +2254,7 @@ TVG_API Tvg_Animation* tvg_animation_new(); * * \note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value * is less than 0.001. In such cases, it returns @c Result::InsufficientCondition. +* Values less than 0.001 may be disregarded and may not be accurately retained by the Animation. * \see tvg_animation_get_total_frame() * * \since 0.13 diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index 01057f42..5bd8fca1 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -313,17 +313,19 @@ bool LottieLoader::override(const char* slot) bool LottieLoader::frame(float no) { + auto frameNo = no + startFrame(); + + //This ensures that the target frame number is reached. + frameNo *= 10000.0f; + frameNo = roundf(frameNo); + frameNo *= 0.0001f; + //Skip update if frame diff is too small. - if (fabsf(this->frameNo - no) < 0.0009f) return false; + if (fabsf(this->frameNo - frameNo) <= 0.0009f) return false; this->done(); - //This ensures that the perfect last frame is reached. - no *= 1000.0f; - no = roundf(no); - no *= 0.001f; - - this->frameNo = no + startFrame(); + this->frameNo = frameNo; TaskScheduler::request(this);