All files / extensions/cornerstone/src/utils transitions.ts

72.85% Statements 51/70
50% Branches 10/20
90.9% Functions 10/11
72.72% Lines 48/66

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301                          78x     78x 78x 78x       231x       153x               78x   78x 231x 231x   231x 78x     153x     153x         153x                                                                   78x 78x 78x                                   78x 78x 78x     78x                                 300x       300x 81x 78x   78x       78x 78x                         60x                 60x                 60x                 60x                 60x                       300x 81x       81x   81x 81x     81x 81x                             60x                         60x                         60x                         60x                         60x       300x               60x              
/** Cubic Bezier Implementation */
 
/**
 * Finds the parameter t for a given x value on a cubic Bézier curve.
 * Uses Newton's method first (fast), then falls back to binary search (reliable).
 * Based on WebKit/Chromium's UnitBezier implementation.
 *
 * @param {number} targetX - The x value to find parameter t for
 * @param {number} x1 - x-coordinate of first control point
 * @param {number} x2 - x-coordinate of second control point
 * @returns {number} The parameter t that gives the specified x value
 */
function solveCubicBezierX(targetX: number, x1: number, x2: number): number {
  const epsilon = 1e-6;
 
  // Pre-compute polynomial coefficients for performance
  const cx = 3.0 * x1;
  const bx = 3.0 * (x2 - x1) - cx;
  const ax = 1.0 - cx - bx;
 
  function sampleCurveX(t: number): number {
    // Evaluate: ax*t³ + bx*t² + cx*t using Horner's rule
    return ((ax * t + bx) * t + cx) * t;
  }
 
  function sampleCurveDerivativeX(t: number): number {
    return (3.0 * ax * t + 2.0 * bx) * t + cx;
  }
 
  /**
   * Newton's method - fast convergence with good initial guess
   * Try this first as it's normally very fast
   */
  function newtonRaphsonMethod(targetX: number): number | null {
    let t = targetX;
 
    for (let iteration = 0; iteration < 8; iteration++) {
      const currentX = sampleCurveX(t);
      const error = currentX - targetX;
 
      if (Math.abs(error) < epsilon) {
        return t;
      }
 
      const derivative = sampleCurveDerivativeX(t);
 
      // Break if derivative is too small (avoid division by zero)
      Iif (Math.abs(derivative) < epsilon) {
        break;
      }
 
      // Newton-Raphson step: t_new = t_old - f(t_old) / f'(t_old)
      t = t - error / derivative;
    }
 
    return null;
  }
 
  /**
   * Binary search fallback - guaranteed to converge
   * Use this when Newton's method fails
   */
  function binarySearchFallback(targetX: number): number {
    let lowerBound = 0.0;
    let upperBound = 1.0;
    let t = targetX;
 
    while (lowerBound < upperBound) {
      const currentX = sampleCurveX(t);
 
      Iif (Math.abs(currentX - targetX) < epsilon) {
        return t;
      }
 
      if (targetX > currentX) {
        lowerBound = t;
      } else {
        upperBound = t;
      }
 
      t = (upperBound - lowerBound) * 0.5 + lowerBound;
    }
 
    return t;
  }
 
  const newtonResult = newtonRaphsonMethod(targetX);
  if (newtonResult !== null) {
    return newtonResult;
  }
 
  return binarySearchFallback(targetX);
}
 
/**
 * Evaluates the Y coordinate of a cubic Bézier curve at parameter t.
 * Optimized for CSS timing functions where P0=(0,0) and P3=(1,1).
 * Uses pre-computed polynomial coefficients and Horner's rule for performance.
 *
 * @param {number} t - Parameter value along the curve, typically in [0, 1]
 * @param {number} y1 - y-coordinate of first control point
 * @param {number} y2 - y-coordinate of second control point
 * @returns {number} The Y value of the curve at parameter t
 */
function sampleCurveY(t: number, y1: number, y2: number): number {
  // Pre-compute polynomial coefficients for performance
  const cy = 3.0 * y1;
  const by = 3.0 * (y2 - y1) - cy;
  const ay = 1.0 - cy - by;
 
  // Evaluate: ay*t³ + by*t² + cy*t using Horner's rule
  return ((ay * t + by) * t + cy) * t;
}
 
/**
 * Cubic Bézier easing function implementation following CSS specifications.
 *
 * A cubic Bézier curve is defined by four points: P0, P1, P2, and P3.
 * In CSS animations, P0 is fixed at (0, 0) and P3 is fixed at (1, 1).
 * This function allows you to specify the intermediate control points P1 and P2.
 *
 * @param {number} x1 - x-coordinate of the first control point P1 (must be in [0, 1])
 * @param {number} y1 - y-coordinate of the first control point P1
 * @param {number} x2 - x-coordinate of the second control point P2 (must be in [0, 1])
 * @param {number} y2 - y-coordinate of the second control point P2
 * @returns {function} A function that takes time t ∈ [0, 1] and returns the eased value
 */
export function cubicBezier(x1: number, y1: number, x2: number, y2: number) {
  Iif (x1 < 0 || x1 > 1 || x2 < 0 || x2 > 1) {
    throw new Error('x1 and x2 must be in the range [0, 1]');
  }
 
  return function (timeProgress: number): number {
    if (timeProgress <= 0) return 0;
    Iif (timeProgress >= 1) return 1;
 
    Iif (x1 === y1 && x2 === y2) {
      return timeProgress;
    }
 
    const curveParameter = solveCubicBezierX(timeProgress, x1, x2);
    return sampleCurveY(curveParameter, y1, y2);
  };
}
 
/** Core Easing Functions */
 
/**
 * Linear easing function - constant speed throughout the animation.
 * Equivalent to CSS: cubic-bezier(0.0, 0.0, 1.0, 1.0)
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @returns {number} The linear eased value.
 */
const linearCore = cubicBezier(0.0, 0.0, 1.0, 1.0);
 
/**
 * Ease-in easing function - starts slow and accelerates.
 * Equivalent to CSS: cubic-bezier(0.42, 0, 1.0, 1.0)
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @returns {number} The eased value.
 */
const easeInCore = cubicBezier(0.42, 0, 1.0, 1.0);
 
/**
 * Ease-out easing function - starts fast and decelerates.
 * Equivalent to CSS: cubic-bezier(0, 0, 0.58, 1.0)
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @returns {number} The eased value.
 */
const easeOutCore = cubicBezier(0, 0, 0.58, 1.0);
 
/**
 * Ease-in-out easing function - starts slow, accelerates, then decelerates.
 * Equivalent to CSS: cubic-bezier(0.42, 0, 0.58, 1.0)
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @returns {number} The eased value.
 */
const easeInOutCore = cubicBezier(0.42, 0, 0.58, 1.0);
 
/**
 * Standard ease easing function (CSS default).
 * Equivalent to CSS: cubic-bezier(0.25, 0.1, 0.25, 1.0)
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @returns {number} The eased value.
 */
const easeCore = cubicBezier(0.25, 0.1, 0.25, 1.0);
 
/** Flexible Easing Function Factory */
 
/**
 * Flexible factory function that creates easing functions with optional baseline and scale support.
 * Provides bell-curve behavior: baseline → baseline*scale → baseline
 *
 * @param {Function} coreEasingFn - The core easing function to wrap
 * @returns {Function} A function that accepts (timeProgress, baseline?, scale?) and provides flexible behavior
 */
function flexibleEasingFunctionFactory(coreEasingFn: (timeProgress: number) => number) {
  return function (timeProgress: number, baseline: number = 0, scale?: number): number {
    Iif (baseline === 0) {
      return coreEasingFn(timeProgress);
    }
 
    const easedProgress = coreEasingFn(timeProgress);
 
    const targetValue = scale ? baseline * scale : 1;
    const range = targetValue - baseline;
 
    // Create bell-curve: baseline → targetValue → baseline
    const bellMultiplier = 1 - Math.abs(2 * easedProgress - 1);
    return baseline + bellMultiplier * range;
  };
}
 
/**
 * Linear easing function with optional baseline and scale support.
 * - No params: standard linear progression [0, 1]
 * - With baseline only: bell-curve baseline → 1 → baseline
 * - With baseline+scale: bell-curve baseline → baseline*scale → baseline
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @param {number} baseline - Optional baseline value (default: 0). Creates bell-curve effect.
 * @param {number} scale - Optional scale multiplier (default: 1). Peak value = baseline * scale.
 * @returns {number} The linear eased value.
 */
export const linear = flexibleEasingFunctionFactory(linearCore);
 
/**
 * Standard ease easing function with optional baseline and scale support.
 * - No params: standard ease progression [0, 1]
 * - With baseline only: bell-curve baseline → 1 → baseline
 * - With baseline+scale: bell-curve baseline → baseline*scale → baseline
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @param {number} baseline - Optional baseline value (default: 0). Creates bell-curve effect.
 * @param {number} scale - Optional scale multiplier (default: 1). Peak value = baseline * scale.
 * @returns {number} The eased value.
 */
export const ease = flexibleEasingFunctionFactory(easeCore);
 
/**
 * Ease-in easing function with optional baseline and scale support.
 * - No params: standard ease-in progression [0, 1]
 * - With baseline only: bell-curve baseline → 1 → baseline
 * - With baseline+scale: bell-curve baseline → baseline*scale → baseline
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @param {number} baseline - Optional baseline value (default: 0). Creates bell-curve effect.
 * @param {number} scale - Optional scale multiplier (default: 1). Peak value = baseline * scale.
 * @returns {number} The eased value.
 */
export const easeIn = flexibleEasingFunctionFactory(easeInCore);
 
/**
 * Ease-out easing function with optional baseline and scale support.
 * - No params: standard ease-out progression [0, 1]
 * - With baseline only: bell-curve baseline → 1 → baseline
 * - With baseline+scale: bell-curve baseline → baseline*scale → baseline
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @param {number} baseline - Optional baseline value (default: 0). Creates bell-curve effect.
 * @param {number} scale - Optional scale multiplier (default: 1). Peak value = baseline * scale.
 * @returns {number} The eased value.
 */
export const easeOut = flexibleEasingFunctionFactory(easeOutCore);
 
/**
 * Ease-in-out easing function with optional baseline and scale support.
 * - No params: standard ease-in-out progression [0, 1]
 * - With baseline only: bell-curve baseline → 1 → baseline
 * - With baseline+scale: bell-curve baseline → baseline*scale → baseline
 *
 * @param {number} timeProgress - The animation progress, in the range [0, 1].
 * @param {number} baseline - Optional baseline value (default: 0). Creates bell-curve effect.
 * @param {number} scale - Optional scale multiplier (default: 1). Peak value = baseline * scale.
 * @returns {number} The eased value.
 */
export const easeInOut = flexibleEasingFunctionFactory(easeInOutCore);
 
/** Export interfaces */
 
export enum EasingFunctionEnum {
  EASE = 'ease',
  EASE_IN = 'ease-in',
  EASE_OUT = 'ease-out',
  EASE_IN_OUT = 'ease-in-out',
  LINEAR = 'linear',
}
 
export const EasingFunctionMap = new Map([
  [EasingFunctionEnum.EASE, ease],
  [EasingFunctionEnum.EASE_IN, easeIn],
  [EasingFunctionEnum.EASE_OUT, easeOut],
  [EasingFunctionEnum.EASE_IN_OUT, easeInOut],
  [EasingFunctionEnum.LINEAR, linear],
]);