Skip to content

Write HTML. Render video. Built for agents.

perf(studio): track the motion path only while a path or create ring is drawn (#4795)

Miguel Ángel44138931ce12
diff --git a/packages/studio/src/components/editor/useMotionPathData.test.tsx b/packages/studio/src/components/editor/useMotionPathData.test.tsx
new file mode 100644
index 0000000000..84c666bbaa
--- /dev/null
+++ b/packages/studio/src/components/editor/useMotionPathData.test.tsx
@@ -0,0 +1,64 @@
+// @vitest-environment happy-dom
+import { act, useRef } from "react";
+import { createRoot } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { useMotionPathData } from "./useMotionPathData";
+import { resetOverlayFrameLoopForTests } from "./overlayFrameLoop";
+import { usePlayerStore } from "../../player/store/playerStore";
+
+Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);
+
+const originalRaf = window.requestAnimationFrame;
+let frames: Array<() => void> = [];
+const runFrames = (count: number) => {
+ for (let i = 0; i < count; i++) {
+ const batch = frames;
+ frames = [];
+ act(() => {
+ for (const frame of batch) frame();
+ vi.advanceTimersByTime(16);
+ });
+ }
+};
+
+beforeEach(() => {
+ vi.useFakeTimers({
+ toFake: ["setTimeout", "clearTimeout", "setInterval", "clearInterval", "performance"],
+ });
+ window.requestAnimationFrame = ((callback: FrameRequestCallback) =>
+ frames.push(() => callback(performance.now()))) as typeof window.requestAnimationFrame;
+ usePlayerStore.setState({ previewBooted: true, motionPathArmed: false });
+});
+
+afterEach(() => {
+ resetOverlayFrameLoopForTests();
+ window.requestAnimationFrame = originalRaf;
+ vi.useRealTimers();
+ usePlayerStore.setState({ previewBooted: false, motionPathArmed: false });
+ document.body.innerHTML = "";
+});
+
+it("reads no layout per frame until there is a path or a create ring to draw", () => {
+ const iframe = document.createElement("iframe");
+ document.body.append(iframe);
+ const reads = vi.spyOn(iframe, "getBoundingClientRect");
+ function Probe() {
+ const ref = useRef(iframe);
+ useMotionPathData(ref, "#box");
+ return null;
+ }
+ const host = document.createElement("div");
+ document.body.append(host);
+ const root = createRoot(host);
+ try {
+ act(() => root.render(<Probe />));
+ runFrames(10);
+ expect(reads).not.toHaveBeenCalled();
+
+ act(() => usePlayerStore.setState({ motionPathArmed: true }));
+ runFrames(10);
+ expect(reads).toHaveBeenCalled();
+ } finally {
+ act(() => root.unmount());
+ }
+});
diff --git a/packages/studio/src/components/editor/useMotionPathData.ts b/packages/studio/src/components/editor/useMotionPathData.ts
index 46a69cbe9e..52e5e1c3e0 100644
--- a/packages/studio/src/components/editor/useMotionPathData.ts
+++ b/packages/studio/src/components/editor/useMotionPathData.ts
@@ -3,6 +3,7 @@ import { readRuntimeKeyframes } from "../../hooks/gsapRuntimeKeyframes";
import { isElementVisibleForOverlay } from "./domEditOverlayGeometry";
import { buildMotionPathGeometry, type MotionPathGeometry } from "./motionPathGeometry";
import { subscribeOverlayFrame } from "./overlayFrameLoop";
+import { usePlayerStore } from "../../player/store/playerStore";
type Rect = { left: number; top: number; width: number; height: number };
@@ -114,6 +115,8 @@ export function useMotionPathData(
// Perspective magnification (1/m44) of the selected element — applied to the
// path's offset points so depth (translateZ) elements' paths track on screen.
const [pScale, setPScale] = useState(1);
+ const armed = usePlayerStore((s) => s.motionPathArmed);
+ const drawn = geometry !== null || armed;
useEffect(() => {
if (!selector) {
@@ -122,6 +125,7 @@ export function useMotionPathData(
return;
}
setHome(null);
+ if (!drawn) return;
const tick = () => {
const el = iframeRef.current;
if (el) {
@@ -155,7 +159,7 @@ export function useMotionPathData(
}
};
return subscribeOverlayFrame(tick);
- }, [selector, iframeRef]);
+ }, [selector, iframeRef, drawn]);
useEffect(() => {
if (!selector) {