moveCursorTo function exported

Last updated: 2026-03-05T11:38:55.014Z

Metrics

LOC: 51 Complexity: 6 Params: 3

Signature

moveCursorTo( target: string | Element, durationMs = 400, ): : Promise<void>

Summary

Animate the cursor to the centre of a DOM element.

Tags

#@param target CSS selector or Element#@param durationMs Animation duration in ms (default 400)#@returns Promise that resolves when the animation ends

Source Code

export function moveCursorTo(
  target: string | Element,
  durationMs = 400,
): Promise<void> {
  return new Promise((resolve) => {
    if (!cursorEl) {
      resolve();
      return;
    }

    const el =
      typeof target === "string" ? document.querySelector(target) : target;

    if (!el) {
      log.warn("Cursor target not found:", target);
      resolve();
      return;
    }

    const rect = el.getBoundingClientRect();
    const targetX = rect.left + rect.width / 2 - CURSOR_SIZE / 2;
    const targetY = rect.top + rect.height / 2 - CURSOR_SIZE / 2;

    // First call: teleport instantly so cursor appears at the correct position
    // rather than animating from (0, 0) top-left corner.
    const moveDuration = isPositioned ? durationMs : 0;
    isPositioned = true;

    cursorEl.style.transitionDuration = `${moveDuration}ms`;
    cursorEl.style.transform = `translate(${targetX}px, ${targetY}px)`;

    currentX = targetX;
    currentY = targetY;

    // If teleporting (duration=0) resolve immediately after next frame;
    // otherwise wait for the CSS transition to finish.
    if (moveDuration === 0) {
      requestAnimationFrame(() => resolve());
      return;
    }

    const onEnd = () => {
      cursorEl?.removeEventListener("transitionend", onEnd);
      resolve();
    };
    cursorEl.addEventListener("transitionend", onEnd);

    // Safety timeout in case transitionend doesn't fire
    setTimeout(resolve, durationMs + 50);
  });
}

Dependencies (Outgoing)

graph LR moveCursorTo["moveCursorTo"] style moveCursorTo fill:#dbeafe,stroke:#2563eb,stroke-width:2px click moveCursorTo "c03bcafc1bbb9bee.html"
TargetType
transitionend dynamic_call

Impact (Incoming)

graph LR moveCursorTo["moveCursorTo"] FillableElement["FillableElement"] FillableElement -->|uses| moveCursorTo style moveCursorTo fill:#dbeafe,stroke:#2563eb,stroke-width:2px click moveCursorTo "c03bcafc1bbb9bee.html" click FillableElement "2ecf5aaac3f668a8.html"
SourceType
FillableElement uses