GraphiteEditor / GraphiteEditor/Graphite

Broken resizing with rectangle tool when scale ≈0

オープン
#4,310 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Rust
スター
27.2k
フォーク
1.3k
平均マージ
20時間 5分
マージ済み PR(30日)
57

説明

## 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()
```

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

editor/src/messages/tool/common_functionality/shapes/shape_utility.rs、transformation_cage.rs、tool_messages/select_tool.rs から始め、issue にある snapping と resizing のシーケンスを再現します。transform-cage の scale と flat/point bounds のチェックを追跡します。幅または高さがほぼ 0 の場合でも、矩形やその他の shape tool が jitter なしでリサイズできれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
rust
領域
computer-graphics
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
明確に書かれている
初心者へのやさしさ
72/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。