Skip to content

Module: lib/util/Looper

Provides a robust, stoppable animation loop based on requestAnimationFrame. Ensures that only one loop instance is running at any given time. Can run on requestAnimationFrame (default) or at a custom interval. Supports an optional frame limit for one-shot animations.

Version
1.0.0
Author
Frank Kudermann - alphanull
License
MIT
Source
Looper.js, line 11

Constructor

new Looper(renderFn, intervalopt)

Creates a new RAFLoop instance.

Parameters:
Name Type Attributes Description
renderFn function

The function to execute on each frame/tick. Must not be null.

interval number optional

Optional: update interval in milliseconds. If omitted or falsy, uses requestAnimationFrame.

Throws

If renderFn is not a function.

Type TypeError

Members

The user-defined render function to be called on each frame/tick.

Type
function
Source
Looper.js, line 16

Current timer ID (RAF or interval), or null if stopped.

Type
number | null
Source
Looper.js, line 22

The update interval in milliseconds. If null, uses requestAnimationFrame.

Type
number | null
Source
Looper.js, line 28

The maximum number of frames/ticks to run, or null for unlimited.

Type
number | null
Source
Looper.js, line 34

Current tick/frame count for a limited run.

Type
number
Source
Looper.js, line 40

Number of frames/ticks since the last FPS calculation.

Type
number
Source
Looper.js, line 46

Timestamp of the last FPS calculation.

Type
number
Source
Looper.js, line 52

Most recent calculated FPS value.

Type
number
Source
Looper.js, line 58

Methods

start(limitopt)

Starts the loop if not already running. Optionally limits the number of ticks/frames. If called again while running, changes the tick limit or makes the loop unlimited.

Parameters:
Name Type Attributes Description
limit number optional

Optional maximum number of ticks; unlimited if omitted or invalid.

Source
Looper.js, line 80

stop()

Stops the loop if running.

Source
Looper.js, line 110

isRunning() → boolean

Returns whether the loop is currently running.

Returns

True if the loop is active, false otherwise.

Type boolean
Source
Looper.js, line 129

getFPS() → number

Returns the currently measured frames/ticks per second (FPS/TPS).

Returns

The most recent FPS/TPS value, updated once per second.

Type number
Source
Looper.js, line 139

private #tickRAF()

Internal tick method for RAF mode.

Source
Looper.js, line 149

private #handleTick()

Internal tick logic shared by RAF and interval. Increments tick counter, handles limit and FPS calculation.

Source
Looper.js, line 161

destroy()

Performs full cleanup by stopping the loop and clearing the render function. Should be called when the instance is no longer needed.

Source
Looper.js, line 189