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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
| module Breakout (component) where
import Prelude
import CSS (background, block, display, margin, marginBottom, marginLeft, marginRight, marginTop, padding, px, rgb)
import CSS.Common (auto)
import Control.Monad.Rec.Class (forever)
import Data.Maybe (Maybe(..), maybe)
import Data.Time.Duration (Milliseconds(..))
import Effect (Effect)
import Effect.Aff as Aff
import Effect.Aff.Class (class MonadAff)
import Graphics.Canvas (CanvasElement, Dimensions)
import Graphics.Canvas as Canvas
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.CSS (style)
import Halogen.HTML.Properties as HP
import Halogen.Subscription as HS
import Math as Math
canvasId :: String
canvasId = "myCanvas"
type Dataset
= { x :: Number
, y :: Number
, dx :: Number
, dy :: Number
, ballRadius :: Number
}
type State
= Maybe
{ canvas :: CanvasElement
, dataset :: Dataset
}
data Action
= Initialize
| Tick
component :: forall query input output m. MonadAff m => H.Component query input output m
component =
H.mkComponent
{ initialState
, render
, eval:
H.mkEval
$ H.defaultEval
{ handleAction = handleAction
, initialize = Just Initialize
}
}
initialState :: forall i. i -> State
initialState _ = Nothing
render :: forall m. State -> H.ComponentHTML Action () m
render _ =
HH.main
[ style do
margin (px 0.0) (px 0.0) (px 0.0) (px 0.0)
padding (px 0.0) (px 0.0) (px 0.0) (px 0.0)
]
[ HH.canvas
[ style do
background (rgb 238 238 238)
display block
marginTop (px 0.0)
marginRight auto
marginBottom (px 0.0)
marginLeft auto
, HP.id canvasId
, HP.width 480
, HP.height 320
]
]
handleAction :: forall output m. MonadAff m => Action -> H.HalogenM State Action () output m Unit
handleAction = case _ of
Initialize -> do
maybeCanvas <- H.liftEffect $ Canvas.getCanvasElementById canvasId
case maybeCanvas of
Nothing -> H.put Nothing
Just canvas -> do
dim <- H.liftEffect $ Canvas.getCanvasDimensions canvas
H.put $ initialize canvas dim
_ <- H.subscribe =<< timer Tick
pure unit
Tick ->
H.get
>>= maybe mempty \state -> do
dim <- H.liftEffect $ Canvas.getCanvasDimensions state.canvas
H.liftEffect $ draw state.canvas dim state.dataset
H.put $ Just state { dataset = nextDataset dim state.dataset }
where
initialize :: CanvasElement -> Dimensions -> State
initialize canvas dim =
Just
{ canvas: canvas
, dataset:
{ x: dim.width / 2.0
, y: dim.height - 30.0
, dx: 2.0
, dy: (-2.0)
, ballRadius: 10.0
}
}
drawBall :: CanvasElement -> Dataset -> Effect Unit
drawBall canvas ds = do
ctx <- Canvas.getContext2D canvas
--
Canvas.beginPath ctx
Canvas.arc ctx
{ x: ds.x
, y: ds.y
, radius: ds.ballRadius
, start: 0.0
, end: Math.pi * 2.0
}
Canvas.setFillStyle ctx "#0095DD"
Canvas.fill ctx
Canvas.closePath ctx
draw :: CanvasElement -> Dimensions -> Dataset -> Effect Unit
draw canvas dim ds = do
ctx <- Canvas.getContext2D canvas
--
Canvas.clearRect ctx { x: 0.0, y: 0.0, width: dim.width, height: dim.height }
drawBall canvas ds
nextDataset :: Dimensions -> Dataset -> Dataset
nextDataset { width: width, height: height } dataset@{ x: x
, y: y
, dx: dx
, dy: dy
, ballRadius: r
} =
dataset
{ x = x + dx
, y = y + dy
, dx =
if between (0.0 + r) (width - r) (x + dx) then
dx
else
(-dx)
, dy =
if between (0.0 + r) (height - r) (y + dy) then
dy
else
(-dy)
}
timer :: forall m a. MonadAff m => a -> m (HS.Emitter a)
timer val = do
{ emitter, listener } <- H.liftEffect HS.create
_ <-
H.liftAff $ Aff.forkAff
$ forever do
Aff.delay $ Milliseconds 10.0
H.liftEffect $ HS.notify listener val
pure emitter
|