Avoid too close points being added to a drawing_line
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user