Skip to content

First onMouseDown event jumps because default initialClickPosition #545

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
mrgrg opened this issue Mar 28, 2025 · 0 comments
Open

First onMouseDown event jumps because default initialClickPosition #545

mrgrg opened this issue Mar 28, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@mrgrg
Copy link

mrgrg commented Mar 28, 2025

Description

At the very first dragging interaction, the default initialClickPosition causes a jump. This jump is as large as the distance between my mouse and the default position, which is {x: 0, y: 0}. In other words, it doesn't matter where I place my cursor; the window's top-left position will jump to the cursor's exact location.

This issue occurs when I:

  • start to pan within the canvas (using the mouse)
  • or grab a Node to move it around.

Here is a video for demonstration:

IMPORTANT!
In the video there are jumps where the element is replaced to its starting position. It is becaues I am refreshing the browser. With this action the initialClickPosition is also resets to its default value: {x: 0, y: 0}. Other than that, when a circle appears around the cursor, it indicates a click. Those jumps are the indications of the bug.

output_video.mp4

Reproduction

I think this bug occurs because the initialClickPosition still has its default value when the first event fires. These lines are responsible for handling this event:

Inside: src/lib/containers/Graph :

    ```

function onMouseDown(e: MouseEvent) {
if (!pannable && !(e.shiftKey || e.metaKey)) return;
if (e.button === 2) return;
if ($graphDOMElement) $graphDOMElement.focus();

	const { clientX, clientY } = e;

	$initialClickPosition = get(cursor);

	if (e.shiftKey || e.metaKey) {
		e.preventDefault();
		selecting = true;
		const { top, left } = dimensions;
		anchor.y = clientY - top;
		anchor.x = clientX - left;
		anchor.top = top;
		anchor.left = left;
		if (e.shiftKey && e.metaKey) {
			creating = true;
		} else {
			creating = false;
		}

		if (e.metaKey && !e.shiftKey) {
			adding = true;
		} else {
			adding = false;
		}
	} else {
		isMovable = true;
		$selected = new Set();
		$selected = $selected;
	}
}


This event can be traced further, as it is where the initialClickPosition is handled. Therefore, this should be managed before the original event, so that by the time it is used, it already has the correct cursor position.

`src/lib/stores/CursorStore.ts:`

export const cursorPositionRaw = readable({ x: 0, y: 0 }, (set) => {
const updateCursorPosition = (e: MouseEvent) => {
set({ x: e.clientX, y: e.clientY });
};

document.addEventListener('mousemove', updateCursorPosition);

return () => {
	window.removeEventListener('mousemove', updateCursorPosition);
};

});



It focuses on the event that directly interacts with the mouse, but the issue might also occur during the touch event.

### System information

Both in:
- chrome
- firefox

With Svelte 5

### Additional information

_No response_

### 👨‍👧‍👦 Contributing

- [ ] 🙋‍♂️ Yes, I'd love to make a PR to fix this bug!
@mrgrg mrgrg added the bug Something isn't working label Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant