From fb0f27c95f63c77f16af70960fd3184154952bb9 Mon Sep 17 00:00:00 2001 From: Abdelrahman Ashraf Date: Thu, 25 Jan 2024 16:43:36 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20a=20way=20to=20che?= =?UTF-8?q?ck=20if=20the=20animation=20is=20completed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dotlottie-ffi/emscripten_bindings.cpp | 3 ++- dotlottie-ffi/src/dotlottie_player.udl | 1 + dotlottie-rs/src/dotlottie_player.rs | 11 +++++++++++ web-example.html | 9 ++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/dotlottie-ffi/emscripten_bindings.cpp b/dotlottie-ffi/emscripten_bindings.cpp index 8214d9c6..b684b905 100644 --- a/dotlottie-ffi/emscripten_bindings.cpp +++ b/dotlottie-ffi/emscripten_bindings.cpp @@ -97,5 +97,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer) .function("setConfig", &DotLottiePlayer::set_config) .function("setFrame", &DotLottiePlayer::set_frame) .function("stop", &DotLottiePlayer::stop) - .function("totalFrames", &DotLottiePlayer::total_frames); + .function("totalFrames", &DotLottiePlayer::total_frames) + .function("isComplete", &DotLottiePlayer::is_complete); } \ No newline at end of file diff --git a/dotlottie-ffi/src/dotlottie_player.udl b/dotlottie-ffi/src/dotlottie_player.udl index cb533b52..ca8f167f 100644 --- a/dotlottie-ffi/src/dotlottie_player.udl +++ b/dotlottie-ffi/src/dotlottie_player.udl @@ -82,4 +82,5 @@ interface DotLottiePlayer { boolean render(); boolean resize(u32 width, u32 height); void clear(); + boolean is_complete(); }; \ No newline at end of file diff --git a/dotlottie-rs/src/dotlottie_player.rs b/dotlottie-rs/src/dotlottie_player.rs index a2b87e4a..7f6adbdd 100644 --- a/dotlottie-rs/src/dotlottie_player.rs +++ b/dotlottie-rs/src/dotlottie_player.rs @@ -505,6 +505,13 @@ impl DotLottieRuntime { pub fn config(&self) -> Config { self.config.clone() } + + pub fn is_complete(&self) -> bool { + match self.config.mode { + Mode::Forward | Mode::ReverseBounce => self.current_frame() >= self.end_frame(), + Mode::Reverse | Mode::Bounce => self.current_frame() <= self.start_frame(), + } + } } pub struct DotLottiePlayer { @@ -647,6 +654,10 @@ impl DotLottiePlayer { None => "{}".to_string(), } } + + pub fn is_complete(&self) -> bool { + self.runtime.read().unwrap().is_complete() + } } unsafe impl Send for DotLottiePlayer {} diff --git a/web-example.html b/web-example.html index 6259dd08..0dcf0503 100644 --- a/web-example.html +++ b/web-example.html @@ -102,7 +102,6 @@ function animationLoop() { const nextFrameNumber = dotLottiePlayer.requestFrame(); - // console.log(nextFrameNumber); const updated = dotLottiePlayer.setFrame(nextFrameNumber); @@ -118,6 +117,14 @@ imageData.data.set(frameBuffer); ctx.putImageData(imageData, 0, 0); } + + if (dotLottiePlayer.isComplete()) { + if (dotLottiePlayer.config().loopAnimation) { + console.log("Loop Completed", dotLottiePlayer.loopCount()); + } else { + console.log("Completed"); + } + } } animationFrameId = requestAnimationFrame(animationLoop);