@blinkk/degu
    Preparing search index...

    Class RafProgress

    A class that runs Raf for a limited time while a given progress (a value between 0-1) eases. This class is useful because instead of constanlty runnig raf, it will only run RAF when a progress value is unstable. This class also allows you to register and listen for specific progress values and run callbacks (IE: run a callback when the progress crosses 0.5).

    Example WITHOUT RafProgress: Consider the following example to understand what RafProgress does. Here we are using an html range input to represent Progress but progresss could be a window scroll, duration, mouse position etc.....a percentage amount:

    HTML
    <input id="range" type="range" name="points" step="0.01" min="0" max="1" style="width: 500px;">

    JS
    this.range = document.getElementById('range');

    // Set the progress to the range value.
    this.progress = +this.range.value;

    const raf = new Raf(() => {
    // Range is a value between 0 and 1.
    let progress = +this.range.value;

    // Add some ease to the progress to smooth it out.
    this.progress = mathf.ease(this.progress, progress, 0.25, EASE.easeInOutQuad);

    // Reduce the precision of progress. We dont need to report progress differences
    // of 0.0000001.
    this.progress = mathf.round(this.progress, 3);

    // Something with progress
    // Many manipulate the DOM here.
    }).start();

    Here you can see that on each RAF cycle, we check the value of the range and get the progress. We then apply an ease to that progress and then do something within the RAF loop based on the current progress.

    This works but the problem is that RAF is constantly running and even if the value of progress is the same as the last raf loop, we still are running RAf and perhaps layout thrashing or performning unncessary calcs.

    Example WITH RafProgress The above is simplied with this class.


    this.range = document.getElementById('range');

    // Create an instance of raf progress.
    let rafProgress = new RafProgress((easedProgress, direction)=> {
    // DO something here like update the dom.
    // This is ONLY called when progress has changed in value.
    //..
    })

    // Set the initial progress value.
    rafProgress.setCurrentProgress(+this.range.value)

    // Optional, set the precision.
    rafProgress.setPrecision(5);

    // Now listen to the range input 'input' event. The input event is basically
    // fired when the range changes.
    this.range.addEventListener('input', ()=> {
    rafProgress.easeTo(+this.range.value, 0.25, EASE.easeInOutQuad)
    })




    // RafProgress is also an event emitter so you can listen to the progressEvent
    rafProgress.addEventListener('progressChange', (easedProgress, direction)=> {
    // Do something.
    })



    // Or use alias watch, unwatch
    var onProgress = (progress)=> {
    console.log('hello')
    }
    rafProgress.watch(onProgress);
    rafProgress.unwatch(onProgress);

    Progress Callbacks

    Raf progress additionally allows you to register and listen specific progress values and run a callback.


    // Triggers when progress runs from 0.2, 0.4
    rafProgress.watchFor([0.2, 0.4], callback);

    // Triggers when 0.2 is crossed .
    rafProgress.watchFor(0.2, callback);

    // Remove watchFor
    rafProgress.unwatchFor(callback);
    Index
    currentProgress: number
    • Similar to easeTo but applied a smoothdamp instead.

      Parameters

      • targetProgress: number
      • easeAmount: number
      • damp: number
      • noClamp: boolean = false

      Returns void

    • Eases the progress to a target value. Until that value is reached, the progressRafLoop is called.

      Parameters

      • targetProgress: number

        The progress to get to.

      • easeAmount: number

        The amount to ease. This gets calculated as per RAF, how much of the difference between the current value and target should the current value be updated by. Therefore, 1 would mean no ease. 0.1 would mean a lot of ease.

      • easingFunction: Function = EASE.linear

        An optional easing funciton. Defaults to linear.

      • noClamp: boolean = false

        Prevent progress clamping. Allows values outside range of 0-1.

      Returns void

    • Gets the lerp delta - the difference between the current ease and the targetEase (where it should be if it caught up).

      Returns number

    • Gets the current scroll direction. 1 is down scroll, -1 is up scroll and 0 is no scroll (when progress is catching up).

      Returns number

    • Run calculations. Normally. you would call easeTo to set the latest current progress, you may want to call this if forced stopped and want to restart the raf loop manually.

      Returns void

    • Sets the current progress. This forces an immediate update to the passed progress.

      Parameters

      • progress: number
      • noClamp: boolean = true

      Returns void

    • Sets the FPS of the internal raf loop.

      Parameters

      • fps: number

      Returns void

    • Sets the precision. Precision is used to check how closely the current progress is versus the previous progress per RAF cycle. The lower the number, the less precise.

      Use a lower number if you want want to improve performance since it will result in calling Raf fewer times.

      Parameters

      • value: number

      Returns void

    • Removes a progress listener.

      Parameters

      • callbackToRemove: Function

      Returns void

    • Removes a given

      Parameters

      • callback: Function

      Returns void

    • Adds a progress listener.

      Parameters

      • callback: Function

      Returns void

    • Sets a callback for a specific range.

      Parameters

      • range: number | number[]
      • callback: Function

      Returns void