Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 add a method to check if the animation is completed #58

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading