Skip to content
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

Possible to programmatically pan/scroll? #524

Open
nihirv opened this issue Nov 7, 2024 · 0 comments
Open

Possible to programmatically pan/scroll? #524

nihirv opened this issue Nov 7, 2024 · 0 comments

Comments

@nihirv
Copy link

nihirv commented Nov 7, 2024

Hi,

I'm trying to scroll a node into view when a reference of it is clicked from somewhere. My logic was was to get the bounding rect of the svelvet canvas and the node rect, calculate the offset, and scroll that into view:

    const nodeElement = document.getElementById(`N-${node.id}`);
    if (nodeElement) {
      const canvasElement = document.getElementById("G-my-canvas");
      if (canvasElement) {
        const nodeRect = nodeElement.getBoundingClientRect();
        const canvasRect = canvasElement.getBoundingClientRect();

        // Calculate the offset to center the node
        const offsetX =
          nodeRect.left -
          canvasRect.left -
          canvasRect.width / 2 +
          nodeRect.width / 2;
        const offsetY =
          nodeRect.top -
          canvasRect.top -
          canvasRect.height / 2 +
          nodeRect.height / 2;

        // Adjust the canvas view
        canvasElement.scrollBy({
          top: offsetY,
          left: offsetX,
          behavior: "smooth",
        });

      }
    }

However, this shifts/translates the canvas as a whole (as opposed to shifting the content within the canvas).

Is there a way to scroll/focus upon a node itself?

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

No branches or pull requests

1 participant