This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Fixed time-step update loop
version 2.1.0
license Apache2.0
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 2.1.0
Committed At 2016-12-12 05:45:43 UTC
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

Modules

README

Update Clock

This module lets you update your model with a fixed time step. Even when the animation-frame updates come in fast or slow, your program will evolve smoothly and deterministically.

For a more detailed explanation of why fixed time steps are useful, check out this article.

Example

In this snippet, the physicsUpdate function gets called 30 times per second.

type alias Model =
    { clock : Clock
    , particle : Particle
    }

type alias Particle =
    { position : Float
    , velocity : Float
    }

init : Model
init =
    { clock = Clock.withPeriod (33 * Time.millisecond)
    , particle =
        { position = 0
        , velocity = 1
        }
    }

update : Action -> Model -> Model
update action model =
    case action of
        TimeDelta dt ->
            let
                ( clock, particle ) =
                    Clock.update physicsUpdate dt model.clock model.particle
            in
                { model
                    | clock = clock
                    , particle = particle
                }

physicsUpdate : Time -> Particle -> Particle
physicsUpdate delta particle =
    { particle
        | position = particle.position + particle.velocity * delta
        , velocity = particle.velocity + acceleration * delta
    }