How to initialize a model using Window.dimensions in Elm?

In a sliding puzzle game, I would like to set the initial tile size based on the initial window size (to increase the screen real estate).

enter image description here

In other words, I would like to set initialModel based on the initial value of Window.dimensions .

I could not find how to do this, and ended up using ports to get the initial size of the window:

index.html

 Elm.fullscreen(Elm.App, { windowSize: [ document.documentElement.clientWidth, document.documentElement.clientHeight ] }); 

App.elm

 port windowSize : (Int, Int) initialModel = -- some function of windowSize model = Signal.foldp update initialModel input type Action = WindowResize (Int, Int) | ... windowResize = Signal.map WindowResize Window.dimensions update action model = case action of WindowResize dimensions -> { model | some change based on dimensions } ... 

Is there a way to achieve the same result without using ports?

+5
source share
1 answer

You can use Signal.Extra.foldp ' from Apanatshka / elm-signal-extra to check the initial value of the model at the initial value of the input signal.

Full disclosure: I am the author of this package.

+3
source

Source: https://habr.com/ru/post/1235530/


All Articles