mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Brush smoothing: a timed stroke stays timed on a zero-time report (#12246)
Once time had passed between two input points, a later report with the same timestamp still took the untimed per-point pull (90 % of the way at 10 % smoothing), so the brush jumped. A timed stroke now remembers it is timed, and a report with no time since the last one leaves the brush where it is. Untimed input keeps its per-point pull. CodeRabbit finding on #12246. Co-Authored-By: Hector Villarreal <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
e9a204f965
commit
90816ab768
2 files changed
+24
-2
No files matched your search
@@ -117,12 +117,15 @@ pub struct Smoother {
|
||||
zoom: f64,
|
||||
pos: Option<StrokePoint>,
|
||||
last_input: Option<StrokePoint>,
|
||||
/// Has time passed between two input points? Then the stroke is timed, and a later point
|
||||
/// with no time since the last one moves the brush no further (not by the untimed pull).
|
||||
timed: bool,
|
||||
}
|
||||
|
||||
impl Smoother {
|
||||
/// `zoom` is the view zoom (only used with `adjust_for_zoom`).
|
||||
pub fn new(cfg: &Smoothing, zoom: f32) -> Self {
|
||||
Self { cfg: cfg.clone(), zoom: f64::from(zoom.max(0.01)), pos: None, last_input: None }
|
||||
Self { cfg: cfg.clone(), zoom: f64::from(zoom.max(0.01)), pos: None, last_input: None, timed: false }
|
||||
}
|
||||
|
||||
fn active(&self) -> bool {
|
||||
@@ -171,7 +174,13 @@ impl Smoother {
|
||||
// Without Stroke Catch-Up the brush stops while the pointer pauses (Photoshop).
|
||||
return;
|
||||
}
|
||||
let (iters, step) = if dt > 0.0 && dt.is_finite() {
|
||||
let has_time = dt > 0.0 && dt.is_finite();
|
||||
self.timed |= has_time;
|
||||
if self.timed && !has_time {
|
||||
// No time passed (two reports in one millisecond): the brush stays where it is.
|
||||
return;
|
||||
}
|
||||
let (iters, step) = if has_time {
|
||||
let steps = dt / SMOOTHING_STEP_MS;
|
||||
let iters = steps.ceil().clamp(1.0, 64.0);
|
||||
// Each of `iters` moves covers `steps / iters` steps' worth of pull.
|
||||
|
||||
@@ -72,3 +72,16 @@ fn strokes_without_timestamps_keep_their_per_point_pull() {
|
||||
let x = out.last().map_or(f64::NAN, |p| p.x);
|
||||
assert!((x - 90.0).abs() < 1e-4, "a 10 % untimed step reached {x}, want 90");
|
||||
}
|
||||
|
||||
/// A timed stroke stays timed: a report with no time since the last one (two events in one
|
||||
/// millisecond) leaves the brush where it is, instead of jumping it by the untimed per-point pull.
|
||||
#[test]
|
||||
fn a_report_with_no_time_passed_does_not_move_a_timed_brush() {
|
||||
let cfg = Smoothing { amount: 0.1, catch_up: true, catch_up_on_end: false, ..Default::default() };
|
||||
let at = |x: f64, time: f64| StrokePoint { time, ..StrokePoint::new(x, 0.0, 1.0) };
|
||||
let timed = smooth_points(&cfg, 1.0, &[at(0.0, 0.0), at(100.0, 10.0)]);
|
||||
let twice = smooth_points(&cfg, 1.0, &[at(0.0, 0.0), at(100.0, 10.0), at(100.0, 10.0)]);
|
||||
let (before, after) = (timed.last().map_or(f64::NAN, |p| p.x), twice.last().map_or(f64::NAN, |p| p.x));
|
||||
assert!(before > 0.0 && before < 90.0, "the timed move lags: {before}");
|
||||
assert_eq!(after, before, "no time passed, so the brush stays");
|
||||
}
|
||||
Reference in new issue
Block a user