mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(core): wrap looping Lottie by frame so a loop never holds at its seam
This commit is contained in:
@@ -141,8 +141,8 @@ describe("lottie adapter", () => {
|
||||
adapter.seek({ time: 5 });
|
||||
adapter.seek({ time: 8 });
|
||||
expect(cycle.goToAndStop.mock.calls).toEqual([
|
||||
[1000, false],
|
||||
[0, false],
|
||||
[30, true],
|
||||
[0, true],
|
||||
]);
|
||||
expect(once.goToAndStop.mock.calls).toEqual([
|
||||
[119, true],
|
||||
@@ -150,6 +150,13 @@ describe("lottie adapter", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("wraps a loop whose length is not a whole number of seconds without holding at the seam", () => {
|
||||
const cycle = { ...createLottieWebAnim({ totalFrames: 901, frameRate: 30 }), loop: true };
|
||||
lottieWindow.__hfLottie = [cycle];
|
||||
createLottieAdapter().seek({ time: (9 * 901) / 30 });
|
||||
expect(cycle.goToAndStop).toHaveBeenCalledWith(0, true);
|
||||
});
|
||||
|
||||
it("holds a one-shot lottie-web animation on its last frame past its end", () => {
|
||||
const anim = createLottieWebAnim({ totalFrames: 30, frameRate: 30 });
|
||||
lottieWindow.__hfLottie = [anim];
|
||||
|
||||
@@ -78,19 +78,22 @@ export function createLottieAdapter(): RuntimeDeterministicAdapter {
|
||||
},
|
||||
|
||||
seek: (ctx) => {
|
||||
const seekTime = Math.max(0, Number(ctx.time) || 0);
|
||||
const time = Math.max(0, Number(ctx.time) || 0);
|
||||
const instances = (window as LottieWindow).__hfLottie;
|
||||
if (!instances || instances.length === 0) return;
|
||||
|
||||
for (const anim of instances) {
|
||||
try {
|
||||
const time = animationTime(anim, seekTime);
|
||||
const loops = anim.loop === true;
|
||||
if (isLottieWebAnimation(anim)) {
|
||||
// lottie-web: AnimationItem
|
||||
// goToAndStop(value, isFrame) — isFrame=true means frame number, false means time in ms
|
||||
// We use isFrame=false and pass time in ms for precision.
|
||||
// lottie-web draws nothing past the file's end, so a finished one-shot holds its last frame.
|
||||
if (anim.totalFrames > 0 && time * anim.frameRate >= anim.totalFrames) {
|
||||
// lottie-web draws nothing past the file's end, so a loop wraps and a finished one-shot
|
||||
// holds its last frame; both seek by whole-file frame number to avoid float drift.
|
||||
const frame = time * anim.frameRate;
|
||||
if (anim.totalFrames > 0 && loops) {
|
||||
anim.goToAndStop(frame % anim.totalFrames, true);
|
||||
} else if (anim.totalFrames > 0 && frame >= anim.totalFrames) {
|
||||
anim.goToAndStop(anim.totalFrames - 1, true);
|
||||
} else {
|
||||
anim.goToAndStop(time * 1000, false);
|
||||
@@ -103,7 +106,7 @@ export function createLottieAdapter(): RuntimeDeterministicAdapter {
|
||||
// dotlottie-web v2+: direct frame setter
|
||||
const totalFrames = anim.totalFrames ?? 0;
|
||||
const fps = anim.frameRate ?? 30;
|
||||
const frame = time * fps;
|
||||
const frame = loops && totalFrames > 0 ? (time * fps) % totalFrames : time * fps;
|
||||
if (totalFrames > 0) {
|
||||
anim.setCurrentRawFrameValue(Math.min(frame, totalFrames - 1));
|
||||
}
|
||||
@@ -111,7 +114,10 @@ export function createLottieAdapter(): RuntimeDeterministicAdapter {
|
||||
// dotlottie-web v1: seek(percentage 0-100)
|
||||
const duration = anim.duration ?? 0;
|
||||
if (Number.isFinite(duration) && duration > 0) {
|
||||
const percentage = Math.min(100, (time / duration) * 100);
|
||||
const percentage = Math.min(
|
||||
100,
|
||||
((loops ? time % duration : time) / duration) * 100,
|
||||
);
|
||||
anim.seek(percentage);
|
||||
}
|
||||
}
|
||||
@@ -190,13 +196,6 @@ function finiteFramesToSeconds(
|
||||
return totalFrames / frameRate;
|
||||
}
|
||||
|
||||
/** Composition time mapped into a `loop: true` animation's own cycle; other animations get it unchanged. */
|
||||
function animationTime(anim: LottieWebAnimation | DotLottiePlayer, time: number): number {
|
||||
if (anim.loop !== true) return time;
|
||||
const seconds = inferAnimationDurationSeconds(anim);
|
||||
return seconds ? time % seconds : time;
|
||||
}
|
||||
|
||||
/** The inferred duration in seconds for one registered lottie-web/dotLottie instance, or null. */
|
||||
function inferAnimationDurationSeconds(anim: LottieWebAnimation | DotLottiePlayer): number | null {
|
||||
if (isLottieWebAnimation(anim)) {
|
||||
|
||||
Reference in New Issue
Block a user