Skip to content

Instantly share code, notes, and snippets.

@sarchertech
Created May 30, 2016 02:56
Show Gist options
  • Save sarchertech/3b807091c8c2bdcd69e1b44c4168cb5b to your computer and use it in GitHub Desktop.
Save sarchertech/3b807091c8c2bdcd69e1b44c4168cb5b to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.App as Html
import Html.Events exposing (onMouseDown, onMouseUp)
import Mouse exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ x: Int
, y : Int
, mouseDown: MouseDown
}
init : (Model, Cmd Msg)
init =
({ x = 0, y = 0, mouseDown = No}, Cmd.none)
-- UPDATE
type MouseDown
= Yes
| No
type Msg
= Position Int Int
| Down MouseDown
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Position x y ->
({model | x = x, y = y}, Cmd.none)
Down isit ->
({model | mouseDown = isit}, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
case model.mouseDown of
Yes ->
Mouse.moves (\{x, y} -> Position x y)
No ->
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
let
x = model.x |> toString
y = model.y |> toString
in
div
[ onMouseDown (Down Yes)
, onMouseUp (Down No)
, style
[ ("width", "300px")
, ("height", "300px")
, ("border", "1px solid black")
]
]
[ p []
[text (x ++ ", " ++ y)]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment