Skip to content

Instantly share code, notes, and snippets.

@dcalacci
Created June 1, 2013 14:44
Show Gist options
  • Save dcalacci/5690647 to your computer and use it in GitHub Desktop.
Save dcalacci/5690647 to your computer and use it in GitHub Desktop.
smooth ball-following in elm
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