{"version":3,"file":"AnimationBase.min.mjs","sources":["../../../../src/util/animation/AnimationBase.ts"],"sourcesContent":["import { noop } from '../../constants';\nimport { requestAnimFrame } from './AnimationFrameProvider';\nimport { runningAnimations } from './AnimationRegistry';\nimport { defaultEasing } from './easing';\nimport type {\n  AnimationState,\n  TAbortCallback,\n  TBaseAnimationOptions,\n  TEasingFunction,\n  TOnAnimationChangeCallback,\n} from './types';\n\nconst defaultAbort = () => false;\n\nexport abstract class AnimationBase<\n  T extends number | number[] = number | number[],\n> {\n  declare readonly startValue: T;\n  declare readonly endValue: T;\n  declare readonly duration: number;\n  declare readonly delay: number;\n\n  protected declare readonly byValue: T;\n  protected declare readonly easing: TEasingFunction<T>;\n\n  private declare readonly _onStart: VoidFunction;\n  private declare readonly _onChange: TOnAnimationChangeCallback<T, void>;\n  private declare readonly _onComplete: TOnAnimationChangeCallback<T, void>;\n  private declare readonly _abort: TAbortCallback<T>;\n\n  /**\n   * Used to register the animation to a target object\n   * so that it can be cancelled within the object context\n   */\n  declare readonly target?: unknown;\n\n  private _state: AnimationState = 'pending';\n  /**\n   * Time %, or the ratio of `timeElapsed / duration`\n   * @see tick\n   */\n  durationProgress = 0;\n  /**\n   * Value %, or the ratio of `(currentValue - startValue) / (endValue - startValue)`\n   */\n  valueProgress = 0;\n  /**\n   * Current value\n   */\n  declare value: T;\n  /**\n   * Animation start time ms\n   */\n  private declare startTime: number;\n\n  constructor({\n    startValue,\n    byValue,\n    duration = 500,\n    delay = 0,\n    easing = defaultEasing,\n    onStart = noop,\n    onChange = noop,\n    onComplete = noop,\n    abort = defaultAbort,\n    target,\n  }: TBaseAnimationOptions<T>) {\n    this.tick = this.tick.bind(this);\n\n    this.duration = duration;\n    this.delay = delay;\n    this.easing = easing;\n    this._onStart = onStart;\n    this._onChange = onChange;\n    this._onComplete = onComplete;\n    this._abort = abort;\n    this.target = target;\n\n    this.startValue = startValue;\n    this.byValue = byValue;\n    this.value = this.startValue;\n    this.endValue = Object.freeze(this.calculate(this.duration).value);\n  }\n\n  get state() {\n    return this._state;\n  }\n\n  isDone() {\n    return this._state === 'aborted' || this._state === 'completed';\n  }\n\n  /**\n   * Calculate the current value based on the easing parameters\n   * @param timeElapsed in ms\n   * @protected\n   */\n  protected abstract calculate(timeElapsed: number): {\n    value: T;\n    valueProgress: number;\n  };\n\n  start() {\n    const firstTick: FrameRequestCallback = (timestamp) => {\n      if (this._state !== 'pending') return;\n      this.startTime = timestamp || +new Date();\n      this._state = 'running';\n      this._onStart();\n      this.tick(this.startTime);\n    };\n\n    this.register();\n\n    // setTimeout(cb, 0) will run cb on the next frame, causing a delay\n    // we don't want that\n    if (this.delay > 0) {\n      setTimeout(() => requestAnimFrame(firstTick), this.delay);\n    } else {\n      requestAnimFrame(firstTick);\n    }\n  }\n\n  private tick(t: number) {\n    const durationMs = (t || +new Date()) - this.startTime;\n    const boundDurationMs = Math.min(durationMs, this.duration);\n    this.durationProgress = boundDurationMs / this.duration;\n    const { value, valueProgress } = this.calculate(boundDurationMs);\n    this.value = Object.freeze(value);\n    this.valueProgress = valueProgress;\n\n    if (this._state === 'aborted') {\n      return;\n    } else if (\n      this._abort(this.value, this.valueProgress, this.durationProgress)\n    ) {\n      this._state = 'aborted';\n      this.unregister();\n    } else if (durationMs >= this.duration) {\n      this.durationProgress = this.valueProgress = 1;\n      this._onChange(this.endValue, this.valueProgress, this.durationProgress);\n      this._state = 'completed';\n      this._onComplete(\n        this.endValue,\n        this.valueProgress,\n        this.durationProgress,\n      );\n      this.unregister();\n    } else {\n      this._onChange(this.value, this.valueProgress, this.durationProgress);\n      requestAnimFrame(this.tick);\n    }\n  }\n\n  private register() {\n    runningAnimations.push(this as unknown as AnimationBase);\n  }\n\n  private unregister() {\n    runningAnimations.remove(this as unknown as AnimationBase);\n  }\n\n  abort() {\n    this._state = 'aborted';\n    this.unregister();\n  }\n}\n"],"names":["defaultAbort","AnimationBase","constructor","_ref","startValue","byValue","duration","delay","easing","defaultEasing","onStart","noop","onChange","onComplete","abort","target","_defineProperty","this","tick","bind","_onStart","_onChange","_onComplete","_abort","value","endValue","Object","freeze","calculate","state","_state","isDone","start","firstTick","timestamp","startTime","Date","register","setTimeout","requestAnimFrame","t","durationMs","boundDurationMs","Math","min","durationProgress","valueProgress","unregister","runningAnimations","push","remove"],"mappings":"yTAYA,MAAMA,EAAeA,KAAM,EAEpB,MAAeC,EAyCpBC,WAAAA,CAAWC,GAWkB,IAXjBC,WACVA,EAAUC,QACVA,EAAOC,SACPA,EAAW,IAAGC,MACdA,EAAQ,EAACC,OACTA,EAASC,EAAaC,QACtBA,EAAUC,EAAIC,SACdA,EAAWD,EAAIE,WACfA,EAAaF,EAAIG,MACjBA,EAAQd,EAAYe,OACpBA,GACyBZ,EApC3Ba,gBAMiC,WACjCA,0BAImB,GACnBA,uBAGgB,GAsBdC,KAAKC,KAAOD,KAAKC,KAAKC,KAAKF,MAE3BA,KAAKX,SAAWA,EAChBW,KAAKV,MAAQA,EACbU,KAAKT,OAASA,EACdS,KAAKG,SAAWV,EAChBO,KAAKI,UAAYT,EACjBK,KAAKK,YAAcT,EACnBI,KAAKM,OAAST,EACdG,KAAKF,OAASA,EAEdE,KAAKb,WAAaA,EAClBa,KAAKZ,QAAUA,EACfY,KAAKO,MAAQP,KAAKb,WAClBa,KAAKQ,SAAWC,OAAOC,OAAOV,KAAKW,UAAUX,KAAKX,UAAUkB,MAC9D,CAEA,SAAIK,GACF,OAAOZ,KAAKa,MACd,CAEAC,MAAAA,GACE,MAAuB,YAAhBd,KAAKa,QAAwC,cAAhBb,KAAKa,MAC3C,CAYAE,KAAAA,GACE,MAAMC,EAAmCC,IACnB,YAAhBjB,KAAKa,SACTb,KAAKkB,UAAYD,IAAc,IAAIE,KACnCnB,KAAKa,OAAS,UACdb,KAAKG,WACLH,KAAKC,KAAKD,KAAKkB,WAAU,EAG3BlB,KAAKoB,WAIDpB,KAAKV,MAAQ,EACf+B,YAAW,IAAMC,EAAiBN,IAAYhB,KAAKV,OAEnDgC,EA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