-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.elm
190 lines (145 loc) · 4.14 KB
/
Canvas.elm
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
module Canvas where
import Graphics.Element exposing (Element)
import Basics exposing (pi)
import Color exposing (..)
import Native.Canvas
import Task exposing (Task)
-- Line Styles
type LineCap = ButtCap | RoundCap | SquareCap
type LineJoin = RoundJoin | BevelJoin | MiterJoin
-- Composite Operations
type CompositeOperation
= SourceOver
| SourceIn
| SourceOut
| SourceAtop
| DestinationOver
| DestinationIn
| DestinationOut
| DestinationAtop
| Lighter
| Copy
| Xor
| Multiply
| Screen
| Overlay
| Darken
| Lighten
| ColorDodge
| ColorBurn
| HardLight
| SoftLight
| Difference
| Exclusion
| Hue
| Saturation
| Color
| Luminosity
-- Paths
type alias HasPoint a = { a | x: Float, y: Float }
type alias HasCircle a = HasPoint { a | r: Float }
type alias HasRect a = HasPoint { a | w: Float, h: Float }
type alias Point = HasPoint {}
type alias Circle = HasCircle {}
type alias Rectangle = HasRect {}
type PathMethod
= MoveTo Point
| LineTo Point
| Rect Rectangle
| Arc { x: Float, y: Float, r: Float, startAngle: Float, endAngle: Float, ccw: Bool }
| ArcTo { x1: Float, y1: Float, x2: Float, y2: Float, r: Float }
| ClosePath
type alias Path = List PathMethod
-- Images
type Image = Image
type PatternRepeat
= Repeat
| RepeatX
| RepeatY
| NoRepeat
type Pattern
= ImagePattern Image PatternRepeat
| CanvasPattern String PatternRepeat
-- Commands
type Command
-- Draw Commands
= Clear Rectangle
| FillPath Path
| StrokePath Path
| FillText String Float Float
| StrokeText String Float Float
| DrawImage Float Float Image
-- State Commands
| FillColor Color
| StrokeColor Color
| FillGrad Gradient
| StrokeGrad Gradient
| FillPattern Pattern
| StrokePattern Pattern
| FillCanvas Element
| StrokeCanvas Element
| LineWidth Float
| LineCapStyle LineCap
| LineJoinStyle LineJoin
| LineMiterLimit Float
| ShadowBlur Float
| ShadowColor Color
| ShadowOffset Float Float
| Translate Float Float
| Rotate Float
| Scale Float Float
| Font String
| Alpha Float
| Composite CompositeOperation
-- Wraps Commands in Save/Restore
| Context (List Command)
-- Draw Commands
clearRect rect = Clear rect
fillRect x y w h = FillPath [Rect (rect x y w h)]
strokeRect x y w h = StrokePath [Rect (rect x y w h)]
fillPath path = FillPath path
strokePath path = StrokePath path
fillCircle circle = FillPath [circle]
strokeCircle circle = StrokePath [circle]
fillText text x y = FillText text x y
strokeText text x y = StrokeText text x y
-- Style Commands
fillColor color = FillColor color
strokeColor color = StrokeColor color
fillGrad grad = FillGrad grad
strokeGrad grad = StrokeGrad grad
fillImage image repeat = FillPattern (ImagePattern image repeat)
strokeImage image repeat = StrokePattern (ImagePattern image repeat)
fillCanvas id repeat = FillPattern (CanvasPattern id repeat)
strokeCanvas id repeat = StrokePattern (CanvasPattern id repeat)
lineWidth width = LineWidth width
lineCap cap = LineCapStyle cap
lineJoin join = LineJoinStyle join
lineMiterLimit length = LineMiterLimit length
shadowBlur blurRadius = ShadowBlur blurRadius
shadowColor color = ShadowColor color
shadowOffset offsetX offsetY = ShadowOffset offsetX offsetY
translate x y = Translate x y
rotate angle = Rotate angle
scale scaleX scaleY = Scale scaleX scaleY
font fnt = Font fnt
alpha a = Alpha a
composite compositeOp = Composite compositeOp
context commands = Context commands
-- Path Methods
moveTo x y = MoveTo { x = x, y = y }
lineTo x y = LineTo { x = x, y = y }
rect x y w h = { x = x, y = y, w = w, h = h }
circle x y r = arc x y r 0.0 (2.0 * pi)
arcWithDir x y r startAngle endAngle ccw =
Arc { x = x, y = y, r = r, startAngle = startAngle, endAngle = endAngle, ccw = ccw }
arc x y r startAngle endAngle = arcWithDir x y r startAngle endAngle False
arcTo x1 y1 x2 y2 r = ArcTo { x1 = x1, y1 = y1, x2 = x2, y2 = y2, r = r }
-- Images
drawImage : Float -> Float -> Image -> Command
drawImage x y img = DrawImage x y img
loadImage : String -> Task String Image
loadImage = Native.Canvas.loadImage
-- Canvas
canvas : String -> (Int, Int) -> List Command -> Element
canvas = Native.Canvas.canvas