Skip to content

Possible to programmatically pan/scroll? #524

@nihirv

Description

@nihirv

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions