0 | module Rhone.Canvas
 1 |
 2 | import Data.List
 3 | import JS
 4 | import Rhone.JS.ElemRef
 5 | import Web.Html
 6 |
 7 | import public Rhone.Canvas.Angle
 8 | import public Rhone.Canvas.Scene
 9 | import public Rhone.Canvas.Shape
10 | import public Rhone.Canvas.Style
11 | import public Rhone.Canvas.Transformation
12 |
13 | %default total
14 |
15 | public export
16 | record Canvas where
17 |   constructor MkCanvas
18 |   ref           : ElemRef HTMLCanvasElement
19 |   width, height : Double
20 |   scene         : Scene
21 |
22 | --------------------------------------------------------------------------------
23 | --          IO
24 | --------------------------------------------------------------------------------
25 |
26 | export
27 | context2D : ElemRef HTMLCanvasElement -> JSIO CanvasRenderingContext2D
28 | context2D ref = do
29 |   canvas <- getElementByRef ref
30 |   m      <- getContext canvas "2d"
31 |   case m >>= project CanvasRenderingContext2D of
32 |     Just c  => pure c
33 |     Nothing => throwError $ Caught "Rhone.Canvas.context2d: No rendering context for canvas"
34 |
35 | export
36 | render : Canvas -> JSIO ()
37 | render (MkCanvas ref w h scene) = do
38 |   ctxt <- context2D ref
39 |   apply ctxt $ Rect 0 0 w h Clear
40 |   apply ctxt scene
41 |