Created
June 6, 2016 05:35
-
-
Save adamdicarlo/1e08c9b7aabd778f7b283cbbf6f4f052 to your computer and use it in GitHub Desktop.
Drawing a fullscreen canvas gradient in Elm 0.17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FullscreenGradient exposing (..) | |
-- elm-lang/core | |
import Task | |
import Color exposing (linear, rgb) | |
-- elm-lang/html | |
import Html exposing (..) | |
import Html.App as Html | |
-- evancz/elm-graphics | |
import Collage exposing (collage, gradient, rect) | |
import Element exposing (toHtml) | |
-- elm-lang/window | |
import Window exposing (..) | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- Model | |
type alias Model = | |
{ size : Window.Size | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( Model ( Window.Size 0 0 ), Task.perform (\_ -> Fail) (\x -> Resize x) Window.size ) | |
type Msg | |
= Resize Window.Size | |
| Fail | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Resize newSize -> | |
( Model newSize, Cmd.none ) | |
Fail -> | |
( model, Cmd.none ) | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.batch [ Window.resizes Resize ] | |
view : Model -> Html Msg | |
view model = | |
let | |
w = | |
model.size.width | |
h = | |
model.size.height | |
clrStart = | |
rgb 5 250 140 | |
clrEnd = | |
rgb 231 59 87 | |
clrStops = | |
[ ( 0.0, clrStart ), ( 1.0, clrEnd ) ] | |
gfx = | |
collage w h | |
[ gradient (linear ( 0, 0 ) ( toFloat w, toFloat h ) clrStops) (rect (toFloat w) (toFloat h)) ] | |
in | |
toHtml gfx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code could use some cleanup. But I did this without following any tutorials! 😂