(
animations: GsapAnimation[],
elementStartTime: number,
options?: {
baseX?: number;
baseY?: number;
baseScale?: number;
clampTimeToZero?: boolean;
skipBaseSet?: boolean;
},
)
| 379 | } |
| 380 | |
| 381 | export function gsapAnimationsToKeyframes( |
| 382 | animations: GsapAnimation[], |
| 383 | elementStartTime: number, |
| 384 | options?: { |
| 385 | baseX?: number; |
| 386 | baseY?: number; |
| 387 | baseScale?: number; |
| 388 | clampTimeToZero?: boolean; |
| 389 | skipBaseSet?: boolean; |
| 390 | }, |
| 391 | ): Keyframe[] { |
| 392 | const validMethods: GsapMethod[] = ["set", "to", "from", "fromTo"]; |
| 393 | const baseX = options?.baseX ?? 0; |
| 394 | const baseY = options?.baseY ?? 0; |
| 395 | const baseScale = options?.baseScale ?? 1; |
| 396 | const clampTimeToZero = options?.clampTimeToZero ?? true; |
| 397 | const skipBaseSet = options?.skipBaseSet ?? false; |
| 398 | const baseTimeEpsilon = 0.001; |
| 399 | const baseValueEpsilon = 0.00001; |
| 400 | |
| 401 | return ( |
| 402 | animations |
| 403 | .filter( |
| 404 | (a): a is GsapAnimation & { position: number } => |
| 405 | validMethods.includes(a.method) && typeof a.position === "number", |
| 406 | ) |
| 407 | // fallow-ignore-next-line complexity |
| 408 | .map((a) => { |
| 409 | const relativeTimeRaw = a.position - elementStartTime; |
| 410 | const time = clampTimeToZero ? Math.max(0, relativeTimeRaw) : relativeTimeRaw; |
| 411 | |
| 412 | const properties: Partial<KeyframeProperties> = {}; |
| 413 | for (const [key, value] of Object.entries(a.properties)) { |
| 414 | if (typeof value !== "number") continue; |
| 415 | if (key === "x") properties.x = value - baseX; |
| 416 | else if (key === "y") properties.y = value - baseY; |
| 417 | else if (key === "scale") { |
| 418 | properties.scale = baseScale !== 0 ? value / baseScale : value; |
| 419 | } else { |
| 420 | (properties as Record<string, number>)[key] = value; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if ( |
| 425 | skipBaseSet && |
| 426 | a.method === "set" && |
| 427 | time < baseTimeEpsilon && |
| 428 | Object.values(properties).every( |
| 429 | (v) => typeof v === "number" && Math.abs(v) < baseValueEpsilon, |
| 430 | ) |
| 431 | ) { |
| 432 | return null; |
| 433 | } |
| 434 | |
| 435 | return { |
| 436 | id: a.id.replace(/^.*-kf-/, ""), |
| 437 | time, |
| 438 | properties: properties as KeyframeProperties, |
no test coverage detected