-
Notifications
You must be signed in to change notification settings - Fork 22
Whiteboard: laser dot/trail fix, object snapping, letter hotkeys, per-user close #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pj4real
wants to merge
3
commits into
ACM-VIT:staging
Choose a base branch
from
pj4real:staging
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
94 changes: 94 additions & 0 deletions
94
packages/apps-sdk/src/apps/whiteboard/core/tools/snapping.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { Bounds } from "../model/geometry"; | ||
|
|
||
| export type SnapEdge = "start" | "center" | "end"; | ||
| export type SnapGuide = { axis: "x" | "y"; position: number; spanStart: number; spanEnd: number }; | ||
| export type SnapAdjustment = { dx: number; dy: number; guides: SnapGuide[] }; | ||
|
|
||
| const ALL_EDGES: SnapEdge[] = ["start", "center", "end"]; | ||
|
|
||
| const edgeValue = (bounds: Bounds, axis: "x" | "y", edge: SnapEdge): number => { | ||
| const start = axis === "x" ? bounds.x : bounds.y; | ||
| const size = axis === "x" ? bounds.width : bounds.height; | ||
| if (edge === "start") return start; | ||
| if (edge === "end") return start + size; | ||
| return start + size / 2; | ||
| }; | ||
|
|
||
| type AxisSnap = { offset: number; guide: SnapGuide }; | ||
|
|
||
| const computeAxisSnap = ( | ||
| movingBounds: Bounds, | ||
| otherBounds: Bounds[], | ||
| axis: "x" | "y", | ||
| edges: SnapEdge[], | ||
| threshold: number | ||
| ): AxisSnap | null => { | ||
| let best: { offset: number; distance: number; position: number; other: Bounds } | null = null; | ||
|
|
||
| for (const edge of edges) { | ||
| const value = edgeValue(movingBounds, axis, edge); | ||
| for (const other of otherBounds) { | ||
| for (const otherEdge of ALL_EDGES) { | ||
| const target = edgeValue(other, axis, otherEdge); | ||
| const distance = Math.abs(value - target); | ||
| if (distance <= threshold && (!best || distance < best.distance)) { | ||
| best = { offset: target - value, distance, position: target, other }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!best) return null; | ||
|
|
||
| const crossAxis = axis === "x" ? "y" : "x"; | ||
| const movingStart = crossAxis === "x" ? movingBounds.x : movingBounds.y; | ||
| const movingSize = crossAxis === "x" ? movingBounds.width : movingBounds.height; | ||
| const otherStart = crossAxis === "x" ? best.other.x : best.other.y; | ||
| const otherSize = crossAxis === "x" ? best.other.width : best.other.height; | ||
|
|
||
| return { | ||
| offset: best.offset, | ||
| guide: { | ||
| axis, | ||
| position: best.position, | ||
| spanStart: Math.min(movingStart, otherStart), | ||
| spanEnd: Math.max(movingStart + movingSize, otherStart + otherSize), | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Compares movingBounds' edges/centers against every bounds in otherBounds on | ||
| * each axis independently, and returns the delta needed to align to the | ||
| * closest match within threshold (0 if nothing is close enough to snap). | ||
| */ | ||
| export function computeSnapAdjustment( | ||
| movingBounds: Bounds, | ||
| otherBounds: Bounds[], | ||
| thresholdCanvasUnits: number, | ||
| options?: { xEdges?: SnapEdge[]; yEdges?: SnapEdge[] } | ||
| ): SnapAdjustment { | ||
| if (thresholdCanvasUnits <= 0 || otherBounds.length === 0) { | ||
| return { dx: 0, dy: 0, guides: [] }; | ||
| } | ||
|
|
||
| const xEdges = options?.xEdges ?? ALL_EDGES; | ||
| const yEdges = options?.yEdges ?? ALL_EDGES; | ||
| const guides: SnapGuide[] = []; | ||
| let dx = 0; | ||
| let dy = 0; | ||
|
|
||
| const xSnap = computeAxisSnap(movingBounds, otherBounds, "x", xEdges, thresholdCanvasUnits); | ||
| if (xSnap) { | ||
| dx = xSnap.offset; | ||
| guides.push(xSnap.guide); | ||
| } | ||
|
|
||
| const ySnap = computeAxisSnap(movingBounds, otherBounds, "y", yEdges, thresholdCanvasUnits); | ||
| if (ySnap) { | ||
| dy = ySnap.offset; | ||
| guides.push(ySnap.guide); | ||
| } | ||
|
|
||
| return { dx, dy, guides }; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After an element snaps, the next candidate starts from that already-snapped position but uses only the latest raw pointer delta. Moving away by less than the threshold per frame therefore snaps the element back on every move, so it remains stuck until one pointer event crosses the full threshold. The candidate position needs to come from an unsnapped drag origin or account for the previous snap offset.
Artifacts
Repro: executable TypeScript harness using the real whiteboard tool engine and Yjs document helpers
Repro: runtime trace showing raw pointer deltas, cumulative movement, bounds, snap guides, and the passing bug assertion