GraphiteEditor / GraphiteEditor/Graphite

Broken resizing with rectangle tool when scale ≈0

Offen
#4,310 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
27.2k
Forks
1.3k
Ø Merge
20 Std. 5 Min.
Gemergte PRs (30 T.)
57

Beschreibung

## Reproduction
- New document (with artboard)
- Draw rectangle from the centre of the artboard with good width and height
- Use the handles so the rectangle has approximately no height (using snapping)
- Drag the handles to give height again
- Observe strange jitter

Also occurs with the select tool, ellipse tool, etc.

## Video

https://github.com/user-attachments/assets/af8b36c9-cc0d-4914-805b-cc6aa15fd539

## Fix

The bound transform is set to some extremely small value e.g. 1e-15. This makes the floating point imprecision cause jittering during the transform calculations (which aren't particularly optimised to keep the precision). A fix is simply to scale up the bounding box transform so it has always a scale of 1. Stretch parallel to x or y axis leave the resulting box invariant. Also there was some thing to detect if the box was flattened. However this happened in the bounds space rather than the viewport space. In the diff switched to viewport space as it probably makes more sense?

```diff
diff --git a/editor/src/messages/tool/common_functionality/shapes/shape_utility.rs b/editor/src/messages/tool/common_functionality/shapes/shape_utility.rs
index 5f0d6da01..c66a7b1fc 100644
--- a/editor/src/messages/tool/common_functionality/shapes/shape_utility.rs
+++ b/editor/src/messages/tool/common_functionality/shapes/shape_utility.rs
@@ -266,6 +266,8 @@ pub fn transform_cage_overlays(document: &DocumentMessageHandler, tool_data: &mu
transform.matrix2 += DMat2::IDENTITY * 1e-4; // TODO: Is this the cleanest way to handle this?
transform_tampered = true;
}
+ // Ensure the transform scale is set to one (only view rotation/skew). This gets around floating point imprecision causing jumpiness.
+ transform = transform * DAffine2::from_scale(DVec2::new(transform.x_axis.x, transform.y_axis.y)).inverse();

let bounds = document
.network_interface
diff --git a/editor/src/messages/tool/common_functionality/transformation_cage.rs b/editor/src/messages/tool/common_functionality/transformation_cage.rs
index e6c265e3c..28c32983b 100644
--- a/editor/src/messages/tool/common_functionality/transformation_cage.rs
+++ b/editor/src/messages/tool/common_functionality/transformation_cage.rs
@@ -639,12 +639,14 @@ impl BoundingBoxManager {

/// Determine if these bounds are flat ([`TransformCageSizeCategory::Flat`]), which means that the width and/or height is essentially zero and the bounds are a line with effectively no area. This can happen on actual lines (axis-aligned, i.e. drawn horizontally or vertically) or when an element is scaled to zero in X or Y. A flat transform cage can still be rotated by a transformation, but its local space remains flat.
fn is_bounds_flat(&self) -> bool {
- (self.bounds[0] - self.bounds[1]).abs().cmple(DVec2::splat(MAX_LENGTH_FOR_NO_WIDTH_OR_HEIGHT)).any()
+ let viewport_size = self.transform.transform_vector2(self.bounds[0] - self.bounds[1]);
+ viewport_size.abs().cmple(DVec2::splat(MAX_LENGTH_FOR_NO_WIDTH_OR_HEIGHT)).any()
}

/// Determine if these bounds are point ([`TransformCageSizeCategory::Point`]), which means that the width and height are essentially zero and the bounds are a point with no area. This can happen on points when an element is scaled to zero in both X and Y, or if an element is just a single anchor point. A point transform cage cannot be rotated by a transformation, and its local space remains a point.
fn is_bounds_point(&self) -> bool {
- (self.bounds[0] - self.bounds[1]).abs().cmple(DVec2::splat(MAX_LENGTH_FOR_NO_WIDTH_OR_HEIGHT)).all()
+ let viewport_size = self.transform.transform_vector2(self.bounds[0] - self.bounds[1]);
+ viewport_size.abs().cmple(DVec2::splat(MAX_LENGTH_FOR_NO_WIDTH_OR_HEIGHT)).all()
}

/// Determine if the given point in viewport space falls within the bounds of `self`.
diff --git a/editor/src/messages/tool/tool_messages/select_tool.rs b/editor/src/messages/tool/tool_messages/select_tool.rs
index d559a595b..b81363b83 100644
--- a/editor/src/messages/tool/tool_messages/select_tool.rs
+++ b/editor/src/messages/tool/tool_messages/select_tool.rs
@@ -768,6 +768,9 @@ impl Fsm for SelectToolFsmState {
transform_tampered = true;
}

+ // Ensure the transform scale is set to one (only view rotation/skew). This gets around floating point imprecision causing jumpiness.
+ transform = transform * DAffine2::from_scale(DVec2::new(transform.x_axis.x, transform.y_axis.y)).inverse();
+
let bounds = document
.network_interface
.selected_nodes()
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start in editor/src/messages/tool/common_functionality/shapes/shape_utility.rs, transformation_cage.rs, and tool_messages/select_tool.rs, then reproduce the snapping and resizing sequence from the issue. Trace the transform-cage scale and flat/point bounds checks; done means rectangles and other shape tools resize without jitter when their width or height is approximately zero.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rust
Bereich
computer-graphics
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
72/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.