Clock

View source

Tracks elapsed time and the duration of the most recent update.

Supply timestamps to Clock#update to record a duration in seconds and accumulate elapsed seconds. Use Clock#start to reset the accumulated values when a new timing interval begins. For a shared application clock, use VirtualClock.

Examples

import { Clock } from '@wimaengine/time'

const clock = new Clock()

clock.update(1000)
console.log(clock.getDelta()) // 1 second

clock.update(1016)
console.log(clock.getDelta()) // 0.016 seconds
console.log(clock.getElapsed()) // 1.016 seconds
console.log(clock.getFrameRate()) // 62.5 frames per second

clock.start()
console.log(clock.getElapsed()) // 0

Constructor


constructor()

Methods


start(): void

View source

Resets the elapsed time and current frame delta to zero.

The last update timestamp is preserved. The next call to Clock#update therefore measures from the timestamp before the reset.

getElapsed(): number

View source

Returns the total time accumulated by the clock, in seconds.

Returns

Elapsed time in seconds.

getDelta(): number

View source

Returns the duration calculated by the most recent update, in seconds.

This is 0 before the first update and after Clock#start.

Returns

The most recent frame delta in seconds.

getFrameRate(): number

View source

Returns the instantaneous frame rate derived from the current delta.

The value is calculated as 1 / delta; it is not averaged or smoothed. When the delta is 0, JavaScript returns Infinity.

Returns

Frames per second for the most recent delta.

update(accumulate?: number): number

View source

Advances the clock using an absolute timestamp.

The timestamp is expected in milliseconds, matching performance.now(). The resulting delta and the accumulated elapsed time are in seconds. With no argument, the method uses performance.now().

Parameters

NameDescriptionDefault
accumulate?

Timestamp in milliseconds. Defaults to performance.now().

...

Returns

The duration since the previous update, in seconds.

staticstart(clock: Clock): void

View source

Resets a clock’s elapsed time and current frame delta to zero.

lastTick is intentionally left unchanged, so the next update measures from the timestamp that was recorded before the reset.

Parameters

NameDescriptionDefault
clock

Clock to reset.

—

staticgetElapsed(clock: Clock): number

View source

Gets the total elapsed time of a clock, in seconds.

Parameters

NameDescriptionDefault
clock

Clock to read.

—

Returns

Elapsed time in seconds.

staticgetDelta(clock: Clock): number

View source

Gets the duration calculated by the most recent update, in seconds.

Parameters

NameDescriptionDefault
clock

Clock to read.

—

Returns

The most recent frame delta in seconds.

staticgetFrameRate(clock: Clock): number

View source

Gets the instantaneous frame rate derived from the current delta.

The result is 1 / clock.delta; a zero delta therefore produces JavaScript’s Infinity rather than a finite or averaged frame rate.

Parameters

NameDescriptionDefault
clock

Clock to read.

—

Returns

Frames per second for the most recent delta.

staticupdate(clock: Clock, accumulate?: number): number

View source

Advances a clock using an absolute timestamp in milliseconds.

The difference between the supplied timestamp and the previous lastTick is divided by 1000 to produce a delta in seconds. That delta is added to elapsed, then the supplied timestamp is stored as lastTick. When omitted, the timestamp defaults to performance.now().

Parameters

NameDescriptionDefault
clock

Clock to advance.

—
accumulate?

Timestamp in milliseconds. Defaults to performance.now().

...

Returns

The duration since the previous update, in seconds.

staticserialize(value: Clock): ClockSerial

View source

Converts a clock into the plain state used by the type registry.

The returned object contains the elapsed time and delta in seconds, plus the most recent timestamp in milliseconds.

Parameters

NameDescriptionDefault
value

Clock to serialize.

—

Returns

The ClockSerial state.

staticdeserialize(value: ClockSerial, out?: Clock): Clock

View source

Restores clock state from a serialized value.

When out is supplied, it is updated and returned. Otherwise a new Clock is created. Deserialization restores elapsed, lastTick, and delta exactly; it does not advance the clock or read the current time.

Parameters

NameDescriptionDefault
value

ClockSerial state.

—
out?

Clock to populate.

...

Returns

The restored Clock.