Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(rendering): add edge snapping and continuous zoom-out animation

Richie McIlroy0f964dd639b0
diff --git a/crates/rendering/src/zoom.rs b/crates/rendering/src/zoom.rs
index 73582bdcd..575c0d8ea 100644
--- a/crates/rendering/src/zoom.rs
+++ b/crates/rendering/src/zoom.rs
@@ -11,8 +11,8 @@ const SCREEN_SPRING_MASS: f64 = 2.25;
#[derive(Debug, Clone, Copy)]
pub struct SegmentsCursor<'a> {
time: f64,
- segment: Option<&'a ZoomSegment>,
- prev_segment: Option<&'a ZoomSegment>,
+ pub segment: Option<&'a ZoomSegment>,
+ pub prev_segment: Option<&'a ZoomSegment>,
segments: &'a [ZoomSegment],
}
@@ -106,19 +106,35 @@ impl SegmentBounds {
)
}
+ fn snap_to_edges(scalar: f64, snap_ratio: f64) -> f64 {
+ if snap_ratio <= 0.0 {
+ return scalar;
+ }
+ let lo = snap_ratio;
+ let hi = 1.0 - snap_ratio + 0.0001;
+ if hi <= lo {
+ return 0.5;
+ }
+ ((scalar - lo) / (hi - lo)).clamp(0.0, 1.0)
+ }
+
fn calculate_follow_center(
cursor_pos: (f64, f64),
zoom_amount: f64,
- _edge_snap_ratio: f64,
+ edge_snap_ratio: f64,
) -> (f64, f64) {
- let viewport_half = 0.5 / zoom_amount;
+ let snapped = (
+ Self::snap_to_edges(cursor_pos.0, edge_snap_ratio),
+ Self::snap_to_edges(cursor_pos.1, edge_snap_ratio),
+ );
+ let viewport_half = 0.5 / zoom_amount;
let min_center = viewport_half;
let max_center = 1.0 - viewport_half;
(
- cursor_pos.0.clamp(min_center, max_center),
- cursor_pos.1.clamp(min_center, max_center),
+ (min_center + snapped.0 * (max_center - min_center)).clamp(min_center, max_center),
+ (min_center + snapped.1 * (max_center - min_center)).clamp(min_center, max_center),
)
}
@@ -126,15 +142,22 @@ impl SegmentBounds {
focus_pos: (f64, f64),
cursor_pos: (f64, f64),
target_zoom: f64,
- _edge_snap_ratio: f64,
+ edge_snap_ratio: f64,
) -> (f64, (f64, f64)) {
+ let snapped_focus = (
+ Self::snap_to_edges(focus_pos.0, edge_snap_ratio),
+ Self::snap_to_edges(focus_pos.1, edge_snap_ratio),
+ );
+
let viewport_half = 0.5 / target_zoom;
let min_center = viewport_half;
let max_center = 1.0 - viewport_half;
let mut center = (
- focus_pos.0.clamp(min_center, max_center),
- focus_pos.1.clamp(min_center, max_center),
+ (min_center + snapped_focus.0 * (max_center - min_center))
+ .clamp(min_center, max_center),
+ (min_center + snapped_focus.1 * (max_center - min_center))
+ .clamp(min_center, max_center),
);
let viewport_left = center.0 - viewport_half;
@@ -278,6 +301,16 @@ impl InterpolatedZoom {
cursor: SegmentsCursor,
zoom_focus: Coord<RawDisplayUVSpace>,
actual_cursor: Option<Coord<RawDisplayUVSpace>>,
+ ) -> Self {
+ Self::new_with_cursor_and_end_focus(cursor, zoom_focus, actual_cursor, None, None)
+ }
+
+ pub fn new_with_cursor_and_end_focus(
+ cursor: SegmentsCursor,
+ zoom_focus: Coord<RawDisplayUVSpace>,
+ actual_cursor: Option<Coord<RawDisplayUVSpace>>,
+ segment_end_focus: Option<Coord<RawDisplayUVSpace>>,
+ segment_end_cursor: Option<Coord<RawDisplayUVSpace>>,
) -> Self {
let use_instant = cursor.segment.map(|s| s.instant_animation).unwrap_or(false);
if use_instant {
@@ -285,6 +318,8 @@ impl InterpolatedZoom {
cursor,
zoom_focus,
actual_cursor,
+ segment_end_focus,
+ segment_end_cursor,
instant_ease,
instant_ease,
)
@@ -293,6 +328,8 @@ impl InterpolatedZoom {
cursor,
zoom_focus,
actual_cursor,
+ segment_end_focus,
+ segment_end_cursor,
spring_ease,
spring_ease_out,
)
@@ -307,6 +344,8 @@ impl InterpolatedZoom {
cursor: SegmentsCursor,
zoom_focus: Coord<RawDisplayUVSpace>,
actual_cursor: Option<Coord<RawDisplayUVSpace>>,
+ segment_end_focus: Option<Coord<RawDisplayUVSpace>>,
+ segment_end_cursor: Option<Coord<RawDisplayUVSpace>>,
ease_in: impl Fn(f32) -> f32 + Copy,
ease_out: impl Fn(f32) -> f32 + Copy,
) -> InterpolatedZoom {
@@ -319,27 +358,54 @@ impl InterpolatedZoom {
let result = match (cursor.prev_segment, cursor.segment) {
(Some(prev_segment), None) => {
- let zoom_t =
- ease_out(t_clamp((cursor.time - prev_segment.end) / ZOOM_DURATION) as f32)
- as f64;
-
- Self {
- t: 1.0 - zoom_t,
- bounds: {
- let prev_segment_bounds =
- SegmentBounds::from_segment_with_cursor_constraint(
- prev_segment,
- zoom_focus,
- actual_cursor,
- );
+ let raw_t = t_clamp((cursor.time - prev_segment.end) / ZOOM_DURATION);
+ let ease_val = ease_out(raw_t as f32) as f64;
+
+ let ramp = (raw_t * 5.0).min(1.0);
+ let ramp_smooth = ramp * ramp * (3.0 - 2.0 * ramp);
+ let zoom_t = ease_val * ramp_smooth;
+
+ let eased_amount = prev_segment.amount * (1.0 - zoom_t) + 1.0 * zoom_t;
+
+ if eased_amount > 1.001 {
+ let eased_segment = ZoomSegment {
+ amount: eased_amount,
+ ..prev_segment.clone()
+ };
+ let is_auto_zoom_out = matches!(prev_segment.mode, cap_project::ZoomMode::Auto);
+ let focus_for_bounds = if is_auto_zoom_out {
+ segment_end_focus.unwrap_or(zoom_focus)
+ } else {
+ zoom_focus
+ };
+ let cursor_for_bounds = if is_auto_zoom_out {
+ segment_end_cursor.or(actual_cursor)
+ } else {
+ actual_cursor
+ };
+
+ let bounds = SegmentBounds::from_segment_with_cursor_constraint(
+ &eased_segment,
+ focus_for_bounds,
+ cursor_for_bounds,
+ );
+ let bounds = if let Some(cursor_coord) = cursor_for_bounds {
+ Self { t: 1.0, bounds }
+ .ensure_cursor_visible((cursor_coord.x, cursor_coord.y))
+ .bounds
+ } else {
+ bounds
+ };
- SegmentBounds::new(
- prev_segment_bounds.top_left * (1.0 - zoom_t)
- + default.top_left * zoom_t,
- prev_segment_bounds.bottom_right * (1.0 - zoom_t)
- + default.bottom_right * zoom_t,
- )
- },
+ Self {
+ t: 1.0 - zoom_t,
+ bounds,
+ }
+ } else {
+ Self {
+ t: 0.0,
+ bounds: default,
+ }
}
}
(None, Some(segment)) => {
@@ -392,6 +458,8 @@ impl InterpolatedZoom {
SegmentsCursor::new(segment.start, cursor.segments),
zoom_focus,
actual_cursor,
+ segment_end_focus,
+ segment_end_cursor,
ease_in,
ease_out,
);
@@ -425,7 +493,11 @@ impl InterpolatedZoom {
},
};
- if is_auto_mode && let Some(cursor_coord) = actual_cursor {
+ let is_zooming_out = cursor.prev_segment.is_some() && cursor.segment.is_none();
+ if is_auto_mode
+ && !is_zooming_out
+ && let Some(cursor_coord) = actual_cursor
+ {
return result.ensure_cursor_visible((cursor_coord.x, cursor_coord.y));
}
@@ -589,6 +661,8 @@ mod test {
c(time, segments),
Default::default(),
None,
+ None,
+ None,
|t| t,
|t| t,
);
@@ -920,4 +994,79 @@ mod test {
"Cursor should be visible in interpolated zoom state"
);
}
+
+ #[test]
+ fn zoom_out_boundary_is_continuous() {
+ let segments = vec![test_segment(2.0, 4.0, 2.0, 0.5, 0.5)];
+
+ let at_boundary = InterpolatedZoom::new_with_easing_and_cursor(
+ c(4.0, &segments),
+ Default::default(),
+ None,
+ None,
+ None,
+ |t| t,
+ |t| t,
+ );
+
+ let epsilon = 1e-9;
+ let just_after = InterpolatedZoom::new_with_easing_and_cursor(
+ c(4.0 + epsilon, &segments),
+ Default::default(),
+ None,
+ None,
+ None,
+ |t| t,
+ |t| t,
+ );
+
+ let dx_tl = (just_after.bounds.top_left.x - at_boundary.bounds.top_left.x).abs();
+ let dy_tl = (just_after.bounds.top_left.y - at_boundary.bounds.top_left.y).abs();
+ let dx_br = (just_after.bounds.bottom_right.x - at_boundary.bounds.bottom_right.x).abs();
+ let dy_br = (just_after.bounds.bottom_right.y - at_boundary.bounds.bottom_right.y).abs();
+
+ let max_jump = dx_tl.max(dy_tl).max(dx_br).max(dy_br);
+ assert!(
+ max_jump < 1e-4,
+ "Bounds jumped {max_jump} at segment boundary; expected near-zero"
+ );
+ }
+
+ #[test]
+ fn zoom_out_early_frames_are_smooth() {
+ let segments = vec![test_segment(2.0, 4.0, 2.0, 0.5, 0.5)];
+
+ let dt = ZOOM_DURATION * 0.01;
+ let mut prev = InterpolatedZoom::new_with_easing_and_cursor(
+ c(4.0, &segments),
+ Default::default(),
+ None,
+ None,
+ None,
+ |t| t,
+ |t| t,
+ );
+
+ for i in 1..=20 {
+ let time = 4.0 + dt * i as f64;
+ let current = InterpolatedZoom::new_with_easing_and_cursor(
+ c(time, &segments),
+ Default::default(),
+ None,
+ None,
+ None,
+ |t| t,
+ |t| t,
+ );
+
+ let dx = (current.bounds.top_left.x - prev.bounds.top_left.x).abs();
+ let dy = (current.bounds.top_left.y - prev.bounds.top_left.y).abs();
+ assert!(
+ dx < 0.02 && dy < 0.02,
+ "Frame jump at step {i} (t={time:.4}): dx={dx:.6} dy={dy:.6}"
+ );
+
+ prev = current;
+ }
+ }
}