Skip to content

Commit

Permalink
feat: 🎸 add a way to check if the animation is completed (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf authored Jan 25, 2024
1 parent fb15425 commit cea2527
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ interface DotLottiePlayer {
boolean render();
boolean resize(u32 width, u32 height);
void clear();
boolean is_complete();
};
11 changes: 11 additions & 0 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {}
Expand Down
9 changes: 8 additions & 1 deletion web-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@

function animationLoop() {
const nextFrameNumber = dotLottiePlayer.requestFrame();
// console.log(nextFrameNumber);

const updated = dotLottiePlayer.setFrame(nextFrameNumber);

Expand All @@ -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);
Expand Down

0 comments on commit cea2527

Please sign in to comment.