This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
README

Clock

Clock is designed to work with elm-lang/animation-frame. Your model will get consistently-paced updates, despite fluctuations in frame diffs.

type Clock = Clock { lag : Time , period : Time }

A clock.

withPeriod : Time -> Clock

Create a clock that updates with the given real-time period.

withDelta (100 * Time.millisecond) -- calls the tick function ten times per second
lag : Clock -> Time

Return the amount of lag between the animation frame and the last physics update.

update : (Time -> a -> a) -> Time -> Clock -> a -> ( Clock, a )

Called like so:

update up diff clock model

The diff is a real-time diff, such as what is given by AnimationFrame.diffs. This function will pass the diff to the clock. If the diff causes the clock's counter to increment, then up will be called with the period and the model.

module Clock exposing (Clock, lag, withPeriod, update)

{-| Clock is designed to work with [elm-lang/animation-frame](package.elm-lang.org/packages/elm-lang/animation-frame/latest/AnimationFrame). Your model will get consistently-paced updates, despite fluctuations in frame diffs.

@docs Clock, withPeriod, lag, update
-}

import Time exposing (Time)


{-| A clock.
-}
type Clock
    = Clock
        { lag : Time
        , period : Time
        }


{-| Create a clock that updates with the given real-time period.

    withDelta (100 * Time.millisecond) -- calls the tick function ten times per second
-}
withPeriod : Time -> Clock
withPeriod period =
    Clock
        { lag = 0
        , period = period
        }


{-| Return the amount of lag between the animation frame and the last physics update.
-}
lag : Clock -> Time
lag (Clock clock) =
    clock.lag


{-| Called like so:

    update up diff clock model

The diff is a real-time diff, such as what is given by AnimationFrame.diffs. This function will pass the diff to the clock. If the diff causes the clock's counter to increment, then `up` will be called with the period and the model.
-}
update : (Time -> a -> a) -> Time -> Clock -> a -> ( Clock, a )
update up dt (Clock clock) model =
    let
        reduceLag ( Clock c, m ) =
            if c.lag < c.period then
                ( Clock c, m )
            else
                reduceLag
                    ( Clock { c | lag = c.lag - c.period }
                    , up c.period m
                    )
    in
        reduceLag
            ( Clock { clock | lag = clock.lag + dt }
            , model
            )