1
+ {-# LANGUAGE ScopedTypeVariables #-}
1
2
module Main where
2
3
3
4
@@ -11,8 +12,10 @@ import GHCJS.DOM.Element
11
12
import GHCJS.DOM.Node
12
13
import GHCJS.DOM.EventM
13
14
import GHCJS.DOM.GlobalEventHandlers
14
- import GHCJS.DOM.HTMLHyperlinkElementUtils
15
+ import GHCJS.DOM.HTMLCanvasElement
16
+ import GHCJS.DOM.CanvasRenderingContext2D
15
17
18
+ import Data.Coerce
16
19
17
20
main :: IO ()
18
21
main = liftDOM helloMain
@@ -21,32 +24,17 @@ helloMain :: JSM ()
21
24
helloMain = do
22
25
doc <- currentDocumentUnchecked
23
26
body <- getBodyUnchecked doc
24
- setInnerHTML body "<h1>Kia ora (Hi)</h1>"
25
-
26
- -- Add a mouse click event handler to the document
27
- _ <- on doc click $ do
28
- (x, y) <- mouseClientXY
29
- newParagraph <- uncheckedCastTo HTMLParagraphElement <$> createElement doc "p"
30
- text <- createTextNode doc $ "Click " ++ show (x, y)
31
- appendChild_ newParagraph text
32
- appendChild_ body newParagraph
33
-
34
- -- Make an exit link
35
- exitMVar <- liftIO newEmptyMVar
36
- exit <- uncheckedCastTo HTMLAnchorElement <$> createElement doc "a"
37
- text <- createTextNode doc "Click here to exit"
38
- appendChild_ exit text
39
- appendChild_ body exit
40
-
41
- -- Set an href for the link, but use preventDefault to stop it working
42
- -- (demonstraights synchronous callbacks into haskell, as preventDefault
43
- -- must be called inside the JavaScript event handler function).
44
- setHref exit "https://github.com/ghcjs/ghcjs-dom-hello"
45
- _ <- on exit click $ preventDefault >> liftIO (putMVar exitMVar ())
46
-
47
- -- Force all all the lazy JSaddle evaluation to be executed
48
- syncPoint
49
-
50
- -- Wait until the user clicks exit.
51
- liftIO $ takeMVar exitMVar
52
- setInnerHTML body "<h1>Ka kite ano (See you later)</h1>"
27
+
28
+ (canvas :: HTMLCanvasElement) <- coerce <$> createElement doc "canvas"
29
+
30
+ appendChild_ body canvas
31
+
32
+ (ctx :: CanvasRenderingContext2D) <- coerce <$> getContextUnchecked canvas "2d" ([] :: [JSString])
33
+
34
+ setFillStyle ctx "rgb(200, 0, 0)"
35
+ fillRect ctx 10 10 50 50
36
+
37
+ setFillStyle ctx "rgba(0, 0, 200, 0.5)"
38
+ fillRect ctx 30 30 50 50
39
+
40
+ return ()
0 commit comments