Ad 1), I think that creating one Element
as a collage
of many move
d Form
s is the better approach than creating many Element
s and flow
ing them together. I don’t have a deep technical reason for that at hand, though.
Ad 2), you can get click coordinates relative to a single element (your game board) rather than relative to the whole document. Here is an example:
import Html exposing (..)
import Html.Events exposing (on)
import Html.App exposing (beginnerProgram)
import Collage exposing (collage, filled, circle)
import Color exposing (red)
import Element
import Json.Decode exposing (Decoder, (:=))
main =
beginnerProgram
{ model = Nothing
, view = view
, update = \msg _ -> msg
}
view model =
div []
[ Html.text (toString model)
, div [ on "click" (Json.Decode.map Just offsetPosition) ]
[ Element.toHtml (collage 100 100 [ filled red (circle 30) ]) ]
]
offsetPosition : Decoder ( Int, Int )
offsetPosition =
Json.Decode.object2 (,)
("offsetX" := Json.Decode.int)
("offsetY" := Json.Decode.int)
Try it out and note how when you click on the canvas element, you get displayed coordinates relevant to just that canvas element, not relative to the whole document.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import Html exposing (div)
import Html.App exposing (beginnerProgram)
import List exposing (map)
import Svg exposing (svg)
import Svg.Events exposing (onClick)
import Svg.Attributes exposing (width, height, r, cx, cy, fill)
main =
beginnerProgram
{ model = Nothing
, view = view
, update = \msg _ -> msg
}
svgCircle (xCenter, yCenter) =
Svg.circle [ r "15"
, fill "red"
, cy (toString xCenter)
, cx (toString yCenter)
, onClick (Just (xCenter, yCenter) )
] []
view model =
div []
[ Html.text (toString model)
, div []
[ svg [ width "100" , height "100" ]
(map svgCircle [ (30, 30) , (70, 30) , (50, 60) ])
]
]
--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/OBPKMzNCWlY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss...@googlegroups.com.