In a sliding puzzle game, I'd like set the initial tile size based on the initial window dimensions (to maximize the screen real estate).
In other words, I'd like to set initialModel
based on the the initial value of Window.dimensions
.
I couldn't find how to do this, and ended up using ports to get the initial window dimensions:
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?
You can use Signal.Extra.foldp' from the Apanatshka/elm-signal-extra package to inspect base the initial value of the model on the initial value of the input signal.
Full disclosure: I'm the author of that package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With