Skip to content

Commit 3b5bd00

Browse files
committed
Render something to canvas
1 parent 3928e4e commit 3b5bd00

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

app/Main.hs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
12
module Main where
23

34

@@ -11,8 +12,10 @@ import GHCJS.DOM.Element
1112
import GHCJS.DOM.Node
1213
import GHCJS.DOM.EventM
1314
import GHCJS.DOM.GlobalEventHandlers
14-
import GHCJS.DOM.HTMLHyperlinkElementUtils
15+
import GHCJS.DOM.HTMLCanvasElement
16+
import GHCJS.DOM.CanvasRenderingContext2D
1517

18+
import Data.Coerce
1619

1720
main :: IO ()
1821
main = liftDOM helloMain
@@ -21,32 +24,17 @@ helloMain :: JSM ()
2124
helloMain = do
2225
doc <- currentDocumentUnchecked
2326
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

Comments
 (0)