Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

1.5 KB
Raw Blame
/** Restart the CSS animations owned by one marked interactive graphic. */
export function replaySvgAnimations(root: Element): void {
  const elements = [root, ...Array.from(root.querySelectorAll("*"))];
  for (const element of elements) {
    for (const animation of element.getAnimations()) {
      try {
        animation.cancel();
        animation.play();
      } catch {
        // The graphic can leave the document during a pointer transition.
      }
    }
  }
}

/** Replay a marked graphic whenever its owning interactive surface is entered. */
export function bindSvgReplayOnPointerEnter(
  graphic: SVGSVGElement,
  beforeReplay?: () => void,
): () => void {
  const owner =
    graphic.closest<HTMLElement>(
      "button, a, [role='button'], [role='img']",
    ) ?? graphic;
  const handlePointerOver = (event: Event) => {
    const pointer = event as PointerEvent;
    if (pointer.pointerType === "touch") return;
    if (pointer.relatedTarget instanceof Node && owner.contains(pointer.relatedTarget)) {
      return;
    }
    beforeReplay?.();
    replaySvgAnimations(graphic);
  };
  owner.addEventListener("pointerover", handlePointerOver);
  const handleFocus = () => {
    if (owner.matches(":focus-visible")) {
      beforeReplay?.();
      replaySvgAnimations(graphic);
    }
  };
  owner.addEventListener("focusin", handleFocus);
  return () => {
    owner.removeEventListener("pointerover", handlePointerOver);
    owner.removeEventListener("focusin", handleFocus);
  };
}