Last active
August 29, 2015 14:21
-
-
Save TheSeamau5/7b4638b681d62ccac087 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Signal | |
import Time | |
import List | |
import Graphics.Element exposing (show) | |
import Graphics.Collage as Collage | |
import Color | |
import Random | |
initial = | |
{ center = { x = 0, y = 0 } | |
, radius = 10 | |
} | |
grow speed max frame model = | |
{ model | radius <- model.radius + speed * (toFloat frame / toFloat max) } | |
shrink speed max frame model = | |
{ model | radius <- model.radius - speed * (toFloat frame / toFloat max) } | |
view {center, radius} = | |
Collage.circle radius | |
|> Collage.filled Color.red | |
|> Collage.move (center.x, center.y) | |
|> (\x -> [x]) | |
|> Collage.collage 400 400 | |
animation = | |
repeat 7 | |
[ (50, grow 2) | |
, (20, shrink 1.5) | |
, (20, grow 4) | |
, (50, shrink 2) | |
] | |
model = | |
play animation 60 initial | |
main = | |
Signal.map view model | |
-- Library -- | |
type alias Animation state = Int -> Int -> state -> state | |
type alias Keyframe state = (Int, Animation state) | |
length : List (Keyframe state) -> Int | |
length keyframes = | |
List.foldl (\(n, _) x -> n + x) 0 keyframes | |
cycle : List (Keyframe state) -> Animation state | |
cycle keyframes maxFrames frame = | |
sequence keyframes maxFrames (frame % (maxFrames + 1)) | |
sequence : List (Keyframe state) -> Animation state | |
sequence keyframes maxFrames frame state = | |
if frame <= 0 || frame > maxFrames | |
then | |
state | |
else | |
let | |
getKeyframe list accum = | |
case list of | |
[] -> | |
Nothing | |
(n, animation) :: xs -> | |
if frame <= n + accum | |
then | |
Just (n, animation) | |
else | |
getKeyframe xs (n + accum) | |
in | |
case getKeyframe keyframes 0 of | |
Nothing -> state | |
Just (n, animation) -> | |
animation n (frame % n) state | |
once : List (Keyframe a) -> Animation a | |
once keyframes _ = | |
let n = length keyframes | |
in | |
sequence keyframes n | |
repeat n keyframes = | |
let kl = length keyframes | |
in | |
sequence | |
(List.repeat n (kl, once keyframes)) | |
forever : List (Keyframe a) -> Animation a | |
forever keyframes _ = | |
let n = length keyframes | |
in | |
cycle keyframes n | |
play : Animation state -> number -> state -> Signal state | |
play animation fps initial = | |
let | |
counter = | |
Signal.foldp (\x y -> y + 1) 0 (Time.fps fps) | |
in | |
Signal.foldp (animation Random.maxInt) initial counter | |
---- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment