I'm trying to listen to the "resize" event and change the image size accordingly. However the image size does not change. I think the reason is that my "onResize" function is at the wrong place. But I don't know where else to embed it in this framework. Sorry if this sounds trivial but I've been going through the docs for a long time and couldn't find a solution. The complete elm code is listed below:
module App where
import Html exposing (..)
import Html.Attributes exposing (attribute, class, style, src, width, height)
import Html.Events exposing (on)
import Json.Decode exposing (object2, (:=), int)
import StartApp.Simple exposing (start)
-- MODEL
type alias Model =
{ width : Int
, height : Int
}
init : Model
init = { width = 800, height = 800 }
-- UPDATE
type Action = NewSize Int Int
update : Action -> Model -> Model
update (NewSize w h) model =
{ model | width = w, height = h }
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div
[ class "container", onResize address ]
[ div
[]
[ img
[ src "http://package.elm-lang.org/assets/elm_logo.svg"
, width model.width
, height model.height
]
[]
]
]
onResize : Signal.Address Action -> Attribute
onResize address =
on "resize" (getWidthHeight view) (\(w, h) -> Signal.message address (NewSize w h))
getWidthHeight : a -> Json.Decode.Decoder ( Int, Int )
getWidthHeight view =
object2 (,) ("innerWidth" := int) ("innerHeight" := int)
-- MAIN
main : Signal Html
main =
start
{ model = init
, update = update
, view = view
}