From a546fe38b74f9ed35c59ef2f79421b5ba2e3f2c5 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Fri, 2 Jan 2026 14:32:09 -0500 Subject: [PATCH] snap to cursor on click --- line-segment-intersection/src/App.svelte | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/line-segment-intersection/src/App.svelte b/line-segment-intersection/src/App.svelte index e1bc327..7193edc 100644 --- a/line-segment-intersection/src/App.svelte +++ b/line-segment-intersection/src/App.svelte @@ -77,18 +77,16 @@ } function handleCanvasClick(event) { - // If we're about to turn off tracking, update position one last time - if (trackHover) { - const rect = canvas.getBoundingClientRect(); - const mouseX = event.clientX - rect.left; - const mouseY = event.clientY - rect.top; + // Update position at click location before toggling tracking + const rect = canvas.getBoundingClientRect(); + const mouseX = event.clientX - rect.left; + const mouseY = event.clientY - rect.top; - // Add half grid unit offset for snapping to closest grid intersection - userPoint = { - x: Math.floor((mouseX + GRID_SIZE / 2) / GRID_SIZE), - y: Math.floor((HEIGHT - mouseY + GRID_SIZE / 2) / GRID_SIZE) - }; - } + // Add half grid unit offset for snapping to closest grid intersection + userPoint = { + x: Math.floor((mouseX + GRID_SIZE / 2) / GRID_SIZE), + y: Math.floor((HEIGHT - mouseY + GRID_SIZE / 2) / GRID_SIZE) + }; trackHover = !trackHover; }