From 682907614d39fb66dbe04d190841e5ea5360718f Mon Sep 17 00:00:00 2001 From: Akiba So Date: Tue, 23 Jun 2026 01:45:54 +0800 Subject: [PATCH] fix(zoom): drive pinch absolutely from gesture-start snapshot; snappier pen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zoom flicker root cause (from the on-device badnote_input_log.txt): the pinch computed its per-frame scale change as desiredScale / getMaxScaleOnAxis(), i.e. it fed the LIVE matrix back into its own update. Consecutive frames in the log show `cur` (the live read) dropping to 0.75-0.89 for a single frame while the result track stayed smooth, so the code demanded a 1.3-1.4x correction that popped the zoom bigger/smaller and snapped back. The >1.4 glitch guard missed it because the spikes sat at 1.31-1.40. Fix: the scale branch of PenInteractiveViewer now drives the transform ABSOLUTELY from a gesture-start snapshot (_scaleStart, _referenceFocalPoint) plus the recognizer's clean, monotonic cumulative details.scale. Each frame is fully re-derived in closed form (pure scale+translate, no matrix inversion, no live read-back), so a transient mis-read or interleaved write cannot survive into the next frame. The per-frame glitch guard now keys on the recognizer's own scale-ratio (the true finger motion) instead of the corrupted live read. 2-finger pan still falls out of the same focal-anchor formula. Pen feel: lower perfect_freehand streamline 0.5 -> 0.32 (new shared constants kPenStreamline/kPenSmoothing, single-sourced across screen + export so the parity test still holds). At 0.5 a quick flick lagged so far behind the pen that short fast strokes collapsed toward their start and rendered as a dot ("写字识别成单击"); 0.32 tracks the real path for a crisper, lower-latency feel. flutter analyze lib/editor clean; 66/66 tests pass (incl. screen==export parity). Co-Authored-By: Claude Opus 4.8 (1M context) --- .gitignore | 3 + lib/editor/canvas/ink_painters.dart | 7 +- lib/editor/canvas/pen_interactive_viewer.dart | 77 ++++++++++++------- lib/editor/engine/stroke_geometry.dart | 15 +++- 4 files changed, 69 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 055cf9d..7119ed0 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,6 @@ server/.env # M1 spike generated bench assets (regenerate via tool/gen_*.dart) /test/assets/large_300p.pdf /test/assets/dense_strokes.json + +# On-device input diagnostic capture (local only) +badnote_input_log.txt diff --git a/lib/editor/canvas/ink_painters.dart b/lib/editor/canvas/ink_painters.dart index fc6e54a..e6511f7 100644 --- a/lib/editor/canvas/ink_painters.dart +++ b/lib/editor/canvas/ink_painters.dart @@ -8,7 +8,8 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:perfect_freehand/perfect_freehand.dart' as pf; -import '../engine/stroke_geometry.dart' show kDefaultPenThinning; +import '../engine/stroke_geometry.dart' + show kDefaultPenThinning, kPenSmoothing, kPenStreamline; import 'pen_stroke.dart'; /// Builds a filled outline [Path] for one stroke (already scaled to pixels). @@ -45,8 +46,8 @@ Path buildStrokePath( // Highlighter keeps a constant width (no thinning); pen uses the // configurable [thinning] so real Surface-Pen pressure changes width. thinning: isHighlighter ? 0.0 : thinning, - smoothing: 0.5, - streamline: 0.5, + smoothing: kPenSmoothing, + streamline: kPenStreamline, // Real stylus pressure → don't simulate; no pressure → let freehand fake // it based on velocity. (perfect_freehand 2.x honors REAL pressure when // simulatePressure is false — 1.0.4 ignored it, which made width diff --git a/lib/editor/canvas/pen_interactive_viewer.dart b/lib/editor/canvas/pen_interactive_viewer.dart index 6e6d416..bf73d48 100644 --- a/lib/editor/canvas/pen_interactive_viewer.dart +++ b/lib/editor/canvas/pen_interactive_viewer.dart @@ -105,6 +105,17 @@ class _PenInteractiveViewerState extends State /// applying a frame whose scale/focal still refer to the old finger set. int _lastPointerCount = 0; + /// The recognizer's cumulative `details.scale` and the absolute scale we last + /// APPLIED, both as of the previous accepted frame. The pinch is driven + /// absolutely from these + the gesture-start snapshot — we never read the live + /// matrix back into the per-frame scale change. (Re-reading + /// `getMaxScaleOnAxis()` per frame was the flicker source: a single transient + /// mis-read/interleaved write made `desiredScale/liveScale` demand a ~1.3–1.4x + /// jump for one frame and snap back. The glitch guard missed it because the + /// spike sat just under the 1.4 threshold.) + double _lastRawScale = 1.0; + double _lastAppliedScale = 1.0; + // --- Matrix helpers (infinite boundary → no clamping to bounds) ----------- Matrix4 _matrixTranslate(Matrix4 matrix, Offset translation) { @@ -156,6 +167,8 @@ class _PenInteractiveViewerState extends State _lastPointerCount = details.pointerCount; _scaleStart = _transformer.value.getMaxScaleOnAxis(); _referenceFocalPoint = _transformer.toScene(details.localFocalPoint); + _lastRawScale = 1.0; + _lastAppliedScale = _scaleStart!; } void _onScaleUpdate(ScaleUpdateDetails details) { @@ -169,6 +182,8 @@ class _PenInteractiveViewerState extends State _lastPointerCount = details.pointerCount; _scaleStart = _transformer.value.getMaxScaleOnAxis(); _referenceFocalPoint = _transformer.toScene(details.localFocalPoint); + _lastRawScale = details.scale; + _lastAppliedScale = _scaleStart!; InputDiagnostics.instance.recordRebaseline(); return; } @@ -204,35 +219,48 @@ class _PenInteractiveViewerState extends State switch (_gestureType!) { case _GestureType.scale: assert(_scaleStart != null); - final double desiredScale = _scaleStart! * details.scale; - final double scaleChange = desiredScale / scale; - // Drop a frame demanding an implausible per-frame scale jump (Windows - // multi-touch glitch) OR an implausible focal jump. Absolute tracking - // means the next good frame resumes from the true finger positions, so - // the spike never shows. + // Per-frame finger-motion ratio from the recognizer's OWN cumulative + // scale — the clean, monotonic signal (verified against device logs). + // Crucially we do NOT divide by the live matrix scale here: feeding + // getMaxScaleOnAxis() back in is what let a single mis-read pop the zoom + // and snap back. A ratio outside the glitch band is a real multi-touch + // spike → drop the frame; absolute tracking means the next good frame + // resumes from the true finger span, so the spike never shows. + final double rawRatio = + _lastRawScale > 0 ? details.scale / _lastRawScale : 1.0; final bool scaleDrop = - scaleChange > _kScaleGlitchHi || scaleChange < _kScaleGlitchLo; + rawRatio > _kScaleGlitchHi || rawRatio < _kScaleGlitchLo; if (scaleDrop || focalDrop) { record(1.0, scaleDrop, focalDrop); return; } - _transformer.value = _matrixScale(_transformer.value, scaleChange); - // Keep the focal point anchored under the fingers across the scale. - final Offset focalPointSceneScaled = - _transformer.toScene(details.localFocalPoint); - _transformer.value = _matrixTranslate( - _transformer.value, - focalPointSceneScaled - _referenceFocalPoint!, + // Drive the transform ABSOLUTELY from the gesture-start snapshot: the + // target scale is `_scaleStart * details.scale`, and we re-anchor so the + // scene point that was under the focal at gesture start stays under the + // CURRENT focal (which also yields 2-finger pan for free). Closed form + // for a pure scale+translate matrix — no inversion, no live read-back — + // so an interleaved/transient matrix write can't survive into the next + // frame: every frame is fully re-derived from clean inputs. + final double targetScale = clampDouble( + _scaleStart! * details.scale, + widget.minScale, + widget.maxScale, ); + final Offset focal = details.localFocalPoint; + final double tx = focal.dx - targetScale * _referenceFocalPoint!.dx; + final double ty = focal.dy - targetScale * _referenceFocalPoint!.dy; + _transformer.value = Matrix4.identity() + ..setEntry(0, 0, targetScale) + ..setEntry(1, 1, targetScale) + ..setEntry(2, 2, targetScale) + ..setTranslationRaw(tx, ty, 0); - // Re-anchor only when the rounded focal actually drifted (jitter damp). - final Offset focalPointSceneCheck = - _transformer.toScene(details.localFocalPoint); - if (_round(_referenceFocalPoint!) != _round(focalPointSceneCheck)) { - _referenceFocalPoint = focalPointSceneCheck; - } - record(scaleChange, false, false); + final double applied = + _lastAppliedScale > 0 ? targetScale / _lastAppliedScale : 1.0; + _lastRawScale = details.scale; + _lastAppliedScale = targetScale; + record(applied, false, false); case _GestureType.pan: assert(_referenceFocalPoint != null); @@ -458,10 +486,3 @@ double _getFinalTime(double velocity, double drag, {double effectivelyMotionless = 10}) { return math.log(effectivelyMotionless / velocity) / math.log(drag / 100); } - -Offset _round(Offset offset) { - return Offset( - double.parse(offset.dx.toStringAsFixed(9)), - double.parse(offset.dy.toStringAsFixed(9)), - ); -} diff --git a/lib/editor/engine/stroke_geometry.dart b/lib/editor/engine/stroke_geometry.dart index 13351d2..8835349 100644 --- a/lib/editor/engine/stroke_geometry.dart +++ b/lib/editor/engine/stroke_geometry.dart @@ -21,6 +21,17 @@ import 'stroke_model.dart'; /// Overridable per-stroke via [PenConfig.pressureSensitivity]. const double kDefaultPenThinning = 0.85; +/// perfect_freehand input-smoothing parameters, shared (single source of truth) +/// by the on-screen painter and the export path so the two can never diverge +/// (guarded by the screen==export parity test). [kPenStreamline] lowers the +/// per-point lag from freehand's 0.5 default to 0.32: at 0.5 a quick flick lags +/// so far behind the pen that a short fast stroke collapsed toward its start and +/// rendered as a dot ("写字识别成单击") and the pen felt sluggish; 0.32 tracks the +/// real path closely (crisper, lower-latency feel) while still damping digitizer +/// jitter. [kPenSmoothing] keeps freehand's 0.5 corner rounding. +const double kPenStreamline = 0.32; +const double kPenSmoothing = 0.5; + /// Builds a closed, fillable outline [Path] for one [stroke], scaled into the /// pixel space of [pageSize] (which maps normalized [0,1] coords to pixels). /// @@ -63,8 +74,8 @@ Path buildStrokeOutline( // Highlighter keeps a constant width (no thinning); pen uses the // configurable [thinning] so Surface-Pen pressure changes width. thinning: isHighlighter ? 0.0 : thinning, - smoothing: 0.5, - streamline: 0.5, + smoothing: kPenSmoothing, + streamline: kPenStreamline, // Real stylus pressure -> don't simulate; no pressure -> let freehand // fake it based on velocity (highlighter never simulates). perfect_freehand // 2.x honors real pressure when simulatePressure is false.