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
26 changes: 26 additions & 0 deletions packages/core/src/runtime/adapters/css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ describe("css adapter", () => {
vi.restoreAllMocks();
});

it("does not rescan element animations on every seek", () => {
const el = document.createElement("div");
el.style.animationName = "spin";
document.body.appendChild(el);

vi.spyOn(window, "getComputedStyle").mockImplementation(() => {
return { animationName: "spin" } as CSSStyleDeclaration;
});

const animation = { currentTime: 0, pause: vi.fn(), play: vi.fn() } as unknown as Animation;
const getAnimations = vi.fn(() => [animation]);
(el as HTMLElement & { getAnimations?: () => Animation[] }).getAnimations = getAnimations;

const adapter = createCssAdapter();
adapter.discover();
adapter.seek({ time: 1 });
adapter.seek({ time: 2 });
adapter.seek({ time: 3 });

expect(getAnimations).toHaveBeenCalledTimes(1);
expect(animation.currentTime).toBe(3000);

document.body.removeChild(el);
vi.restoreAllMocks();
});

it("play resumes WAAPI animations and restores inline styles", () => {
const el = document.createElement("div");
el.style.animationName = "spin";
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/runtime/adapters/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function createCssAdapter(params?: {
el: HTMLElement;
baseDelay: string;
basePlayState: string;
animations: Animation[];
}> = [];

const getAnimationsForElement = (el: HTMLElement): Animation[] => {
Expand Down Expand Up @@ -84,6 +85,7 @@ export function createCssAdapter(params?: {
el: rawEl,
baseDelay: rawEl.style.animationDelay || "",
basePlayState: rawEl.style.animationPlayState || "",
animations: getAnimationsForElement(rawEl),
});
}
},
Expand All @@ -95,7 +97,7 @@ export function createCssAdapter(params?: {
? params.resolveStartSeconds(entry.el)
: Number.parseFloat(entry.el.getAttribute("data-start") ?? "0") || 0;
const localTimeMs = Math.max(0, time - start) * 1000;
const animations = getAnimationsForElement(entry.el);
const animations = entry.animations;
if (animations.length > 0) {
seekAnimations(animations, localTimeMs);
continue;
Expand All @@ -109,7 +111,7 @@ export function createCssAdapter(params?: {
pause: () => {
for (const entry of entries) {
if (!entry.el.isConnected) continue;
const animations = getAnimationsForElement(entry.el);
const animations = entry.animations;
if (animations.length > 0) {
pauseAnimations(animations);
}
Expand All @@ -120,7 +122,7 @@ export function createCssAdapter(params?: {
for (const entry of entries) {
if (!entry.el.isConnected) continue;
restoreInlineStyles(entry);
playAnimations(getAnimationsForElement(entry.el));
playAnimations(entry.animations);
}
},
revert: () => {
Expand Down
Loading
Loading