Avoid too close points being added to a drawing_line

This commit is contained in:
2026-02-05 18:23:28 +01:00
parent f615ed5654
commit 3b12be497e
2 changed files with 19 additions and 11 deletions

View File

@@ -64,9 +64,11 @@ class DrawingController extends ChangeNotifier {
void addPoint(Offset normalizedPoint) {
if (_currentLine == null) return;
if (!currentLine!.isPointTooClose(normalizedPoint)) {
_currentLine = _currentLine!.addPoint(normalizedPoint);
notifyListeners();
}
}
/// Completes the current line and adds it to the history.
void endLine() {

View File

@@ -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<Offset> points;
@@ -27,10 +30,9 @@ class DrawingLine {
/// Creates a DrawingLine from JSON data.
factory DrawingLine.fromJson(Map<String, dynamic> 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<Offset>? 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);
}