on 39b3fcb help sign in
elk/core/Graphics/Form.elm
111 lines 2.9 kB history raw
2module Collage where
4data Path = Path [(Float,Float)]
6path = Path
7segment p1 p2 = Path [p1,p2]
9data Shape = Shape [(Float,Float)]
11polygon = Shape
13rect w h = polygon [ (0-w/2,0-h/2), (0-w/2,h/2), (w/2,h/2), (w/2,0-h/2) ]
14oval w h =
15 let n = 50
16 f i = (w/2 * cos (2*pi/n * i), h/2 * sin (2*pi/n * i))
17 in Shape $ map f [0..n-1]
18circle r = oval (2*r) (2*r)
19ngon n r =
20 let m = toFloat n
21 f i = ( r * cos (2*pi/m * i), r * sin (2*pi/m * i))
22 in Shape $ map f [0..n-1]
24data Transform
25 = Rotate Float
26 | Move Float Float
27 | Scale Float Float
28 | Transform Float Float Float Float Float Float
30data Form = Form [Transform] BasicForm
33data LineCap = Butt | Round | Square
34data LineJoin = Round | Bevel | Miter
36data FillStyle
37 = NoFill
38 | Solid Color
39 | Texture String
40 | LinearGradient [(Float,Color)] (Float,Float) (Float,Float)
41 | RadialGradient [(Float,Color)] (Float,Float) Float (Float,Float) Float
43-- dash pattern, dash offset, line width, cap, join, miter limit, fill style
44type LineStyle = {
45 width : Float,
46 cap : LineCap,
47 join : LineJoin,
48 miterLimit : Float,
49 fillStyle : FillStyle,
50 dashing : [Int],
51 dashOffset : Int
54data BasicForm
55 = FLine LineStyle Line
56 | FShape LineStyle FillStyle Shape
57 | FImage Int Int Int Int String
58 | FElement Element
59 | FGroup [Form]
61setFillStyle fs form =
62 case form of
63 FLine ls _ ln -> FLine ls fs ln
64 FShape ls _ shp -> FLine ls fs shp
65 _ -> form
67noFill = setFillStyle NoFill
68fillColor clr = setFillStyle (Solid clr)
69texture src = setFillStyle (Texture src)
70linearGradient stops start end = setFillStyle (LinearGradient stops start end)
71radialGradient stops innerP innerR outerP outerR =
72 setFillStyle (RadialGradient stops innerP innerR outerP outerR)
74plain = { width=1, cap=Butt, join=Miter, miterLimit=10,
75 fillStyle=...,
76 dashing=[], dashOffset=0 }
78solid c = Form [] . FLine c
79dotted c = Form [] . FLine c
80dashed c = Form [] . FLine c
81lineStyle = Form [] . FLine
84filled clr shp = Form [] (FShape shp)
85outlined clr shp = Form [] (FShape shp)
86customOutline pattern clr (Shape ps pos) =
87 Form [] (FShape (CustomOutline pattern) clr (Shape ps pos))
88textured src (Shape ps pos) = Form [] (FShape (Textured src) black (Shape ps pos))
90fromLine = FLine
91fromShape = FShape
92fromElement = FElement
94sprite w h pos src = Form 0 1 pos (FImage w h src)
95toForm pos e = Form 0 1 pos (FElement e)
97addTransform t (Form ts form) = Form (t:ts) form
99rotateRad rad = addTransform $ Rotate rad
100rotateDeg deg = addTransform $ Rotate (pi * deg / 180)
101rotate rot = addTransform $ Rotate (2 * pi * rot)
103scale s = addTransform (Scale s s)
104scaleX s = addTransform (Scale s 1)
105scaleY s = addTransform (Scale 1 s)
107move x y = addTransform (Move x y)
108moveX x = addTransform (Move x 0)
109moveY y = addTransform (Move 0 y)
111transform a b c d e f = addTransform (Transform a b c d e f)