Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/studio/src/hooks/useGestureRecording.sealed.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ vi.mock("../player/lib/previewChannel", async (load) => ({
...(await load<typeof import("../player/lib/previewChannel")>()),
previewFrameOpaque: () => frame.sealed,
}));
const drafts = vi.hoisted(() => ({ draw: vi.fn(), end: vi.fn() }));
vi.mock("../components/editor/previewDrafts", () => ({
drawPreviewDraft: drafts.draw,
endPreviewDraft: drafts.end,
}));
vi.mock("../player/lib/bridgePlayback", async (load) => ({
...(await load<typeof import("../player/lib/bridgePlayback")>()),
previewStageSize: () => frame.stage,
Expand All @@ -35,6 +40,8 @@ beforeEach(() => {
vi.stubGlobal("cancelAnimationFrame", vi.fn());
vi.spyOn(performance, "now").mockImplementation(() => nowMs);
frame.sealed = true;
drafts.draw.mockClear();
drafts.end.mockClear();
usePlayerStore.setState({ currentTime: 0, duration: 10, requestedSeekTime: null });
});

Expand Down Expand Up @@ -108,6 +115,22 @@ describe("a gesture recording on a sealed preview", () => {
expect(recordOnSealedFrame(100, { ...AT_REST, x: 240, y: 80 }).last).toEqual({ x: 440, y: 80 });
});

// fails without the fix: the frame's layer stood still while recording, since only an unsealed frame's GSAP moved it.
it("draws the layer moving live on the frame, and puts it back when the recording stops", () => {
frame.stage = { width: 1920, height: 1080 };
recordOnSealedFrame(100, { ...AT_REST, x: 240, y: 80 });
const [standIn, last] = drafts.draw.mock.lastCall!;
expect(last).toEqual({ translate: { x: 200, y: 0 } });
expect(drafts.end.mock.calls).toEqual([[standIn, "restore"]]);
});

it("draws nothing over the channel for an unsealed frame, whose GSAP moves the layer", () => {
frame.sealed = false;
frame.stage = { width: 1920, height: 1080 };
recordOnSealedFrame(100, null);
expect([drafts.draw.mock.calls, drafts.end.mock.calls]).toEqual([[], []]);
});

it("scales the pointer by the film's own width, not 1920", () => {
frame.stage = { width: 1080, height: 1920 };
expect(recordOnSealedFrame(100, AT_REST).last).toEqual({ x: 113, y: 0 });
Expand Down
13 changes: 13 additions & 0 deletions packages/studio/src/hooks/useGestureRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useLinkedClipPreferences } from "../utils/linkedClipPreferences";
import { previewFrameOpaque } from "../player/lib/previewChannel";
import { previewStageSize } from "../player/lib/bridgePlayback";
import { getIframeGsap, type IframeGsap } from "./gsapShared";
import { drawPreviewDraft, endPreviewDraft } from "../components/editor/previewDrafts";
import type { EditingGestureBase } from "@hyperframes/core/runtime/editing-port";

export interface GestureSample {
Expand Down Expand Up @@ -222,6 +223,8 @@ function resolveGestureProperties(
// ---------------------------------------------------------------------------

interface RecordingRefs {
/** A sealed frame's stand-in, whose draft shows the recording live. */
drawsOn: HTMLElement | null;
pointer: { x: number; y: number };
startPointer: { x: number; y: number };
hasMoved: boolean;
Expand Down Expand Up @@ -260,10 +263,13 @@ function createRecordingRefs(): RecordingRefs {
cleanup: null,
playhead: null,
lastTime: Infinity,
drawsOn: null,
};
}

function releaseRuntimePreview(r: RecordingRefs): void {
if (r.drawsOn) endPreviewDraft(r.drawsOn, "restore");
r.drawsOn = null;
const runtime = r.runtime;
if (!runtime) return;
const { element, savedVisibility, savedTranslate } = runtime;
Expand Down Expand Up @@ -350,6 +356,7 @@ export function useGestureRecording() {
// --- Phase 3: Connect to the iframe GSAP runtime ---
r.runtime = connectGsapRuntime(element, iframeEl, elementEndTime);
const { currentTime, duration } = usePlayerStore.getState();
r.drawsOn = sealed ? element : null;
r.playhead = sealed
? { start: currentTime, end: Math.min(elementEndTime ?? Infinity, duration) }
: null;
Expand Down Expand Up @@ -447,6 +454,12 @@ export function useGestureRecording() {
} else if (r.playhead) {
usePlayerStore.getState().requestSeek(Math.min(r.playhead.start + time, r.playhead.end));
}
if (r.drawsOn && ("x" in properties || "y" in properties)) {
const { x = r.basePosition.x, y = r.basePosition.y } = properties;
drawPreviewDraft(r.drawsOn, {
translate: { x: x - r.basePosition.x, y: y - r.basePosition.y },
});
}

recordSample(r, time, properties);

Expand Down
Loading