Created
June 17, 2016 15:26
-
-
Save pdamoc/6023ecc9a1e877df91f770ae8ad80306 to your computer and use it in GitHub Desktop.
SAM with AnimationFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "1.0.0", | |
"summary": "helpful summary of your project, less than 80 characters", | |
"repository": "https://github.com/user/project.git", | |
"license": "BSD3", | |
"source-directories": [ | |
"." | |
], | |
"exposed-modules": [], | |
"dependencies": { | |
"elm-lang/animation-frame": "1.0.0 <= v < 2.0.0", | |
"elm-lang/core": "4.0.1 <= v < 5.0.0", | |
"elm-lang/html": "1.0.0 <= v < 2.0.0" | |
}, | |
"elm-version": "0.17.0 <= v < 0.18.0" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (..) | |
import Html.App as App | |
import Html.Events exposing (onClick) | |
import AnimationFrame | |
import Time exposing (Time, second) | |
-- MODEL | |
type Model | |
= Ready | |
| Countdown Time | |
| Aborted | |
| Launched | |
init : Model | |
init = | |
Ready | |
-- UPDATE | |
type Msg | |
= StartCountdown | |
| UpdateTime Time | |
| Abort | |
update : Msg -> Model -> Model | |
update msg model = | |
case ( msg, model ) of | |
( StartCountdown, Ready ) -> | |
Countdown (10 * second) | |
( UpdateTime diff, Countdown c ) -> | |
if (c - diff) < 0 then | |
Launched | |
else | |
Countdown (c - diff) | |
( Abort, Countdown _ ) -> | |
Aborted | |
( _, _ ) -> | |
model | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
case model of | |
Ready -> | |
div [] | |
[ button [ onClick StartCountdown ] [ text "Start Countdown" ] ] | |
Countdown count -> | |
div [] | |
[ p [] [ text ("Countdown: " ++ (toString (ceiling (count / 1000)))) ] | |
, button [ onClick Abort ] [ text "Abort" ] | |
] | |
Aborted -> | |
div [] [ text "Launch Aborted!" ] | |
Launched -> | |
div [] [ text "Rocket Launched!" ] | |
-- WIRING | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
case model of | |
Countdown _ -> | |
AnimationFrame.diffs UpdateTime | |
_ -> | |
Sub.none | |
main : Program Never | |
main = | |
App.program | |
{ init = init ! [] | |
, update = \msg model -> update msg model ! [] | |
, view = view | |
, subscriptions = subscriptions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment