on de4e9ac help sign in
elk/libraries/Graphics/Element.elm
255 lines 9.0 kB history raw
2module Graphics.Element (widthOf, heightOf, sizeOf,
3 width, height, opacity, color, tag, link,
4 image, fittedImage, croppedImage,
5 flow, up, down, left, right, inward, outward,
6 above, below, beside, layers,
7 container, absolute, relative,
8 middle, topLeft, topRight, bottomLeft, bottomRight,
9 midLeft, midRight, midTop, midBottom, middleAt,
10 topLeftAt, topRightAt, bottomLeftAt, bottomRightAt,
11 midLeftAt, midRightAt, midTopAt, midBottomAt,
12 spacer, newElement
13 ) where
15import Native.Utils (guid, max, htmlHeight)
16import JavaScript as JS
17import List as List
18import Color
19import Maybe (Just, Nothing)
21type Properties = {
22 id : Int,
23 width : Int,
24 height : Int,
25 opacity : Float,
26 color : Maybe Color,
27 href : JSString,
28 tag : JSString
29 }
31type Element = { props : Properties, element : ElementPrim }
33-- Get the width of an Element
34widthOf : Element -> Int
35widthOf e = e.props.width
37-- Get the height of an Element
38heightOf : Element -> Int
39heightOf e = e.props.height
41-- Get the width and height of an Element
42sizeOf : Element -> (Int,Int)
43sizeOf e = (e.props.width, e.props.height)
45-- Create an `Element` with a given width.
46width : Int -> Element -> Element
47width nw e = let p = e.props
48 props = case e.element of
49 Image _ w h _ -> {p| height <- h/w*nw }
50 RawHtml html -> {p| height <- let (w,h) = htmlHeight nw html in h}
51 _ -> p
52 in { element=e.element, props={props| width <- nw} }
54-- Create an `Element` with a given height.
55height : Int -> Element -> Element
56height nh e = let p = e.props
57 props = case e.element of
58 Image _ w h _ -> {p| width <- w/h*nh }
59 _ -> p
60 in { element=e.element, props={p| height <- nh} }
62-- Create an `Element` with a given opacity. Opacity is a number between 0 and 1
63-- where 0 means totally clear.
64opacity : Float -> Element -> Element
65opacity o e = let p = e.props in { element=e.element, props={p| opacity <- o} }
67-- Create an `Element` with a given background color.
68color : Color -> Element -> Element
69color c e = let p = e.props in
70 { element=e.element, props={p| color <- Just c} }
72-- Create an `Element` with a tag. This lets you link directly to it.
73-- The element `(tag "all-about-badgers" thirdParagraph)` can be reached
74-- with a link lik this: `/facts-about-animals.elm#all-about-badgers`
75tag : String -> Element -> Element
76tag name e = let p = e.props in
77 { element=e.element, props={p| tag <- JS.fromString name} }
79-- Create an `Element` that is a hyper-link.
80link : String -> Element -> Element
81link href e = let p = e.props in
82 { element=e.element, props={p| href <- JS.fromString href} }
84emptyStr = JS.fromString ""
85newElement w h e =
86 { props = Properties (guid ()) w h 1 Nothing emptyStr emptyStr, element = e }
88data ElementPrim
89 = Image ImageStyle Int Int JSString
90 | Container Position Element
91 | Flow Direction [Element]
92 | Spacer
93 | RawHtml JSString
94 | Custom -- for custom Elements implemented in JS, see collage for example
96data ImageStyle = Plain | Fitted | Cropped (Int,Int)
98-- Create an image given a width, height, and image source.
99image : Int -> Int -> String -> Element
100image w h src = newElement w h (Image Plain w h (JS.fromString src))
102-- Create a fitted image given a width, height, and image source.
103-- This will crop the picture to best fill the given dimensions.
104fittedImage : Int -> Int -> String -> Element
105fittedImage w h src = newElement w h (Image Fitted w h (JS.fromString src))
107-- Create a cropped image. Take a rectangle out of the picture starting
108-- at the given top left coordinate. If you have a 140-by-140 image,
109-- the following will cut a 100-by-100 square out of the middle of it.
110--
111-- croppedImage 100 100 (20,20) "yogi.jpg"
112croppedImage : Int -> Int -> (Int,Int) -> String -> Element
113croppedImage w h pos src =
114 newElement w h (Image (Cropped pos) w h (JS.fromString src))
116data Three = P | Z | N
117data Pos = Absolute Int | Relative Float
118type Position = { horizontal : Three, vertical : Three, x : Pos, y : Pos }
120-- Put an element in a container. This lets you position the element really
121-- easily, and there are tons of ways to set the `Position`.
122-- To center `element` exactly in a 300-by-300 square you would say:
123--
124-- container 300 300 middle element
125--
126-- By setting the color of the container, you can create borders.
127container : Int -> Int -> Position -> Element -> Element
128container w h pos e = newElement w h (Container pos e)
130-- Create an empty box. This is useful for getting your spacing right and
131-- for making borders.
132spacer : Int -> Int -> Element
133spacer w h = newElement w h Spacer
135data Direction = DUp | DDown | DLeft | DRight | DIn | DOut
137-- Have a list of elements flow in a particular direction.
138-- The `Direction` starts from the first element in the list.
139--
140-- flow right [a,b,c]
141--
142-- +---+---+---+
143-- | a | b | c |
144-- +---+---+---+
145flow : Direction -> [Element] -> Element
146flow dir es =
147 let ws = List.map widthOf es
148 hs = List.map heightOf es
149 newFlow w h = newElement w h (Flow dir es)
150 in
151 if es == [] then spacer 0 0 else
152 case dir of
153 DUp -> newFlow (List.maximum ws) (List.sum hs)
154 DDown -> newFlow (List.maximum ws) (List.sum hs)
155 DLeft -> newFlow (List.sum ws) (List.maximum hs)
156 DRight -> newFlow (List.sum ws) (List.maximum hs)
157 DIn -> newFlow (List.maximum ws) (List.maximum hs)
158 DOut -> newFlow (List.maximum ws) (List.maximum hs)
160-- Stack elements vertically. To put `a` above `b` you would say:
161--
162-- a `above` b
163above : Element -> Element -> Element
164above hi lo =
165 newElement (max (widthOf hi) (widthOf lo))
166 (heightOf hi + heightOf lo)
167 (Flow DDown [hi,lo])
169-- Stack elements vertically. To put `a` below `b` you would say:
170--
171-- a `below` b
172below : Element -> Element -> Element
173below lo hi =
174 newElement (max (widthOf hi) (widthOf lo))
175 (heightOf hi + heightOf lo)
176 (Flow DDown [hi,lo])
178-- Put elements beside each other horizontally.
179beside : Element -> Element -> Element
180beside lft rht =
181 newElement (widthOf lft + widthOf rht)
182 (max (heightOf lft) (heightOf rht))
183 (Flow right [lft,rht])
185-- Layer elements on top of each other, starting from the bottom.
186-- `(layers == flow outward)`
187layers : [Element] -> Element
188layers es =
189 let ws = List.map widthOf es
190 hs = List.map heightOf es
191 in newElement (List.maximum ws) (List.maximum hs) (Flow DOut es)
194-- Repetitive things --
196absolute : Int -> Pos
197absolute = Absolute
198relative : Float -> Pos
199relative = Relative
201middle : Position
202middle = { horizontal=Z, vertical=Z, x=Relative 0.5, y=Relative 0.5 }
203topLeft : Position
204topLeft = { horizontal=N, vertical=P, x=Absolute 0, y=Absolute 0 }
205topRight : Position
206topRight = { topLeft | horizontal <- P }
207bottomLeft : Position
208bottomLeft = { topLeft | vertical <- N }
209bottomRight : Position
210bottomRight = { bottomLeft | horizontal <- P }
211midLeft : Position
212midLeft = { middle | horizontal <- N, x <- Absolute 0 }
213midRight : Position
214midRight = { midLeft | horizontal <- P }
215midTop : Position
216midTop = { middle | vertical <- P, y <- Absolute 0 }
217midBottom : Position
218midBottom = { midTop | vertical <- N }
220middleAt : Pos -> Pos -> Position
221middleAt x y = { horizontal = Z, vertical = Z, x = x, y = y }
222topLeftAt : Pos -> Pos -> Position
223topLeftAt x y = { horizontal = N, vertical = P, x = x, y = y }
224topRightAt : Pos -> Pos -> Position
225topRightAt x y = { horizontal = P, vertical = P, x = x, y = y }
226bottomLeftAt : Pos -> Pos -> Position
227bottomLeftAt x y = { horizontal = N, vertical = N, x = x, y = y }
228bottomRightAt : Pos -> Pos -> Position
229bottomRightAt x y = { horizontal = P, vertical = N, x = x, y = y }
230midLeftAt : Pos -> Pos -> Position
231midLeftAt x y = { horizontal = N, vertical = Z, x = x, y = y }
232midRightAt : Pos -> Pos -> Position
233midRightAt x y = { horizontal = P, vertical = Z, x = x, y = y }
234midTopAt : Pos -> Pos -> Position
235midTopAt x y = { horizontal = Z, vertical = P, x = x, y = y }
236midBottomAt : Pos -> Pos -> Position
237midBottomAt x y = { horizontal = Z, vertical = N, x = x, y = y }
239up : Direction
240up = DUp
242down : Direction
243down = DDown
245left : Direction
246left = DLeft
248right : Direction
249right = DRight
251inward : Direction
252inward = DIn
254outward : Direction
255outward = DOut