@blinkk/degu
    Preparing search index...

    Class Raf

    A class that creates a RAF loop and calls a specific callback. Setting the frame rate will throttle the animation.

    Example usage;

    var raf = new Raf((frame, lastUpdateTime, stop)=> {
    console.log('this runs on request animation frame');
    // The current FPS.
    console.log(raf.getCurrentFps());
    });
    raf.start();

    // Later to stop raf.
    raf.stop();

    // Set the FPS
    raf.setFps(30);
    raf.start();


    // Add or remove listeners
    var onRaf = ()=> {
    console.log('hello')
    }
    raf.watch(onRaf);
    raf.unwatch(onRaf);

    // Clock functions: Delta and elapsed time.
    var raf2 = new Raf((frame, lastUpdateTime, stop)=> {
    // Gets the time when raf started.
    const startTime = raf.getStartTime();
    // Gets the delta between the last update
    const delta = raf.getDelta();
    // Time elapsed
    const elapsedTime = raf.getElapsedTime();
    }).start();

    RunWhen option. You can pass a run condition to limit raf only when the condition resolve to true. This is useful to cull unncessary requests. An example is to use elementVisility to only run RAF when an element is in view and stop raf when it goes out of view.


    // Runs only when the window is small.
    var raf = new Raf(()=> {
    console.log('this runs when scren size is less than 1000');
    });
    raf.runWhen(()=> { window.innerWidth < 1000});
    raf.start();



    // Runs only when myElement is in view.
    // Option 1: You can use runWhen condition


    let ev = elementVisibility.inview(myElement);
    var raf = new Raf(() => {
    console.log('a raf that runs when element is in view.');
    });
    raf.runWhen(() => {
    return ev.state().inview;
    });

    // This normally works fine but elementVisibility has a split second to
    // boot up.
    raf.start();

    // if you want to be sure to only run RAF when ev is ready do this:
    ev.readyPromise.then(()=> {
    raf.start();
    })

    // Runs only when myElement is in view. // Option 2: You can use runWhenElementIsInview option.

    var raf = new Raf(()=> {
    ...
    })


    // Set element and run when it's ready.
    raf.runWhenElementIsInview(
    document.getElementById("myelement"),
    {
    rootMargin: '500px 0px 500px 0px'
    }
    ).then(()=> {
    raf.start();
    })

    Batch read / write.

    Batch write / read. To batch write and read, wrap your methods in read and write calls. Read / Write calls uses the degu raf registry so your reads or writes can get delayed by one raf cycle.

    var raf = new Raf(()=> {
    raf.read(()=> {
    this.height = element.offsetHeight;
    })

    raf.write(()=> {
    element.style.height = this.height + 20 + 'px';
    })
    })

    You can also create a read / write only raf by passing no rafLoop to the constructor.

    var raf = new Raf();
    raf.read(()=> {
    this.height = element.offsetHeight;
    })

    raf.write(()=> {
    element.style.height = this.height + 20 + 'px';
    })

    If you want read / writes to NOT cull, you can set the read write mode. This is needed if you pass a rafLoop to the constructor but additionally, want to use the read write outside your loop even when your raf is stopped.

    var raf = new Raf(()=> {
    raf.read(()=> {
    this.height = element.offsetHeight;
    })

    raf.write(()=> {
    element.style.height = this.height + 20 + 'px';
    })
    })

    // Raf is stopped.
    raf.stop();

    // This won't work.
    raf.write(()=> {
    ...
    })


    raf.setReadWriteMode(true);
    // Now it will work even when raf is stopped.
    raf.write(()=> {
    ...
    })
    Index
    • Parameters

      • OptionalrafLoop: Function

        Optional function to be called on each request animation frame.

      Returns Raf

    isDisposed: boolean = false
    isPlaying: boolean
    isReadWriteOnly: boolean = false
    • Gets the current frame rate that raf is running at. Useful for debugging.

      Returns number

    • Gets the delta in ms between the last executed raf update.

      Parameters

      • inSeconds: boolean

        Whether to acquire the delta time in seconds. Defaults to ms.

      Returns number

    • Gets the elasped time since raf started.

      Returns number

    • Gets the time (Date) when raf started.

      Returns number

    • Adds a one time post write callback executed by the global degu raf registry. Returns a Promise resolving to the callback's return value.

      Type Parameters

      • T = void

      Parameters

      • callback: () => T

      Returns Promise<T>

    • Adds a one time pre-read callback executed by the global degu raf registry. Returns a Promise resolving to the callback's return value.

      Type Parameters

      • T = void

      Parameters

      • callback: () => T

      Returns Promise<T>

    • Adds a one time read callback executed by the global degu raf registry. This allows you to batch read calls and optionally await their result.

      Type Parameters

      • T = void

      Parameters

      • callback: () => T

      Returns Promise<T>

    • Sets a function to execute on each raf loop. If the condition resolves to true, the raf loop callbacks will be executed. If false, the raf loop is culled.

      Parameters

      • callbackCondition: Function

      Returns void

    • Allows you to pass an option to tell this raf to execute only when the given element is in the viewport. Optionally pass intersection observer options.

      Note that this still requires you to start the raf. You can do this with the promise that the method returns (resolved when ev is ready) or at a later time.

      Note it's recommended that you add a rootMargin to your ev settings if need to do offscreen processing.

      var raf = new Raf(()=> {
      ...
      })


      // Set element and run when it's ready.
      raf.runWhenElementIsInview(
      document.getElementById("myelement"),
      {
      rootMargin: '300px 0px 300px 0px'
      }
      ).then(()=> {
      raf.start();
      })

      Parameters

      • element: HTMLElement
      • OptionalintersectionObserverOptions: Object

      Returns Promise<void>

    • Sets the fps .

      Parameters

      • fps: number

      Returns void

    • Sets the read write mode.

      Parameters

      • value: boolean

      Returns void

    • Starts the RAF animation loop.

      Parameters

      • force: boolean = false

      Returns void

    • Stops the RAF animation loop.

      Returns void

    • Removes a progress listener.

      Parameters

      • callbackToRemove: Function

      Returns void

    • Returns Promise<void>

    • Returns Promise<void>

    • Adds a raf listener

      Parameters

      • callback: Function

      Returns void

    • Adds a one time write callback executed by the global degu raf registry. This allows you to batch write calls and optionally await completion.

      Type Parameters

      • T = void

      Parameters

      • callback: () => T

      Returns Promise<T>