Last active
January 11, 2017 17:11
-
-
Save thiagoarrais/24ef04f1d55176b823ece5a40bdece31 to your computer and use it in GitHub Desktop.
Trying to add a button to pause the clock (first exercise from https://guide.elm-lang.org/architecture/effects/time.html)
This file contains 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
-- Read more about this program in the official Elm guide: | |
-- https://guide.elm-lang.org/architecture/effects/time.html | |
import Html exposing (Html) | |
import Svg exposing (..) | |
import Svg.Attributes exposing (..) | |
import Svg.Events exposing (onClick) | |
import Time exposing (Time, second) | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
{ time : Time | |
, paused : Bool | |
} | |
init : (Model, Cmd Msg) | |
init = | |
({ time = 0, paused = False }, Cmd.none) | |
-- UPDATE | |
type Msg | |
= Tick Time | |
| TooglePause | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
TooglePause -> | |
({ model | paused = not model.paused }, Cmd.none) | |
Tick newTime -> | |
({ model | time = newTime }, Cmd.none) | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
case model.paused of | |
True -> Sub.none | |
False -> Time.every second Tick | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
let | |
angle = | |
turns (Time.inMinutes model.time) | |
handX = | |
toString (50 + 40 * cos angle) | |
handY = | |
toString (50 + 40 * sin angle) | |
in | |
svg [ viewBox "0 0 100 130", width "300px" ] | |
[ circle [ cx "50", cy "50", r "45", fill "#0B79CE" ] [] | |
, line [ x1 "50", y1 "50", x2 handX, y2 handY, stroke "#023963" ] [] | |
, rect [ x "0", y "100", width "100", height "28", onClick TooglePause ] [] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment