Skip to content

Instantly share code, notes, and snippets.

@dethe
Created November 6, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dethe/49ef6a24f5f334583b3f0f8ece461cd3 to your computer and use it in GitHub Desktop.
Save dethe/49ef6a24f5f334583b3f0f8ece461cd3 to your computer and use it in GitHub Desktop.
Elm example: Canvas, Noise, AnimationFrame
import Html exposing (Html)
import Html.App as App
import AnimationFrame exposing (times, diffs)
import Time exposing (Time)
import Collage exposing (..)
import Color exposing (Color)
import Element exposing (toHtml)
import Array
import Noise exposing (PermutationTable, permutationTable, noise3d)
import Random exposing (initialSeed)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Point = {x: Int, y: Int}
type alias Model = {
points: List Point,
time: Time,
simplex: PermutationTable
}
init : (Model, Cmd Msg)
init =
let
(perm, newSeed) = permutationTable (initialSeed 42)
in
({
points = Array.toList(Array.initialize 768 (\n -> Point (n%32 - 16) (n // 32 - 16))),
time = 0,
simplex = perm
},
Cmd.none)
-- UPDATE
type Msg
= Tick Time
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Tick newTime ->
({model | time = newTime}, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
times Tick
-- VIEW
drawCircle simplex time pt =
circle 10.0
|> filled Color.red
|> alpha (noise3d simplex ((toFloat pt.x) / 15.0) ((toFloat pt.y) / 15.0) (time / 2000.0))
|> move (toFloat (pt.x * 20 + 10), toFloat (pt.y * 20 + 10))
view : Model -> Html Msg
view model =
toHtml (collage 640 480 (List.map (drawCircle model.simplex model.time) model.points))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment