Module

Gesso.Time

Package
purescript-gesso
Repository
smilack/purescript-gesso

Timestamps, time deltas, intervals, and requestAnimationFrame

#Delta Source

type Delta = { delta :: Number, last :: Number, now :: Number }

A current time, a previous time, and the difference between them. All values are in milliseconds.

For per-frame updates and the rendering function, last is the time of the previous animation frame and now is the time of the current animation frame.

For fixed-rate updates, last is the scheduled time of the previous fixed-rate update and now is the update's scheduled time.

For interaction events and Halogen queries, last is the time of the previous animation frame and now is the event's arrival time.

#Interval Source

data Interval

A interval of time, in milliseconds, to space out repeating an action, or Never perform the action. Constructed with hz or never.

#Last Source

newtype Last

A time in the past, in milliseconds.

#Now Source

newtype Now

The current time in milliseconds, or the time at which a value became Stamped.

#RequestAnimationFrameId Source

newtype RequestAnimationFrameId

A RequestAnimationFrameId is returned when calling requestAnimationFrame. This can be used to cancel the request.

#Stamped Source

type Stamped a = { item :: a, time :: Number }

An item and a specific time associated with that item. Used for comparing timestamped values produced by stamp and stampInterval.

#StampedBatch Source

type StampedBatch a = { items :: List (Stamped a), last :: Last }

Results of repeatedly timestamping a function at certain interval:

  • last: the timestamp of the latest item in the list
  • items: a list of repeated applications of Deltas to the function, with timestamps

#cancelAnimationFrame Source

cancelAnimationFrame :: RequestAnimationFrameId -> Window -> Effect Unit

Cancel a request for an animation frame using the RequestAnimationFrameId returned by requestAnimationFrame.

#delta Source

delta :: Now -> Last -> Delta

Create a Delta from a current time and a previous time.

#elapse Source

elapse :: Now -> Last

Convert a current time into a previous time.

#hz Source

hz :: Number -> Interval

Construct an Interval from Hz or frames per second. Invalid frequencies (±Infinity, NaN, zero, or negative) result in a Never interval.

#never Source

never :: Interval

An interval that never occurs.

#requestAnimationFrame Source

requestAnimationFrame :: (Now -> Effect Unit) -> Window -> Effect RequestAnimationFrameId

An interface to window.requestAnimationFrame which passes the timestamp argument to the callback function.

Provided because requestAnimationFrame in Web.HTML.Window only accepts an Effect Unit instead of a Number -> Effect Unit.

#sort Source

sort :: forall a. List (Stamped a) -> List (Stamped a)

Sort a list of Stamped records ascending chronologically.

#stamp Source

stamp :: forall a. Last -> (Delta -> a) -> Effect (Stamped a)

For a last timestamp and a function f :: Delta -> a (such as Gesso.Application.Behavior.UpdateFunction, create a Delta between last and the current time, apply the delta to f, and return a record containing the current time and the result of f delta.

#stampInterval Source

stampInterval :: forall a. Last -> (Delta -> a) -> Interval -> Effect (StampedBatch a)

Repeatedly timestamp a function at a given interval, starting at the last time plus the interval, as long as the timestamp does not pass the current time.

The last value in the returned record should be saved by the caller to pass in as the last argument for the next call to stampInterval.

#started Source

started :: Effect Last

Get a single Last value at the current time, useful for starting a timer.

#toRatio Source

toRatio :: Last -> Interval -> Now -> Number

Convert the span between two periods of time to a unitless number of intervals, typically between 0.0 and 1.0. The result is used with linear interpolation to find a weighted average between two values.

For a Never interval, returns 1, signalling to use the newer value and ignore the older.