on master help sign in
Get Graphics/ libraries closer to compiling
commit5234d6d2948ed004736b65a8b2fb719924074d0e authorEvan Czaplicki <info@elm-lang.org> dateSat 27 Jul 2013, 00:51 (13 years ago) parent272557d
2 files changed+37−33
libraries/Graphics/Collage.elm+13−12 libraries/Graphics/Element.elm+24−21
libraries/Graphics/Collage.elm+13−12
@@ -2,11 +2,11 @@
22module Graphics.Collage where
33
44import List
5−import Native.Utils (toFloat)
5+import Basics (toFloat)
66import Either
7−import Native.Matrix2D as Matrix
8−import Native.Graphics.Collage as N
9−import Graphics.Element
7+import Matrix2D as Matrix
8+import Native.Graphics.Collage as Native
9+import Graphics.Element (Element)
1010import Color
1111
1212type Form = {
@@ -156,16 +156,17 @@ opacity a f = { f | alpha <- a }
156156-- A collage is a collection of 2D forms. There are no strict positioning
157157-- relationships between forms, so you are free to do all kinds of 2D graphics.
158158collage : Int -> Int -> [Form] -> Element
159+collage = Native.collage
159160
160161
161162type Path = [(Float,Float)]
162163
163164-- Create a path that follows a sequence of points.
164−path : [(Number a,Number a)] -> Path
165+path : [(number,number)] -> Path
165166path ps = ps
166167
167168-- Create a path along a given line segment.
168−segment : (Number a,Number a) -> (Number a,Number a) -> Path
169+segment : (number,number) -> (number,number) -> Path
169170segment p1 p2 = [p1,p2]
170171
171172type Shape = [(Float,Float)]
@@ -173,21 +174,21 @@ type Shape = [(Float,Float)]
173174-- Create an arbitrary polygon by specifying its corners in order.
174175-- `polygon` will automatically close all shapes, so the given list
175176-- of points does not need to start and end with the same position.
176−polygon : [(Number a,Number a)] -> Shape
177+polygon : [(number,number)] -> Shape
177178polygon points = points
178179
179180-- A rectangle with a given width and height.
180−rect : Number a -> Number a -> Shape
181+rect : number -> number -> Shape
181182rect w h = let hw = w/2
182183 hh = h/2
183184 in [ (0-hw,0-hh), (0-hw,hh), (hw,hh), (hw,0-hh) ]
184185
185186-- A square with a given edge length.
186−square : Number a -> Shape
187+square : number -> Shape
187188square n = rect n n
188189
189190-- An oval with a given width and height.
190−oval : Number a -> Number a -> Shape
191+oval : number -> number -> Shape
191192oval w h =
192193 let n = 50
193194 t = 2 * Math.PI / n
@@ -197,7 +198,7 @@ oval w h =
197198 in List.map f [0..n-1]
198199
199200-- A circle with a given radius.
200−circle : Number a -> Shape
201+circle : number -> Shape
201202circle r = oval (2*r) (2*r)
202203
203204-- A regular polygon with N sides. The first argument specifies the number
@@ -205,7 +206,7 @@ circle r = oval (2*r) (2*r)
205206-- 30 you would say:
206207--
207208-- ngon 5 30
208−ngon : Int -> Number a -> Shape
209+ngon : Int -> number -> Shape
209210ngon n r =
210211 let m = toFloat n
211212 t = 2 * Math.PI / m
libraries/Graphics/Element.elm+24−21
@@ -12,11 +12,12 @@ module Graphics.Element (widthOf, heightOf, sizeOf,
1212 spacer, newElement
1313 ) where
1414
15−import Native.Utils (guid, max, htmlHeight)
16−import JavaScript as JS
15+import open Basics
16+import Native.Utils as Native
17+import open JavaScript
1718import List as List
18−import Color
19−import Maybe (Just, Nothing)
19+import Color (Color)
20+import Maybe (Maybe, Just, Nothing)
2021
2122type Properties = {
2223 id : Int,
@@ -45,18 +46,18 @@ sizeOf e = (e.props.width, e.props.height)
4546
4647-- Create an `Element` with a given width.
4748width : Int -> Element -> Element
48−width nw e = let p = e.props
49− props = case e.element of
50− Image _ w h _ -> {p| height <- h/w*nw }
51− RawHtml html -> {p| height <- let (w,h) = htmlHeight nw html in h}
52− _ -> p
53− in { element=e.element, props={props| width <- nw} }
49+width nw e = let p = e.props
50+ props = case e.element of
51+ Image _ w h _ -> {p| height <- h `div` w * nw }
52+ RawHtml html -> {p| height <- let (w,h) = Native.htmlHeight nw html in h}
53+ _ -> p
54+ in { element=e.element, props={ props | width <- nw } }
5455
5556-- Create an `Element` with a given height.
5657height : Int -> Element -> Element
5758height nh e = let p = e.props
5859 props = case e.element of
59− Image _ w h _ -> {p| width <- w/h*nh }
60+ Image _ w h _ -> {p| width <- w `div` h * nh }
6061 _ -> p
6162 in { element=e.element, props={p| height <- nh} }
6263
@@ -71,24 +72,26 @@ opacity o e = let p = e.props in { element=e.element, props={p| opacity <- o} }
7172
7273-- Create an `Element` with a given background color.
7374color : Color -> Element -> Element
74−color c e = let p = e.props in
75− { element=e.element, props={p| color <- Just c} }
75+color c e = let p = e.props in
76+ { element = e.element
77+ , props = { p | color <- Just c}
78+ }
7679
7780-- Create an `Element` with a tag. This lets you link directly to it.
7881-- The element `(tag "all-about-badgers" thirdParagraph)` can be reached
7982-- with a link lik this: `/facts-about-animals.elm#all-about-badgers`
8083tag : String -> Element -> Element
8184tag name e = let p = e.props in
82− { element=e.element, props={p| tag <- JS.fromString name} }
85+ { element=e.element, props={p | tag <- fromString name} }
8386
8487-- Create an `Element` that is a hyper-link.
8588link : String -> Element -> Element
8689link href e = let p = e.props in
87− { element=e.element, props={p| href <- JS.fromString href} }
90+ { element=e.element, props={p | href <- fromString href} }
8891
89−emptyStr = JS.fromString ""
92+emptyStr = fromString ""
9093newElement w h e =
91− { props = Properties (guid ()) w h 1 Nothing emptyStr emptyStr (), element = e }
94+ { props = Properties (Native.guid ()) w h 1 Nothing emptyStr emptyStr (), element = e }
9295
9396data ElementPrim
9497 = Image ImageStyle Int Int JSString
@@ -102,12 +105,12 @@ data ImageStyle = Plain | Fitted | Cropped (Int,Int) | Tiled
102105
103106-- Create an image given a width, height, and image source.
104107image : Int -> Int -> String -> Element
105−image w h src = newElement w h (Image Plain w h (JS.fromString src))
108+image w h src = newElement w h (Image Plain w h (fromString src))
106109
107110-- Create a fitted image given a width, height, and image source.
108111-- This will crop the picture to best fill the given dimensions.
109112fittedImage : Int -> Int -> String -> Element
110−fittedImage w h src = newElement w h (Image Fitted w h (JS.fromString src))
113+fittedImage w h src = newElement w h (Image Fitted w h (fromString src))
111114
112115-- Create a cropped image. Take a rectangle out of the picture starting
113116-- at the given top left coordinate. If you have a 140-by-140 image,
@@ -116,11 +119,11 @@ fittedImage w h src = newElement w h (Image Fitted w h (JS.fromString src))
116119-- croppedImage (20,20) 100 100 "yogi.jpg"
117120croppedImage : (Int,Int) -> Int -> Int -> String -> Element
118121croppedImage pos w h src =
119− newElement w h (Image (Cropped pos) w h (JS.fromString src))
122+ newElement w h (Image (Cropped pos) w h (fromString src))
120123
121124tiledImage : Int -> Int -> String -> Element
122125tiledImage w h src =
123− newElement w h (Image Tiled w h (JS.fromString src))
126+ newElement w h (Image Tiled w h (fromString src))
124127
125128data Three = P | Z | N
126129data Pos = Absolute Int | Relative Float