Skip to content

Write HTML. Render video. Built for agents.

fix(core): an animation playing at its first seek shows the sought time (#5503)

Cathryn Lavery5e6e58fc9bae
diff --git a/packages/core/src/runtime/adapters/_holdAnimationAt.test.ts b/packages/core/src/runtime/adapters/_holdAnimationAt.test.ts
new file mode 100644
index 0000000000..baa62bb85c
--- /dev/null
+++ b/packages/core/src/runtime/adapters/_holdAnimationAt.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+import { holdAnimationAt } from "./_holdAnimationAt";
+import { PendingPauseAnimation } from "./pendingPauseAnimation.test-helpers";
+
+const hold = (animation: PendingPauseAnimation, timeMs: number) =>
+ holdAnimationAt(animation as unknown as Animation, timeMs, "test.currentTime", "test.pause");
+
+describe("holdAnimationAt", () => {
+ it("holds the sought time on the first frame of a running animation", () => {
+ const animation = new PendingPauseAnimation();
+
+ hold(animation, 1000);
+ animation.nextFrame();
+
+ expect(animation.currentTime).toBe(1000);
+ expect(animation.playState).toBe("paused");
+ });
+
+ it("writes the time and retries pause when the first pause throws", () => {
+ const animation = new PendingPauseAnimation({ unresolvedPause: true });
+
+ hold(animation, 1000);
+
+ expect(animation.currentTime).toBe(1000);
+ expect(animation.calls).toEqual(["pause", "currentTime", "pause"]);
+ });
+
+ it("calls pause once when the first call succeeds", () => {
+ const animation = new PendingPauseAnimation();
+
+ hold(animation, 1000);
+
+ expect(animation.pauseCalls).toBe(1);
+ expect(animation.calls).toEqual(["pause", "currentTime"]);
+ });
+});
diff --git a/packages/core/src/runtime/adapters/_holdAnimationAt.ts b/packages/core/src/runtime/adapters/_holdAnimationAt.ts
new file mode 100644
index 0000000000..42343d571c
--- /dev/null
+++ b/packages/core/src/runtime/adapters/_holdAnimationAt.ts
@@ -0,0 +1,29 @@
+import { swallow } from "../diagnostics";
+
+export function holdAnimationAt(
+ animation: Animation,
+ timeMs: number,
+ currentTimeSite: string,
+ pauseSite: string,
+): void {
+ let pauseFailed = false;
+ // Pause first so the pending pause holds the seek time, not the next frame's time.
+ try {
+ animation.pause();
+ } catch (err) {
+ pauseFailed = true;
+ swallow(pauseSite, err);
+ }
+ try {
+ animation.currentTime = timeMs;
+ } catch (err) {
+ swallow(currentTimeSite, err);
+ }
+ if (pauseFailed) {
+ try {
+ animation.pause();
+ } catch (err) {
+ swallow(pauseSite, err);
+ }
+ }
+}
diff --git a/packages/core/src/runtime/adapters/css.test.ts b/packages/core/src/runtime/adapters/css.test.ts
index dd47f12358..b7c3489fa8 100644
--- a/packages/core/src/runtime/adapters/css.test.ts
+++ b/packages/core/src/runtime/adapters/css.test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";
import { createCssAdapter } from "./css";
+import { PendingPauseAnimation } from "./pendingPauseAnimation.test-helpers";
// jsdom has no Animation subclasses; the adapter tells them apart with instanceof CSSAnimation.
class FakeAnimation {}
@@ -203,6 +204,18 @@ describe("css adapter", () => {
vi.restoreAllMocks();
});
+ it("holds the sought time on the first frame of a running CSS animation", () => {
+ vi.stubGlobal("CSSAnimation", PendingPauseAnimation);
+ const animation = new PendingPauseAnimation({ target: el });
+ const { adapter } = setup([animation as unknown as Animation]);
+
+ adapter.seek({ time: 2 });
+ animation.nextFrame();
+
+ expect(animation.currentTime).toBe(1000);
+ expect(animation.pauseCalls).toBe(1);
+ });
+
it("seek drives the live animation, not the one seen at discover", () => {
const stale = makeAnimation(el);
const current = makeAnimation(el);
diff --git a/packages/core/src/runtime/adapters/css.ts b/packages/core/src/runtime/adapters/css.ts
index d859e0896e..b2cb4ef653 100644
--- a/packages/core/src/runtime/adapters/css.ts
+++ b/packages/core/src/runtime/adapters/css.ts
@@ -1,6 +1,7 @@
import type { RuntimeDeterministicAdapter } from "../types";
import { swallow } from "../diagnostics";
import { isHtmlElement } from "../domRealm";
+import { holdAnimationAt } from "./_holdAnimationAt";
export function createCssAdapter(params?: {
resolveStartSeconds?: (element: Element) => number;
@@ -98,18 +99,12 @@ export function createCssAdapter(params?: {
const seekAnimations = (animations: Animation[], timeMs: number) => {
for (const animation of animations) {
- try {
- animation.currentTime = timeMs;
- } catch (err) {
- // ignore animations that reject currentTime writes
- swallow("runtime.adapters.css.site1", err);
- }
- try {
- animation.pause();
- } catch (err) {
- // infinite unresolved animations can throw on pause before currentTime sticks
- swallow("runtime.adapters.css.site2", err);
- }
+ holdAnimationAt(
+ animation,
+ timeMs,
+ "runtime.adapters.css.site1",
+ "runtime.adapters.css.site2",
+ );
}
};
diff --git a/packages/core/src/runtime/adapters/pendingPauseAnimation.test-helpers.ts b/packages/core/src/runtime/adapters/pendingPauseAnimation.test-helpers.ts
new file mode 100644
index 0000000000..cd8b8d8c38
--- /dev/null
+++ b/packages/core/src/runtime/adapters/pendingPauseAnimation.test-helpers.ts
@@ -0,0 +1,53 @@
+export class PendingPauseAnimation {
+ private timelineMs = 0;
+ private startTimeMs = 0;
+ private holdTimeMs = 0;
+ private pendingPause = false;
+ private unresolvedPause: boolean;
+ readonly effect: { target: Element } | null;
+ readonly calls: string[] = [];
+ pauseCalls = 0;
+ playState: "running" | "paused" = "running";
+
+ constructor(options: { target?: Element; unresolvedPause?: boolean } = {}) {
+ this.effect = options.target ? { target: options.target } : null;
+ this.unresolvedPause = options.unresolvedPause ?? false;
+ }
+
+ get currentTime(): number | null {
+ if (this.unresolvedPause) return null;
+ return this.playState === "paused" ? this.holdTimeMs : this.timelineMs - this.startTimeMs;
+ }
+
+ set currentTime(timeMs: number) {
+ this.calls.push("currentTime");
+ this.unresolvedPause = false;
+ if (this.pendingPause) {
+ this.holdTimeMs = timeMs;
+ this.pendingPause = false;
+ this.playState = "paused";
+ } else if (this.playState === "running") {
+ this.startTimeMs = this.timelineMs - timeMs;
+ } else {
+ this.holdTimeMs = timeMs;
+ }
+ }
+
+ pause(): void {
+ this.calls.push("pause");
+ this.pauseCalls++;
+ if (this.unresolvedPause) throw new Error("unresolved time");
+ if (this.playState === "running") this.pendingPause = true;
+ }
+
+ addEventListener(): void {}
+
+ nextFrame(ms = 1000 / 60): void {
+ this.timelineMs += ms;
+ if (this.pendingPause) {
+ this.holdTimeMs = this.timelineMs - this.startTimeMs;
+ this.pendingPause = false;
+ this.playState = "paused";
+ }
+ }
+}
diff --git a/packages/core/src/runtime/adapters/waapi.test.ts b/packages/core/src/runtime/adapters/waapi.test.ts
index 9b29ce5d2b..a7a8e9375d 100644
--- a/packages/core/src/runtime/adapters/waapi.test.ts
+++ b/packages/core/src/runtime/adapters/waapi.test.ts
@@ -1,5 +1,6 @@
import { beforeEach, afterEach, describe, it, expect, vi } from "vitest";
import { createWaapiAdapter } from "./waapi";
+import { PendingPauseAnimation } from "./pendingPauseAnimation.test-helpers";
describe("waapi adapter", () => {
const originalDocument = (globalThis as { document?: unknown }).document;
@@ -68,6 +69,17 @@ describe("waapi adapter", () => {
expect(mockAnim.currentTime).toBe(2500); // seconds → ms
});
+ it("holds the sought time on the first frame of a running animation", () => {
+ const animation = new PendingPauseAnimation();
+ document.getAnimations = vi.fn(() => [animation as unknown as Animation]);
+
+ createWaapiAdapter().seek({ time: 1 });
+ animation.nextFrame();
+
+ expect(animation.currentTime).toBe(1000);
+ expect(animation.pauseCalls).toBe(1);
+ });
+
it("seek clamps negative time to 0", () => {
const mockAnim = makeAnimation();
setAnimations([mockAnim]);
diff --git a/packages/core/src/runtime/adapters/waapi.ts b/packages/core/src/runtime/adapters/waapi.ts
index 7ed4333988..2bde3eb20b 100644
--- a/packages/core/src/runtime/adapters/waapi.ts
+++ b/packages/core/src/runtime/adapters/waapi.ts
@@ -1,5 +1,6 @@
import type { RuntimeDeterministicAdapter } from "../types";
import { swallow } from "../diagnostics";
+import { holdAnimationAt } from "./_holdAnimationAt";
export function createWaapiAdapter(): RuntimeDeterministicAdapter {
let didDiscover = false;
@@ -162,18 +163,12 @@ export function createWaapiAdapter(): RuntimeDeterministicAdapter {
: ensureBaseline(animation, 0);
const localTimeMs =
baseline.animationTimeMs + Math.max(0, timeMs - baseline.compositionTimeMs);
- try {
- animation.currentTime = localTimeMs;
- } catch (err) {
- // ignore animations that reject currentTime writes
- swallow("runtime.adapters.waapi.site1", err);
- }
- try {
- animation.pause();
- } catch (err) {
- // infinite unresolved animations can throw here until currentTime resolves
- swallow("runtime.adapters.waapi.site2", err);
- }
+ holdAnimationAt(
+ animation,
+ localTimeMs,
+ "runtime.adapters.waapi.site1",
+ "runtime.adapters.waapi.site2",
+ );
}
},
pause: (ctx) => {