diff --git a/src/ui/src/lib/Map.svelte b/src/ui/src/lib/Map.svelte index fe4dfacf..f7002545 100644 --- a/src/ui/src/lib/Map.svelte +++ b/src/ui/src/lib/Map.svelte @@ -126,12 +126,12 @@ padding={{ top: 16, left: 16, bottom: 16, right: 16 }} > + - {:else} diff --git a/src/ui/src/lib/MapCoordinates.svelte b/src/ui/src/lib/MapCoordinates.svelte index 849b0bbb..ccb5a8fd 100644 --- a/src/ui/src/lib/MapCoordinates.svelte +++ b/src/ui/src/lib/MapCoordinates.svelte @@ -7,29 +7,33 @@ $: cursorX = 0; $: cursorY = 0; - const { xScale, yScale, width, height } = getContext('LayerCake'); + const { xScale, yScale, width, height, padding } = getContext('LayerCake'); - function updateCoordinates(event: PointerEvent) { + function updateCoordinates(event: MouseEvent) { if (!$xScale || !$yScale) return; - const svgElement = event.currentTarget as SVGElement; - const rect = svgElement.getBoundingClientRect(); - const x = event.clientX - rect.left; - const y = event.clientY - rect.top; + const target = event.target as SVGElement; + const svg = target.ownerSVGElement || target as SVGSVGElement; + const point = svg.createSVGPoint(); + point.x = event.clientX; + point.y = event.clientY; - cursorX = $xScale.invert((x - transform.x) / transform.k); - cursorY = $yScale.invert((y - transform.y) / transform.k); + // Convert screen coordinates to SVG coordinates + const svgPoint = point.matrixTransform(svg.getScreenCTM()?.inverse()); + + // Adjust for padding from context + const adjustedX = svgPoint.x - $padding.left; + const adjustedY = svgPoint.y - $padding.top; + + const transformedX = (adjustedX - transform.x) / transform.k; + const transformedY = (adjustedY - transform.y) / transform.k; + + cursorX = $xScale.invert(transformedX); + cursorY = $yScale.invert(transformedY); } - +