pycodestyle icon indicating copy to clipboard operation
pycodestyle copied to clipboard

Possible to programmatically pan/scroll?

Open nihirv opened this issue 1 year ago • 0 comments

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?

nihirv avatar Nov 07 '24 20:11 nihirv