Created
October 29, 2015 05:04
-
-
Save localshred/1c847776ca76d300f751 to your computer and use it in GitHub Desktop.
Watch the eyes follow the mouse.
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
module FollowTheMouse where | |
import Color exposing (..) | |
import Graphics.Collage exposing (..) | |
import Graphics.Element exposing (..) | |
import Mouse | |
import Signal exposing ((<~), (~)) | |
import Window | |
type alias Viewport = | |
{ width : Int | |
, height : Int | |
, halfWidth : Int | |
, halfHeight : Int | |
, minDimension : Int | |
, minX : Float | |
, maxX : Float | |
, minY : Float | |
, maxY : Float | |
} | |
main : Signal Element | |
main = | |
drawFace | |
<~ Mouse.position | |
~ Window.dimensions | |
drawFace : (Int,Int) -> (Int,Int) -> Element | |
drawFace mouseXY dimensions = | |
let | |
viewport = makeViewport dimensions mouseXY | |
x = fst mouseXY | |
y = snd mouseXY | |
xDist = (toFloat x - toFloat viewport.halfWidth) / toFloat viewport.halfWidth | |
yDist = (toFloat viewport.halfHeight - toFloat y) / toFloat viewport.halfHeight | |
face = filled yellow (circle 100) | |
rightEye = makeEye viewport 20 xDist yDist |> move (-35,20) | |
leftEye = makeEye viewport 20 xDist yDist |> move (35,20) | |
in | |
[ face | |
, leftEye | |
, rightEye | |
, show mouseXY |> toForm |> move (0,-130) | |
, show (xDist,yDist) |> toForm |> move (0,-150) | |
] |> collage viewport.width viewport.height | |
makeEye : Viewport -> Float -> Float -> Float -> Form | |
makeEye viewport size xDist yDist = | |
let | |
radius = size / 2 | |
pupilSize = size * 0.4 | |
pupilX = size * xDist * 0.5 | |
pupilY = size * yDist * 0.5 | |
gleamSize = (size * 0.25) | |
iris = circle size |> filled blue | |
pupil = circle pupilSize | |
|> filled black | |
|> move (pupilX,pupilY) | |
gleam = circle gleamSize | |
|> filled white | |
|> move (5,7) | |
|> alpha 0.5 | |
in | |
group [ iris, pupil, gleam ] | |
makeViewport : (Int,Int) -> (Int,Int) -> Viewport | |
makeViewport (width,height) (mouseX,mouseY)= | |
let | |
halfWidth = width // 2 | |
halfHeight = height // 2 | |
minX = negate halfWidth |> toFloat | |
maxX = halfWidth |> toFloat | |
minY = negate halfHeight |> toFloat | |
maxY = halfHeight |> toFloat | |
minDimension = List.minimum [width, height] |> Maybe.withDefault 20 | |
in | |
(Viewport | |
width height halfWidth halfHeight | |
minDimension minX maxX minY maxY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment