All files / src/dom lottie-progress-player.ts

0% Statements 0/31
0% Branches 0/6
0% Functions 0/13
0% Lines 0/31

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                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import lottie, {type AnimationItem} from 'lottie-web';
import * as func from '../func/func';
import * as mathf from '../mathf/mathf';
import {
  type EventCallback,
  type EventDispatcher,
  EventManager,
} from '../ui/events';
import {DomWatcher} from './dom-watcher';
 
export enum LottieProgressPlayerEvents {
  RESIZE = 'resize',
  INIT = 'init',
  DOMLOADED = 'DOMLoaded',
}
 
export interface LottieProgressPlayerSettings {
  // The json path to the lottie json file.
  jsonPath: string | null;
 
  // // If images are not embedded in the json file, the image path to the image directory.
  imagePath?: string | null;
 
  // The aspect ratio sizing settings to use for lottie. // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
  // Defaults to xMidYMid slice.
  preserveAspectRatio?: string;
 
  // The renderer to use 'canvas', 'svg'
  renderer?: string;
 
  // The start and end frame of the lottie
  // If you don't know this, drag your lottie file to https://lottie-tester-dot-googwebreview.appspot.com/
  // and check the console.  It will display the frame.
  startFrame: number;
  endFrame: number;
}
 
/**
 * Lottie progress player class.
 *
 *
 * The lottie progress player is designed to play a lottie based on progress
 * or frame.
 *
 * ```
 * const lottieProgressPlayer = new LottieProgressPlayer(myContainerElementToAddLottie,
 * {
 *    jsonPath: .. // Path to lottie
 *    startFrame: 0,
 *    endFrame: 0,
 * }
 * )
 *
 *
 * // Update the progress to 0.5.
 * lottieProgressPlayer.setProgress(0.5);
 *
 * // Update the lottie to frame 30
 * lottieProgressPlayer.setFrame(0.5);
 * ```
 *
 * The lottie progress player can go well with scrollProgress or
 * rafTimer classes.
 *
 * If you want to autoplay your lottie.
 *
 * ```
 * let rafTimer = new RafTimer((progress)=> {
 *  lottieProgressPlayer.setProgress(progress)
 * });
 * rafTimer.setDuration(300);
 * rafTimer.play();
 * ```
 *
 * Or you could setup scroll progress and update it as you need.
 *
 * ```
 * const scrollProgress = new ScrollProgress(element);
 * // Add your settings.
 * const settings = {
 *    debug: false,
 *    top: '0px'
 *    bottom: '10px'
 * }
 * scrollProgress.init(settings);
 *
 * scrollProgress.on(ScrollProgressEvents.RAF, ()=> {
 *   lottieProgressPlayer.setProgress(
 *      this.scrollProgress.getProgress();
 *   )
 * });
 * ```
 *
 * # Lottie player events.
 *
 * If you need to hook into lottie events, these are available on lottie progress
 * player.
 *
 *
 * Example: Immediately update the lottie-progress-player to the last frame on
 * load.
 * ```
 *   this.lottieProgressPlayer.on(LottieProgressPlayerEvents.DOMLOADED, ()=> {
 *       this.lottieProgressPlayer.setProgress(1);
 *    })
 * ```
 */
export class LottieProgressPlayer implements EventDispatcher {
  private containerElement: HTMLElement;
  private domWatcher: DomWatcher;
  private eventManager: EventManager;
  private settings: LottieProgressPlayerSettings;
  private lottieInstance: AnimationItem | null = null;
  private currentProgress = 0;
 
  constructor(
    containerElement: HTMLElement,
    settings: LottieProgressPlayerSettings
  ) {
    this.eventManager = new EventManager();
    this.containerElement = containerElement;
    this.domWatcher = new DomWatcher();
 
    this.settings = {
      ...{
        jsonPath: null,
        renderer: 'canvas',
        preserveAspectRatio: 'xMidYMid slice',
        imagePath: null,
      },
      ...settings,
    };
 
    this.domWatcher.add({
      element: window,
      on: 'smartResize',
      callback: func.debounce(() => {
        this.onWindowResize();
      }, 300),
    });
 
    this.createLottie();
 
    this.eventManager.dispatch(LottieProgressPlayerEvents.INIT);
  }
 
  public createLottie() {
    const lottieSettings = {
      loop: true,
      autoplay: false,
      path: this.settings.jsonPath,
      assetsPath: this.settings.imagePath,
      renderer: this.settings.renderer,
      rendererSettings: {
        // https://github.com/airbnb/lottie-web/issues/1860
        // https://github.com/airbnb/lottie-web/wiki/Renderer-Settings
        // For svg.
        // progressiveLoad: true,
        preserveAspectRatio: this.settings.preserveAspectRatio,
      },
      container: this.containerElement,
    };
 
    this.lottieInstance = <AnimationItem>(
      lottie.loadAnimation(lottieSettings as any)
    );
 
    // Supposed lottie optimization.
    this.lottieInstance.setSubframe(false);
    this.lottieInstance.addEventListener('DOMLoaded', () => {
      this.setProgress(0);
      this.eventManager.dispatch(LottieProgressPlayerEvents.DOMLOADED);
    });
  }
 
  /**
   * Update the lottie by progress
   */
  public setProgress(progress: number) {
    this.currentProgress = progress;
    const frame = mathf.lerp(
      this.settings.startFrame,
      this.settings.endFrame,
      progress
    );
    this.setFrame(frame);
  }
 
  /**
   * Update the lottie to a specific frame.
   */
  public setFrame(frame: number) {
    this.lottieInstance && this.lottieInstance['goToAndStop'](frame, true);
  }
 
  public getLottieInstance() {
    return this.lottieInstance;
  }
 
  public getProgress() {
    return this.currentProgress;
  }
 
  public on(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  public off(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  protected onWindowResize() {
    this.lottieInstance && this.lottieInstance.resize();
    this.eventManager.dispatch(LottieProgressPlayerEvents.RESIZE);
  }
 
  public dispose(): void {
    this.domWatcher && this.domWatcher.dispose();
  }
}