All files / player-skin-sdk/src motion.ts

100% Statements 13/13
100% Branches 7/7
100% Functions 5/5
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38              713672x 713672x               5x 3x       79x 79x         6x 6x 6x         4x 3x    
/** Timing and easing helpers for skin animation, plus a deterministic hash for decorative jitter. */
 
/**
 * Deterministic [0, 1) hash for decorative jitter — the same seed always yields the same value, so pooled per-frame
 * redraws don't shimmer.
 */
export function hash01(seed: number): number {
  const value = Math.sin(seed * 12.9898 + 78.233) * 43758.5453;
  return value - Math.floor(value);
}
 
/**
 * 0..1 progress of a staged animation step that starts `delayMs` after the timeline origin and runs for `durationMs`.
 * Clamped at both ends so callers can compute every step from one elapsed clock.
 */
export function stageProgress(elapsedMs: number, delayMs: number, durationMs: number): number {
  if (durationMs <= 0) return elapsedMs >= delayMs ? 1 : 0;
  return Math.max(0, Math.min(1, (elapsedMs - delayMs) / durationMs));
}
 
export function easeOutCubic(t: number): number {
  const clamped = Math.max(0, Math.min(1, t));
  return 1 - (1 - clamped) ** 3;
}
 
/** Ease-out with a small overshoot past 1 before settling — the "slam" landing for tags and stamps. */
export function easeOutBack(t: number, overshoot = 1.70158): number {
  const clamped = Math.max(0, Math.min(1, t));
  const shifted = clamped - 1;
  return 1 + (overshoot + 1) * shifted ** 3 + overshoot * shifted ** 2;
}
 
/** Counter roll-up: the integer shown at `progress` (0..1) of counting up to `target`. */
export function rollUpValue(target: number, progress: number): number {
  if (!Number.isFinite(target)) return 0;
  return Math.round(target * easeOutCubic(progress));
}