feat(canvas): smart-align guides during line endpoint resize#50
Merged
Conversation
Line endpoint resize previously bypassed Konva's Transformer (it uses custom <Rect> handles in LineObject), so the Stage-level object-snap that fires for normal drag never kicked in — endpoints could only free-form-snap to the grid. Plumb getOthersSnapshot / labelRect / setGuides through KonvaObjectProps so LineObject can run its own snap during endpoint drag: - snapEndpoint(): zero-size rect snap via computeSnap against neighbour edges + label rect, in stage coords. Local↔stage conversion via the parent group's absoluteTransform keeps the snap correct under rotated view (viewRotation 90°/270°). Snap is skipped when Shift is held (axis constraint wins) and when grid-snap is on (mutual exclusion with the existing drag-time snap, mirroring the rest of the app). - snapshot is cached at drag-start to avoid re-querying every other node's clientRect per frame; cleared on drag-end. - selectionHandlers helper applied to BarcodeObject's remaining unfixed Group node (caught while threading new KonvaObjectProps imports). Open trade-off: snap picks the nearest neighbour edge by raw distance. For aligning a line endpoint to the *matching* edge of another line (bottom-to-bottom etc.) the user may need to drag past the near edge. A side-aware heuristic (which edge to prefer when dragging in a given direction) is a follow-up — orthogonal to this branch's scope.
There was a problem hiding this comment.
Code Review
This pull request implements object-snapping for line endpoints, allowing lines to snap to the edges of other shapes and labels during resizing. It introduces new hooks in the KonvaObjectProps interface and a snap-guide pipeline in LineObject.tsx that handles coordinate conversions for rotated views. Feedback was provided regarding a performance optimization in LabelCanvas.tsx, where defining getOthersSnapshot inside a loop causes unnecessary function re-creations on every render.
Reviewer-spotted (gemini-code-assist on PR #50): defining the snapshot closure inside objects.map allocated a fresh function for every object on every LabelCanvas render — and LabelCanvas re-renders on every setGuides call during a drag. Lift to a single useCallback-stable function that takes excludeId. The KonvaObjectProps signature changes from () => SnapRect[] to (excludeId) => SnapRect[]; LineObject's lazy snapshot call now passes obj.id.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.