From 388f612822437725e445e00a78198f91785418d0 Mon Sep 17 00:00:00 2001 From: TheZupZup <16434778+TheZupZup@users.noreply.github.com> Date: Sat, 11 Apr 2026 18:10:20 -0400 Subject: [PATCH] fix: drawing follows cursor correctly (S Pen / stylus support) _toCanvasPos was using context.findRenderObject() which returns the RenderBox of the entire InkCanvas widget including the toolbar (52px). All drawn points were offset by the toolbar height, so strokes never matched the cursor/stylus position. Fix: use e.localPosition directly from the Listener event, which is already in the canvas area coordinate space (below the toolbar). Also: - Add PointerDeviceKind.invertedStylus (Samsung S Pen eraser end) - Guard onPointerMove to skip if no stroke is in progress https://claude.ai/code/session_01DrMeeXsyuHucgvtcwtJoLN --- app/lib/widgets/ink_canvas.dart | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/lib/widgets/ink_canvas.dart b/app/lib/widgets/ink_canvas.dart index afafd83..51797ff 100644 --- a/app/lib/widgets/ink_canvas.dart +++ b/app/lib/widgets/ink_canvas.dart @@ -356,13 +356,10 @@ class _InkCanvasState extends State { widget.onStrokesChanged([]); } - Offset _toCanvasPos(Offset globalPos) { - final box = context.findRenderObject() as RenderBox?; - if (box == null) return globalPos; - final local = box.globalToLocal(globalPos); + Offset _toCanvasPos(Offset localPos) { return Offset( - (local.dx - _offset.dx) / _scale, - (local.dy - _offset.dy) / _scale, + (localPos.dx - _offset.dx) / _scale, + (localPos.dy - _offset.dy) / _scale, ); } @@ -393,15 +390,17 @@ class _InkCanvasState extends State { child: Listener( onPointerDown: (e) { if (e.kind == PointerDeviceKind.stylus || + e.kind == PointerDeviceKind.invertedStylus || e.kind == PointerDeviceKind.mouse || e.kind == PointerDeviceKind.touch) { final pressure = e.pressure > 0 ? e.pressure : 0.5; - _startStroke(_toCanvasPos(e.position), pressure); + _startStroke(_toCanvasPos(e.localPosition), pressure); } }, onPointerMove: (e) { + if (_currentStroke == null) return; final pressure = e.pressure > 0 ? e.pressure : 0.5; - _addPoint(_toCanvasPos(e.position), pressure); + _addPoint(_toCanvasPos(e.localPosition), pressure); }, onPointerUp: (_) => _endStroke(), onPointerCancel: (_) => _endStroke(),