-
-
Save arecvlohe/ba0fa2b6f1e0621f00dae17ac3af7c77 to your computer and use it in GitHub Desktop.
Elm 0.17 - Change Background Color Based on Mouse Position
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
"version": "1.0.0", | |
"summary": "let people do a cool thing in a fun way", | |
"repository": "https://github.com/user/project.git", | |
"license": "BSD3", | |
"source-directories": [ | |
"." | |
], | |
"exposed-modules": [], | |
"dependencies": { | |
"elm-lang/core": "4.0.0 <= v < 5.0.0", | |
"elm-lang/html": "1.0.0 <= v < 2.0.0", | |
"elm-lang/mouse": "1.0.0 <= v < 2.0.0", | |
}, | |
"elm-version": "0.17.0 <= v < 0.18.0" | |
} |
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
import Html exposing (Html, text, div) | |
import Html.Attributes exposing (style) | |
import Html.App as Html | |
import Mouse exposing (..) | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
{ x: Int | |
, y: Int | |
} | |
initialModel: Model | |
initialModel = | |
{ x = 0 | |
, y = 0 | |
} | |
init: (Model, Cmd Msg) | |
init = | |
(initialModel, Cmd.none) | |
-- Update | |
type Msg | |
= Position Int Int | |
update: Msg -> Model -> (Model, Cmd a) | |
update msg model = | |
case msg of | |
Position x y -> | |
({model | x = x, y = y}, Cmd.none) | |
-- Subscriptions | |
subscriptions: Model -> Sub Msg | |
subscriptions model = | |
Mouse.moves (\{x, y} -> Position x y) | |
-- View | |
view: Model -> Html a | |
view model = | |
div [ style | |
[ ("backgroundColor", "#" ++ (toString model.x)) | |
, ( "height", "100vh") | |
] | |
] | |
[ text (toString model) ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment