Skip to content

Commit

Permalink
feat(playback): 🎸 segment duration getter (#161)
Browse files Browse the repository at this point in the history
* feat(playback): 🎸 segment duration getter

* chore: 🤖 update changelog
  • Loading branch information
theashraf authored May 29, 2024
1 parent 4a946a9 commit a26564e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/featplayback_segment_duration_getter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: minor
---

# feat(playback): 🎸 segment duration getter
3 changes: 2 additions & 1 deletion dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.function("loadThemeData", &DotLottiePlayer::load_theme_data)
.function("markers", &DotLottiePlayer::markers)
.function("activeAnimationId", &DotLottiePlayer::active_animation_id)
.function("activeThemeId", &DotLottiePlayer::active_theme_id);
.function("activeThemeId", &DotLottiePlayer::active_theme_id)
.function("segmentDuration", &DotLottiePlayer::segment_duration);
}
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@ interface DotLottiePlayer {
sequence<Marker> markers();
string active_animation_id();
string active_theme_id();
f32 segment_duration();
};
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@ interface DotLottiePlayer {
sequence<Marker> markers();
string active_animation_id();
string active_theme_id();
f32 segment_duration();
};
17 changes: 17 additions & 0 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,19 @@ impl DotLottieRuntime {
self.renderer.duration().unwrap_or(0.0)
}

pub fn segment_duration(&self) -> f32 {
if self.config.segment.is_empty() {
self.duration()
} else {
let start_frame = self.start_frame();
let end_frame = self.end_frame();

let frame_rate = self.total_frames() / self.duration();

(end_frame - start_frame) / frame_rate
}
}

pub fn current_frame(&self) -> f32 {
self.renderer.current_frame
}
Expand Down Expand Up @@ -955,6 +968,10 @@ impl DotLottiePlayer {
self.runtime.read().unwrap().duration()
}

pub fn segment_duration(&self) -> f32 {
self.runtime.read().unwrap().segment_duration()
}

pub fn current_frame(&self) -> f32 {
self.runtime.read().unwrap().current_frame()
}
Expand Down
8 changes: 1 addition & 7 deletions dotlottie-rs/tests/speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ mod tests {
);
assert!(player.is_playing(), "Animation should be playing");

let expected_duration = if player.config().segment.is_empty() {
player.duration()
} else {
let segment_total_frames = player.config().segment[1] - player.config().segment[0];

segment_total_frames / player.total_frames() * player.duration()
};
let expected_duration = player.segment_duration();

let start_time = std::time::Instant::now();

Expand Down

0 comments on commit a26564e

Please sign in to comment.