@@ -4,8 +4,11 @@ import Html exposing (Html, div, program)
44import Html.Attributes exposing (style )
55import Types exposing (..)
66import Dict exposing (Dict )
7+ import List
78import Svg exposing (svg , circle )
89import Svg.Attributes exposing (cx , cy , r , width , height , fill , viewBox )
10+ import WebSocket exposing (listen )
11+ import Json.Decode as Decode exposing (Decoder , field , float , decodeString )
912
1013
1114init =
@@ -16,33 +19,98 @@ update : Msg -> Model -> ( Model, Cmd Msg )
1619update msg model =
1720 case msg of
1821 Noop ->
19- init
22+ model ! []
23+
24+ Frame msg ->
25+ let
26+ trigoPoint =
27+ msg
28+ |> decodeString trigoPointDecoder
29+ |> Result . withDefault { angle = 0 , distance = 0 }
30+
31+ angle =
32+ trigoPoint. angle
33+
34+ point =
35+ trigoToCartesianPoint trigoPoint
36+
37+ _ =
38+ Debug . log " trigoPoint" point
39+ in
40+ { model | points = Dict . insert angle point model. points } ! []
41+
42+
43+ trigoPointDecoder : Decoder TrigoPoint
44+ trigoPointDecoder =
45+ Decode . map2 TrigoPoint
46+ ( field " angle" float)
47+ ( field " distance" float)
48+
49+
50+ trigoToCartesianPoint : TrigoPoint -> Point
51+ trigoToCartesianPoint { distance, angle } =
52+ let
53+ zoom =
54+ ( * ) 5
55+
56+ x =
57+ distance
58+ |> ( * ) ( cos ( degrees angle))
59+ |> floor
60+ |> zoom
61+
62+ y =
63+ distance
64+ |> ( * ) ( sin ( degrees ( angle + 180 )))
65+ |> floor
66+ |> zoom
67+ in
68+ ( x, y )
2069
2170
2271subscriptions : Model -> Sub Msg
2372subscriptions model =
24- Sub . none
73+ listen " ws://localhost:9999" Frame
74+
75+
76+ viewPoint : Point -> Html Msg
77+ viewPoint ( x, y ) =
78+ circle
79+ [ cx ( toString ( 250 + x))
80+ , cy ( toString ( 250 + y))
81+ , r " 5"
82+ , fill " #aacc66"
83+ ]
84+ []
2585
2686
2787view : Model -> Html Msg
2888view model =
29- div
30- [ style
31- [ ( " width" , " 500px" )
32- , ( " display" , " block" )
33- , ( " margin" , " 50px auto" )
34- ]
35- ]
36- [ svg [ width " 500" , height " 250" , viewBox " 0 0 500 250" ]
37- [ circle
89+ let
90+ layout =
91+ circle
3892 [ cx " 250"
3993 , cy " 250"
4094 , r " 250"
4195 , fill " #efefef"
4296 ]
4397 []
98+
99+ circles =
100+ model. points
101+ |> Dict . values
102+ |> List . map viewPoint
103+ in
104+ div
105+ [ style
106+ [ ( " width" , " 500px" )
107+ , ( " display" , " block" )
108+ , ( " margin" , " 50px auto" )
109+ ]
110+ ]
111+ [ svg [ width " 500" , height " 250" , viewBox " 0 0 500 250" ]
112+ ( layout :: circles)
44113 ]
45- ]
46114
47115
48116main =
0 commit comments