Skip to content

Instantly share code, notes, and snippets.

@adamdicarlo
Created June 6, 2016 05:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamdicarlo/1e08c9b7aabd778f7b283cbbf6f4f052 to your computer and use it in GitHub Desktop.
Save adamdicarlo/1e08c9b7aabd778f7b283cbbf6f4f052 to your computer and use it in GitHub Desktop.
Drawing a fullscreen canvas gradient in Elm 0.17
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
@adamdicarlo
Copy link
Author

Code could use some cleanup. But I did this without following any tutorials! 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment