-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.elm
154 lines (111 loc) · 2.79 KB
/
circle.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (on)
import Json.Decode as Decode
import Mouse exposing (Position)
import List exposing (..)
-- Exercise
-- #1 change square to circle
-- #2 change text to nothing
-- #3 place 3 of the on the page
-- #4 draw the circle through the 3 points
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ pieces: List Tile
}
type alias Tile =
{ position : Position
, drag : Maybe Drag
}
type alias Drag =
{ start : Position
, current : Position
}
init : ( Model, Cmd Msg )
init =
( Model [ Tile (Position 200 200) Nothing , Tile (Position 400 200) Nothing , Tile (Position 200 400) Nothing ] , Cmd.none )
-- UPDATE
type Msg
= DragStart Position Tile
| DragAt Position Tile
| DragEnd Position Tile
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( updateHelp msg model, Cmd.none )
updateHelp : Msg -> Tile
updateHelp msg =
case msg of
DragStart xy tile->
Tile position (Just (Drag xy xy))
DragAt xy tile ->
Tile position (Maybe.map (\{start} -> Drag start xy) drag)
DragEnd _ tile ->
Tile (getPosition tile) Nothing
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch <| List.map tileSubscription model.pieces
tileSubscription : Model -> Sub Msg
tileSubscription tile =
case tile.drag of
Nothing ->
Sub.none
Just _ ->
Sub.batch [ Mouse.moves DragAt tile , Mouse.ups DragEnd tile ]
-- VIEW
(=>) = (,)
tileView : Tile -> Html Msg
tileView tile =
let
realPosition =
getPosition tile
in
div
[ onMouseDown
, style
[ "background-color" => "#3C8D2F"
, "cursor" => "move"
, "width" => "50px"
, "height" => "50px"
, "border-radius" => "2px"
, "position" => "absolute"
, "left" => px realPosition.x
, "top" => px realPosition.y
, "color" => "white"
, "display" => "flex"
, "align-items" => "center"
, "justify-content" => "center"
]
]
[ text "O"
]
view : Model -> Html Msg
view model =
div
[ style
[ "width" => "100%"
, "height"=> "100%"
]
] <| List.map tileView model.pieces
px : Int -> String
px number =
toString number ++ "px"
getPosition : Tile -> Position
getPosition {position, drag} =
case drag of
Nothing ->
position
Just {start,current} ->
Position
(position.x + current.x - start.x)
(position.y + current.y - start.y)
onMouseDown : Attribute Msg
onMouseDown =
on "mousedown" (Decode.map DragStart Mouse.position)