diff --git a/lib/features/sheet_viewer/drawing/drawing_controller.dart b/lib/features/sheet_viewer/drawing/drawing_controller.dart index 945ba40..a7e7286 100644 --- a/lib/features/sheet_viewer/drawing/drawing_controller.dart +++ b/lib/features/sheet_viewer/drawing/drawing_controller.dart @@ -64,8 +64,10 @@ class DrawingController extends ChangeNotifier { void addPoint(Offset normalizedPoint) { if (_currentLine == null) return; - _currentLine = _currentLine!.addPoint(normalizedPoint); - notifyListeners(); + if (!currentLine!.isPointTooClose(normalizedPoint)) { + _currentLine = _currentLine!.addPoint(normalizedPoint); + notifyListeners(); + } } /// Completes the current line and adds it to the history. diff --git a/lib/features/sheet_viewer/drawing/drawing_line.dart b/lib/features/sheet_viewer/drawing/drawing_line.dart index 8ebd435..e0cee61 100644 --- a/lib/features/sheet_viewer/drawing/drawing_line.dart +++ b/lib/features/sheet_viewer/drawing/drawing_line.dart @@ -8,6 +8,9 @@ import 'dart:ui'; /// /// This allows drawings to scale correctly when the canvas size changes. class DrawingLine { + /// The minimal squared distance between to points which are normalized so that this point is allowed to be added to the line + static const minNormalizedPointDistanceSquared = 0.001 * 0.001; + /// Points in normalized coordinates (0.0 to 1.0) final List points; @@ -27,10 +30,9 @@ class DrawingLine { /// Creates a DrawingLine from JSON data. factory DrawingLine.fromJson(Map json) { final pointsList = (json['points'] as List) - .map((p) => Offset( - (p['x'] as num).toDouble(), - (p['y'] as num).toDouble(), - )) + .map( + (p) => Offset((p['x'] as num).toDouble(), (p['y'] as num).toDouble()), + ) .toList(); return DrawingLine( @@ -58,6 +60,14 @@ class DrawingLine { ); } + bool isPointTooClose(Offset nextNormalizedPoint) { + if (points.isEmpty) { + return false; + } + return (points.last - nextNormalizedPoint).distanceSquared < + minNormalizedPointDistanceSquared; + } + /// Creates a copy with updated points. DrawingLine copyWith({ List? points, @@ -85,9 +95,5 @@ class DrawingLine { } @override - int get hashCode => Object.hash( - Object.hashAll(points), - color, - strokeWidth, - ); + int get hashCode => Object.hash(Object.hashAll(points), color, strokeWidth); }