Created
June 1, 2013 14:44
-
-
Save dcalacci/5690647 to your computer and use it in GitHub Desktop.
smooth ball-following in elm
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
import Either | |
import Mouse | |
import Window | |
-- updates for movement | |
data Update = Click (Int,Int) | TimeDelta Time | |
-- control | |
-- merges the Click | |
input = let clickPos = Mouse.position | |
in merge (Click <~ clickPos) | |
(TimeDelta <~ (40 `fpsWhen` (second `since` clickPos))) | |
-- steps | |
step inp ((tx,ty),(x,y)) = | |
case inp of | |
Click t -> (t, (x,y)) | |
TimeDelta d -> ((tx,ty), ( x + (tx-x) * (d/100) , | |
y + (ty-y) * (d/100) )) | |
-- MODEL | |
data Update = Click (Int,Int) | TimeDelta Time | |
-- view | |
ball (w,h) (endpos,(x,y)) = | |
collage w h [ circle 10 |> filled blue | |
|> move (x- toFloat w/2, toFloat h/2-y) ] | |
main = ball <~ Window.dimensions | |
~ foldp step ((0,0),(0,0)) input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment