diff --git a/src/joint.ts b/src/joint.ts index 660147b..7e47933 100644 --- a/src/joint.ts +++ b/src/joint.ts @@ -1,5 +1,11 @@ import { GeoPoint, TargetedEvent } from './types'; -import { createHtmlMarker, getJointDistanceText, getLabelHtml, getMarkerPopupHtml } from './utils'; +import { + createHtmlMarker, + getJointDistanceText, + getLabelHtml, + getMarkerPopupHtml, + getMousePosition, +} from './utils'; import { Evented } from './evented'; import { style } from './style'; @@ -205,7 +211,9 @@ export class Joint extends Evented { this.emit('move', { targetData: this }); - this.coordinates = this.map.unproject([ev.clientX, ev.clientY]); + const container = this.map.getContainer(); + + this.coordinates = this.map.unproject(getMousePosition(container, ev.clientX, ev.clientY)); this.marker?.setCoordinates(this.coordinates); }; diff --git a/src/utils.ts b/src/utils.ts index c5df078..b014b6f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -179,3 +179,12 @@ export function getSnapPoint(map: mapgl.Map, joints: Joint[], point: ScreenPoint return { point: geoPoint, distance, segment: bestSegmentIndex }; } + +export function getMousePosition( + container: HTMLElement, + clientX: number, + clientY: number, +): number[] { + const rect = container.getBoundingClientRect(); + return [clientX - rect.left - container.clientLeft, clientY - rect.top - container.clientTop]; +}