fix(zoom): drive pinch absolutely from gesture-start snapshot; snappier pen
All checks were successful
CI / Windows build (push) Successful in 12m36s

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) <noreply@anthropic.com>
This commit is contained in:
2026-06-23 01:45:54 +08:00
parent f9ec04fe86
commit 682907614d
4 changed files with 69 additions and 33 deletions

3
.gitignore vendored
View File

@@ -60,3 +60,6 @@ server/.env
# M1 spike generated bench assets (regenerate via tool/gen_*.dart) # M1 spike generated bench assets (regenerate via tool/gen_*.dart)
/test/assets/large_300p.pdf /test/assets/large_300p.pdf
/test/assets/dense_strokes.json /test/assets/dense_strokes.json
# On-device input diagnostic capture (local only)
badnote_input_log.txt

View File

@@ -8,7 +8,8 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:perfect_freehand/perfect_freehand.dart' as pf; 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'; import 'pen_stroke.dart';
/// Builds a filled outline [Path] for one stroke (already scaled to pixels). /// 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 // Highlighter keeps a constant width (no thinning); pen uses the
// configurable [thinning] so real Surface-Pen pressure changes width. // configurable [thinning] so real Surface-Pen pressure changes width.
thinning: isHighlighter ? 0.0 : thinning, thinning: isHighlighter ? 0.0 : thinning,
smoothing: 0.5, smoothing: kPenSmoothing,
streamline: 0.5, streamline: kPenStreamline,
// Real stylus pressure → don't simulate; no pressure → let freehand fake // Real stylus pressure → don't simulate; no pressure → let freehand fake
// it based on velocity. (perfect_freehand 2.x honors REAL pressure when // it based on velocity. (perfect_freehand 2.x honors REAL pressure when
// simulatePressure is false — 1.0.4 ignored it, which made width // simulatePressure is false — 1.0.4 ignored it, which made width

View File

@@ -105,6 +105,17 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
/// applying a frame whose scale/focal still refer to the old finger set. /// applying a frame whose scale/focal still refer to the old finger set.
int _lastPointerCount = 0; 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.31.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) ----------- // --- Matrix helpers (infinite boundary → no clamping to bounds) -----------
Matrix4 _matrixTranslate(Matrix4 matrix, Offset translation) { Matrix4 _matrixTranslate(Matrix4 matrix, Offset translation) {
@@ -156,6 +167,8 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
_lastPointerCount = details.pointerCount; _lastPointerCount = details.pointerCount;
_scaleStart = _transformer.value.getMaxScaleOnAxis(); _scaleStart = _transformer.value.getMaxScaleOnAxis();
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint); _referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
_lastRawScale = 1.0;
_lastAppliedScale = _scaleStart!;
} }
void _onScaleUpdate(ScaleUpdateDetails details) { void _onScaleUpdate(ScaleUpdateDetails details) {
@@ -169,6 +182,8 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
_lastPointerCount = details.pointerCount; _lastPointerCount = details.pointerCount;
_scaleStart = _transformer.value.getMaxScaleOnAxis(); _scaleStart = _transformer.value.getMaxScaleOnAxis();
_referenceFocalPoint = _transformer.toScene(details.localFocalPoint); _referenceFocalPoint = _transformer.toScene(details.localFocalPoint);
_lastRawScale = details.scale;
_lastAppliedScale = _scaleStart!;
InputDiagnostics.instance.recordRebaseline(); InputDiagnostics.instance.recordRebaseline();
return; return;
} }
@@ -204,35 +219,48 @@ class _PenInteractiveViewerState extends State<PenInteractiveViewer>
switch (_gestureType!) { switch (_gestureType!) {
case _GestureType.scale: case _GestureType.scale:
assert(_scaleStart != null); assert(_scaleStart != null);
final double desiredScale = _scaleStart! * details.scale; // Per-frame finger-motion ratio from the recognizer's OWN cumulative
final double scaleChange = desiredScale / scale; // scale — the clean, monotonic signal (verified against device logs).
// Drop a frame demanding an implausible per-frame scale jump (Windows // Crucially we do NOT divide by the live matrix scale here: feeding
// multi-touch glitch) OR an implausible focal jump. Absolute tracking // getMaxScaleOnAxis() back in is what let a single mis-read pop the zoom
// means the next good frame resumes from the true finger positions, so // and snap back. A ratio outside the glitch band is a real multi-touch
// the spike never shows. // 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 = final bool scaleDrop =
scaleChange > _kScaleGlitchHi || scaleChange < _kScaleGlitchLo; rawRatio > _kScaleGlitchHi || rawRatio < _kScaleGlitchLo;
if (scaleDrop || focalDrop) { if (scaleDrop || focalDrop) {
record(1.0, scaleDrop, focalDrop); record(1.0, scaleDrop, focalDrop);
return; return;
} }
_transformer.value = _matrixScale(_transformer.value, scaleChange);
// Keep the focal point anchored under the fingers across the scale. // Drive the transform ABSOLUTELY from the gesture-start snapshot: the
final Offset focalPointSceneScaled = // target scale is `_scaleStart * details.scale`, and we re-anchor so the
_transformer.toScene(details.localFocalPoint); // scene point that was under the focal at gesture start stays under the
_transformer.value = _matrixTranslate( // CURRENT focal (which also yields 2-finger pan for free). Closed form
_transformer.value, // for a pure scale+translate matrix — no inversion, no live read-back —
focalPointSceneScaled - _referenceFocalPoint!, // 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 double applied =
final Offset focalPointSceneCheck = _lastAppliedScale > 0 ? targetScale / _lastAppliedScale : 1.0;
_transformer.toScene(details.localFocalPoint); _lastRawScale = details.scale;
if (_round(_referenceFocalPoint!) != _round(focalPointSceneCheck)) { _lastAppliedScale = targetScale;
_referenceFocalPoint = focalPointSceneCheck; record(applied, false, false);
}
record(scaleChange, false, false);
case _GestureType.pan: case _GestureType.pan:
assert(_referenceFocalPoint != null); assert(_referenceFocalPoint != null);
@@ -458,10 +486,3 @@ double _getFinalTime(double velocity, double drag,
{double effectivelyMotionless = 10}) { {double effectivelyMotionless = 10}) {
return math.log(effectivelyMotionless / velocity) / math.log(drag / 100); 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)),
);
}

View File

@@ -21,6 +21,17 @@ import 'stroke_model.dart';
/// Overridable per-stroke via [PenConfig.pressureSensitivity]. /// Overridable per-stroke via [PenConfig.pressureSensitivity].
const double kDefaultPenThinning = 0.85; 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 /// Builds a closed, fillable outline [Path] for one [stroke], scaled into the
/// pixel space of [pageSize] (which maps normalized [0,1] coords to pixels). /// 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 // Highlighter keeps a constant width (no thinning); pen uses the
// configurable [thinning] so Surface-Pen pressure changes width. // configurable [thinning] so Surface-Pen pressure changes width.
thinning: isHighlighter ? 0.0 : thinning, thinning: isHighlighter ? 0.0 : thinning,
smoothing: 0.5, smoothing: kPenSmoothing,
streamline: 0.5, streamline: kPenStreamline,
// Real stylus pressure -> don't simulate; no pressure -> let freehand // Real stylus pressure -> don't simulate; no pressure -> let freehand
// fake it based on velocity (highlighter never simulates). perfect_freehand // fake it based on velocity (highlighter never simulates). perfect_freehand
// 2.x honors real pressure when simulatePressure is false. // 2.x honors real pressure when simulatePressure is false.