public
Description: Functional Reactive DSL in Haskell that compiles to JavaScript.
Homepage:
Clone URL: git://github.com/sebastiaanvisser/frp-js.git
frp-js / src / Demo.hs
100644 70 lines (54 sloc) 1.779 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module Demo (demo) where
 
import Control.Monad
import Geometry
import List
import Number
import Prelude hiding (mod, max, min, reverse, sin)
import Text
import Val
import Window
 
demo :: FRP ()
demo =
  do let red = ById "first"
         blue = ById "second"
         orange = ById "third"
         magenta = ById "fourth"
         pink = ById "fifth"
         boxes = [blue, orange, magenta, pink]
 
         yellow = ById "header"
 
         t1 = input "t1"
         t2 = input "t2"
         t3 = input "t3"
         t4 = input "t4"
         t5 = input "t5"
         
         subs = [blue, orange, magenta, pink]
 
     -- Window title follows mouse cursor.
     text Window <-: string (List [string (px Mouse), string (py Mouse)])
 
     -- Background color depends on mouse state.
     color Body <-:
       _if (down Mouse)
         (con "green")
         (con "white")
 
     -- Let yellow be sorted list of the five input fields.
     let inputs = List $ map text [t1, t2, t3, t4, t5]
     text yellow <-: string (sort inputs)
 
     -- Set initial geomtry.
     geom (0, 0, 40, 40) `mapM_` subs
     geom (0, 0, 400, 300) red
 
     -- Let red and yellow follow mouse.
     left red <-: 200 `max` (px Mouse - width red / 2) `min` 600
     top red <-: 200 `max` (py Mouse - height red / 2) `min` 400
     overlay yellow red 20
 
     -- Let color boxes follow red.
     mapM_ grow boxes
     zipWithM_ attachCenter boxes (corners red)
 
-- Helpers.
 
grow :: Geometry a => a -> FRP ()
grow a =
  do let s = 60 + 40 * sin (time / 200)
     width a <-: s
     height a <-: s
 
attachCenter :: Geometry a => a -> (Val Number, Val Number) -> FRP ()
attachCenter a (x, y) =
  do left a <-: x - width a / 2
     top a <-: y - height a / 2