All files interpolate.ts

100% Statements 19/19
100% Branches 4/4
100% Functions 1/1
100% Lines 19/19

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99              5x 5x                                                 5x                                                         46x 46x   46x   46x 46x 46x 46x 46x   46x 46x 23x         46x 46x   46x                     46x   46x    
import { Tweenable, composeEasingObject, tweenProps } from './tweenable'
 
import { TweenState, Easing, TweenRawState } from './types'
 
// Fake a Tweenable and patch some internals.  This approach enables skipping
// uneccessary processing and object recreation, cutting down on garbage
// collection pauses.
const tweenable = new Tweenable()
const { filters } = Tweenable
 
/**
 * Compute the midpoint of two Objects.  This method effectively calculates a
 * specific frame of animation that {@link Tweenable#tween} does many times
 * over the course of a full tween.
 *
 * ```
 * import { interpolate } from 'shifty';
 *
 * const interpolatedValues = interpolate({
 *     width: '100px',
 *     opacity: 0,
 *     color: '#fff'
 *   }, {
 *     width: '200px',
 *     opacity: 1,
 *     color: '#000'
 *   },
 *   0.5
 * );
 *
 * console.log(interpolatedValues); // Logs: {opacity: 0.5, width: "150px", color: "rgb(127,127,127)"}
 * ```
 */
export const interpolate = <T extends TweenState>(
  /**
   * The starting values to tween from.
   */
  from: T,
  /**
   * The ending values to tween to.
   */
  to: T,
  /**
   * The normalized position value (between `0.0` and `1.0`) to interpolate the
   * values between `from` and `to` for.  `from` represents `0` and `to`
   * represents `1`.
   */
  position: number,
  /**
   * The easing curve(s) to calculate the midpoint against.  You can reference
   * any easing function attached to {@link Tweenable.easing}, or provide the
   * {@link EasingFunction}(s) directly.
   */
  easing: Easing = Tweenable.easing.linear,
  /**
   * Optional delay to pad the beginning of the interpolated tween with.  This
   * increases the range of `position` from (`0` through `1`) to (`0` through
   * `1 + delay`).  So, a delay of `0.5` would increase all valid values of
   * `position` to numbers between `0` and `1.5`.
   */
  delay = 0
): T => {
  const current = { ...from }
  const easingObject = composeEasingObject(from, easing)
 
  tweenable._filters.length = 0
 
  tweenable.setState({})
  tweenable._currentState = current
  tweenable._originalState = from
  tweenable._targetState = to
  tweenable._easing = easingObject
 
  for (const name in filters) {
    if (filters[name].doesApply(tweenable)) {
      tweenable._filters.push(filters[name])
    }
  }
 
  // Any defined value transformation must be applied
  tweenable._applyFilter('tweenCreated')
  tweenable._applyFilter('beforeTween')
 
  const interpolatedValues = tweenProps(
    position,
    current as TweenRawState,
    from as TweenRawState,
    to as TweenRawState,
    1,
    delay,
    easingObject
  )
 
  // Transform data in interpolatedValues back into its original format
  tweenable._applyFilter('afterTween')
 
  return interpolatedValues as T
}