-
Notifications
You must be signed in to change notification settings - Fork 7
/
Example.lhs
91 lines (68 loc) · 2.61 KB
/
Example.lhs
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
> {-# LANGUAGE TypeFamilies #-}
> {-# LANGUAGE OverloadedStrings #-}
> {-# LANGUAGE ScopedTypeVariables #-}
> module Example where
> import Control.Lens ((^.), (.~), (^?), (+~), ix, from)
> import Data.Function ((&))
> import Data.Monoid (mempty)
> import Data.Semigroup (mappend)
> import Reflex (Dynamic)
> import Reflex.Dom (MonadWidget, (=:))
> import qualified Reflex.Dom.Widget.SVG as S
> import Reflex.Dom.Widget.SVG.Types (SVG_Rect)
> import qualified Reflex.Dom.Widget.SVG.Types as S
> exampleUsage :: forall t m. ( MonadWidget t m ) => m ()
> exampleUsage = do
> let
Construct the ``<svg>`` element.
> dSvgProps = pure $ S.SVG_El
> (S.Width 400)
> (S.Height 300)
> Nothing
Create a normal ``Map`` of HTML attributes to apply to the shape
> attrs = mempty
> & at "id" ?~ "svg1"
> & at "class" ?~ "blue no-yellow"
Build our first ``<rect>``.
> dRect1 = pure $ S.SVG_Rect
> (S._PosX # 40.0)
> (S._PosY # 40.0)
> (S.Width 50.0)
> (S.Height 50.0)
> Nothing
> Nothing
This is the same as writing: <rect x="40" y="40" height="50" width="50">.
We can use lenses to modify the properties of our shape.
> shiftRect :: Dynamic t SVG_Rect -> Dynamic t SVG_Rect
> shiftRect = fmap (S.svg_rect_pos_x . S._PosX +~ 70.0)
We can also define a ``<rect>`` with corner radius.
> dRect3 = pure $ S.SVG_Rect
> (S._PosX # 20.0)
> (S._PosY # 20.0)
> (S.Width 50.0)
> (S.Height 50.0)
> (15.0 ^? from S._CornerRadiusX)
> (15.0 ^? from S._CornerRadiusY)
This is the same as <rect x="20" y="20" height="50" width="50" rx="15" ry="15">.
Build a ``<circle>``.
> dCircle = pure $ S.SVG_Circle
> (S._PosCenterX # 200.0)
> (S._PosCenterY # 200.0)
> (S._Radius # 70.0)
This is the same as writing: <circle cx="200" cy="200" r="70">.
We can also build some ``Dynamic`` animation element properties:
> dAnim = S.makeAnimateProps $ S.SVG_Animate
> ( S.AttributeName "x" )
> ( S.AnimFrom 10 )
> ( S.AnimTo 100 )
> ( S.Secs 10 )
> ( S.Indefinite )
This is the same as having written: <animate attributeName="x" from="10" to="100" dur="10s" repeatCount="indefinite"/>
Finally, put it all together for ``Reflex.Dom`` to add to our page.
> _ <- S.svg_ dSvgProps $ do
> _ <- S.svgBasicDyn S.Rectangle (mappend attrs . S.makeRectProps) (shiftRect dRect1) (pure mempty)
> _ <- S.svgBasicDyn_ S.Rectangle S.makeRectProps dRect1
> _ <- S.svgBasicDyn_ S.Circle S.makeCircleProps dCircle
> S.svgBasicDyn S.Rectangle S.makeRectProps dRect3 ( pure $ S.Animate =: dAnim )
>
> pure ()