Skip to content

Add vertex-snap mode: Shift+Option+drag snaps to vertices with highlights#1007

Open
chriswhong wants to merge 5 commits into
mainfrom
cw/vertex-snap-mode
Open

Add vertex-snap mode: Shift+Option+drag snaps to vertices with highlights#1007
chriswhong wants to merge 5 commits into
mainfrom
cw/vertex-snap-mode

Conversation

@chriswhong

@chriswhong chriswhong commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Improves snapping by providing a modifier key for snapping to vertices only, not edges. Highlights vertices on nearby features.

More on the use case: I often need to hand-draw a polygon adjacent to an existing polygon, and want them to share vertices. Snapping is very helpful here, but depending on zoom the current snapping feature sometimes gets you close but not quite on the adjacent feature's vertex. There are also situations where many vertices make a smooth curve and it may not be clear where the vertices are, so showing them in a subtle way is helpful for knowing what needs to be added to a new polygon.

vertex snap
  • Option+drag a vertex snaps to the nearest edge of a nearby feature (existing behavior, unchanged)
  • Shift+Option+drag a vertex now snaps to the nearest actual vertex of nearby features (not the closest point on an edge)
  • When in vertex-snap mode, all vertices of features within ~80px of the cursor are highlighted as amber circles, giving visual guidance for precisely aligning polygon edges to match existing geometries

Implementation

  • New EphemeralEditingStateVertexSnap type in state/jotai.ts carries vertex positions to the render layer
  • getSnappingCoordinates in utils.ts accepts a verticesOnly flag — when true, snaps to the nearest coordinate vertex using turf distance() rather than nearestPointOnLine()
  • getNearbyVertices in utils.ts queries a wider 80px search box and collects all vertices from every nearby feature
  • New deck.gl ScatterplotLayer in pmap/index.ts renders the amber highlight circles when ephemeral state is 'vertex-snap'
  • Ephemeral state is cleared automatically on mouse-up (existing up handler)

Test plan

  • Select a feature with vertices (polygon or line)
  • Hold Option and drag a vertex — should snap to nearest point on nearby edges (existing behavior)
  • Hold Shift+Option and drag a vertex — nearby feature vertices should appear as amber circles; dragging should snap to the nearest highlighted vertex
  • Release mouse — highlights disappear
  • Verify no highlights appear when dragging without modifier keys

🤖 Generated with Claude Code

@chriswhong chriswhong requested a review from a team as a code owner June 11, 2026 14:32
@chriswhong chriswhong force-pushed the cw/vertex-snap-mode branch from 0f5c94f to 0b474cc Compare June 11, 2026 15:38

@AndrewSepic AndrewSepic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with existing polygons, but does not work when creating a new polygon. ie: creating a new Polygon adjacent to an existing one, does not have the ability to vertex snap, (although snapping to edges does work). I think this should behaviour should be the same whether you are working on an existing feature or creating a new feature.

chriswhong and others added 3 commits June 24, 2026 16:36
typeof dragTargetRef.current === 'string' was always false because
RawId is Opaque<number, 'RawId'>. The draggingVertex check never passed,
so highlights never appeared on keydown.

Fix mirrors the move handler: use !Array.isArray to distinguish a
single-vertex drag (RawId) from a multi-feature drag (string[]).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Fix line.ts snapping to exclude the feature being drawn (missing excludeFeatureId)
- Add vertex-snap visual feedback (highlight circles) during drawing for both line and polygon modes
- Alt+Shift during drawing now snaps to vertices only (matching edit mode behavior) instead of conflicting with direction locking
- Update mode hints to document Option+Shift vertex snapping and only show snap hint for features with vertices

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@chriswhong chriswhong force-pushed the cw/vertex-snap-mode branch from c21809e to b0ae9b5 Compare June 24, 2026 21:07
- Snap first vertex of new lines and polygons when Alt is held
- Show vertex snap highlights before first click in line/polygon draw modes
- Add full snap support (Alt/Alt+Shift) when drawing points

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@chriswhong

Copy link
Copy Markdown
Contributor Author

Snapping is now working when drawing new linestrings and polygons, and I also made sure that it also works when when drawing the initial vertex of a linestring or polygon, or when drawing a point.

@AndrewSepic AndrewSepic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Looking good. Left a small comment to make sure the change was intentional.

Comment thread app/lib/handlers/none.ts

// Immediately show/hide vertex-snap highlights when modifier keys change,
// without waiting for the next mousemove event.
useEffect(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the most recent commits, the vertexes are shown on a geometry when you press option, not when you press option+shift. I don't know if that's a bad thing, but it's worth sharing. Maybe a bit strange that you are showing vertexes when you aren't snapping to them..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants