All files / src/time fps.ts

0% Statements 0/28
0% Branches 0/12
0% Functions 0/7
0% Lines 0/28

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                                                                                                                                                                                                                                                                                                                                                                                     
import * as time from '../time/time';
 
/**
 * A class that helps with time with fps.
 *
 *
 * ```ts
 *
 * class myClass {
 *    contructor() {
 *       this.fps = new Fps(30);
 *       draw();
 *    }
 *
 *    draw() {
 *        // Keep loop going.
 *        requestAnimationFrame(draw);
 *
 *        // Can run would return true when it is okay to execute limiting
 *        // the calls to 30 FPS.
 *        if(this.fps.canRun()) {
 *          // Do some expensive computation.
 *          canvas.drawImage(hugeImage);
 *        }
 *
 *    }
 * }
 * ```
 *
 * The above works but it is a simple gating a call to run at a set rate.
 * Here is a slightly more complex usage where you can "schedule" the last call.
 *
 * This will also draw at 30FPS but the difference is that it is debounced
 * so your very last call to draw is guaranteed to run when the drawing goes idle.
 *
 * ```ts
 *
 * class myClass {
 *    contructor() {
 *       this.fps = new Fps(30);
 *       this.index = 0;
 *       draw(1);
 *       draw(2);
 *       draw(3);  // This last one gets executed in this example.
 *    }
 *
 *    draw(index) {
 *        // If this call came too quickly between 30FPS, schedule it to run.
 *        if(!this.fps.canRun()) {
 *          this.fps.schedule(()=> {
 *             this.draw(index);
 *          });
 *          return;
 *        }
 *
 *        // Do something expensive here.
 *    }
 * }
 * ```
 *
 *
 *
 */
export class Fps {
  /**
   * An fps rate limiter.
   */
  private fps: number;
 
  /**
   * Whether the fps is in a locked state.  A locked state
   * represents that the fps should be limited.  Defaults to
   * true.
   */
  private locked: boolean;
 
  /**
   * The time measurement used to measure internal fps.
   */
  private lastUpdateTime = 0;
 
  /**
   * A timeout that is used for scheduling.
   */
  private scheduleTimeout = 0;
 
  constructor(fps: number) {
    this.fps = fps;
    this.locked = true;
  }
 
  /**
   * Sets the fps.
   * @param fps
   */
  setFps(fps: number) {
    this.fps = fps;
  }
 
  /**
   * Locks or Unlocks fps.  This forces canRun to always return true.
   * This is useful in cases you need to temporarily remove
   * FPS throttling.
   *
   * ```ts
   *
   * // On Window resize we want to run the drawLoop regardless
   * // of FPS.  We can temporarily tell fps to be unlocked.
   * resize() {
   *    this.fps.lock(false);
   *    this.drawLoop();
   *    this.fps.lock(true);
   * }
   *
   *
   * // Runs on RAF
   * drawLoop() {
   *  if(this.fps.canRun()) {
   *     // Do some expensive computation.
   *     canvas.drawImage(hugeImage);
   *  }
   * }
   *
   * ```
   */
  lock(lock: boolean) {
    this.locked = lock;
  }
 
  /**
   * Schedules a callback to force run after a set period.
   */
  schedule(callback: Function) {
    this.cancelSchedule();
    this.scheduleTimeout = window.setTimeout(
      () => {
        this.locked = false;
        callback();
        this.locked = true;
      },
      1000 / this.fps + 1
    );
  }
 
  /**
   * Clears the schdule timeout.
   */
  cancelSchedule() {
    if (this.scheduleTimeout) {
      window.clearTimeout(this.scheduleTimeout);
    }
  }
 
  /**
   * Checks the lastUpdateTime and checks if it is within the threshold
   * of being allowed to run.  This method would return true if it's okay
   * to run but return false a call should be culled.
   */
  canRun(): boolean {
    this.cancelSchedule();
 
    // If the FPS is unlocked always return true.
    if (!this.locked) {
      return true;
    }
 
    if (this.lastUpdateTime) {
      const current = time.now();
      const elapsed = current - this.lastUpdateTime;
      const fps = this.fps === 0 ? 0 : 1000 / this.fps;
      if (elapsed > fps) {
        this.lastUpdateTime = time.now();
        return true;
      } else {
        return false;
      }
    }
 
    if (!this.lastUpdateTime) {
      this.lastUpdateTime = time.now();
      return true;
    }
 
    return false;
  }
}