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) {
|
void addPoint(Offset normalizedPoint) {
|
||||||
if (_currentLine == null) return;
|
if (_currentLine == null) return;
|
||||||
|
|
||||||
_currentLine = _currentLine!.addPoint(normalizedPoint);
|
if (!currentLine!.isPointTooClose(normalizedPoint)) {
|
||||||
notifyListeners();
|
_currentLine = _currentLine!.addPoint(normalizedPoint);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Completes the current line and adds it to the history.
|
/// 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.
|
/// This allows drawings to scale correctly when the canvas size changes.
|
||||||
class DrawingLine {
|
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)
|
/// Points in normalized coordinates (0.0 to 1.0)
|
||||||
final List<Offset> points;
|
final List<Offset> points;
|
||||||
|
|
||||||
@@ -27,10 +30,9 @@ class DrawingLine {
|
|||||||
/// Creates a DrawingLine from JSON data.
|
/// Creates a DrawingLine from JSON data.
|
||||||
factory DrawingLine.fromJson(Map<String, dynamic> json) {
|
factory DrawingLine.fromJson(Map<String, dynamic> json) {
|
||||||
final pointsList = (json['points'] as List)
|
final pointsList = (json['points'] as List)
|
||||||
.map((p) => Offset(
|
.map(
|
||||||
(p['x'] as num).toDouble(),
|
(p) => Offset((p['x'] as num).toDouble(), (p['y'] as num).toDouble()),
|
||||||
(p['y'] as num).toDouble(),
|
)
|
||||||
))
|
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
return DrawingLine(
|
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.
|
/// Creates a copy with updated points.
|
||||||
DrawingLine copyWith({
|
DrawingLine copyWith({
|
||||||
List<Offset>? points,
|
List<Offset>? points,
|
||||||
@@ -85,9 +95,5 @@ class DrawingLine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
int get hashCode => Object.hash(
|
int get hashCode => Object.hash(Object.hashAll(points), color, strokeWidth);
|
||||||
Object.hashAll(points),
|
|
||||||
color,
|
|
||||||
strokeWidth,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user