-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sketch.elm
310 lines (244 loc) · 7.58 KB
/
Sketch.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
module Sketch
exposing
( scene
, rectangle
, rotate
, move
, named
, random
, always
, calculate
, combine
, hsla
)
import Graphics.Render as Render
import Color exposing (Color)
import Html exposing (Html)
import Html.App
import Random
import Time
import Dict exposing (Dict)
type Shape
= Rectangle
{ width : SketchNumber
, height : SketchNumber
, color : SketchColor
}
| Rotate SketchNumber Shape
| Move SketchNumber SketchNumber Shape
type SketchNumber
= ConstantNumber Float
| RandomNumber { min : Float, max : Float }
| NamedNumber String SketchNumber
| CalculatedNumber SketchNumber (Float -> Float)
| CombinedNumber (Float -> Float -> Float) SketchNumber SketchNumber
type SketchColor
= SketchColor
{ hue : SketchNumber
, saturation : SketchNumber
, lightness : SketchNumber
, alpha : SketchNumber
}
type alias SceneState =
{ seed : Random.Seed
, pastShapes : List (Render.Form Never)
}
type SceneMsg
= NewRandomSeed
scene : Color -> Shape -> Program Never
scene backGroundColor shape =
let
sceneSize =
{ width = 500, height = 500 }
initialModel : SceneState
initialModel =
{ seed = Random.initialSeed 0
, pastShapes = []
}
update : SceneMsg -> SceneState -> ( SceneState, Cmd SceneMsg )
update msg model =
case msg of
NewRandomSeed ->
let
( newShape, newContext ) =
renderShape shape { seed = model.seed, floatCache = Dict.empty }
in
( { model
| seed = newContext.seed
, pastShapes = newShape :: model.pastShapes
}
, Cmd.none
)
view model =
renderScene sceneSize backGroundColor shape model.seed model.pastShapes
in
Html.App.program
{ init = ( initialModel, Cmd.none )
, subscriptions = \_ -> Time.every 100 (Basics.always NewRandomSeed)
, update = update
, view = view
}
renderScene :
{ width : Float, height : Float }
-> Color
-> Shape
-> Random.Seed
-> List (Render.Form Never)
-> Html msg
renderScene sceneSize backGroundColor shape seed pastShapes =
Render.group
[ Render.rectangle sceneSize.width sceneSize.height
|> Render.solidFill backGroundColor
, pastShapes
|> Render.group
, renderShape shape { seed = seed, floatCache = Dict.empty } |> fst
]
|> Render.svg sceneSize.width sceneSize.height
|> Html.App.map (\x -> Debug.crash "got a Never value")
renderShape : Shape -> Context -> ( Render.Form msg, Context )
renderShape shape context =
case shape of
Rectangle { width, height, color } ->
let
( widthValue, context1 ) =
toFloat width context
( heightValue, context2 ) =
toFloat height context1
( colorValue, context3 ) =
toColor color context2
in
( Render.rectangle widthValue heightValue
|> Render.solidFill colorValue
, context3
)
Rotate angle innerShape ->
let
( angelValue, context1 ) =
toFloat angle context
( innerShapeValue, context2 ) =
renderShape innerShape context1
in
( Render.rotate angelValue innerShapeValue
, context2
)
Move x y innerShape ->
let
( xValue, context1 ) =
toFloat x context
( yValue, context2 ) =
toFloat y context1
( innerShapeValue, context3 ) =
renderShape innerShape context2
in
( Render.move xValue yValue innerShapeValue
, context3
)
-- SHAPES
rectangle :
{ width : SketchNumber
, height : SketchNumber
, color : SketchColor
}
-> Shape
rectangle =
Rectangle
rotate : SketchNumber -> Shape -> Shape
rotate angle shape =
Rotate angle shape
move : SketchNumber -> SketchNumber -> Shape -> Shape
move x y shape =
Move x y shape
-- VALUES
random : Float -> Float -> SketchNumber
random min max =
RandomNumber { min = min, max = max }
always : Float -> SketchNumber
always value =
ConstantNumber value
named : String -> SketchNumber -> SketchNumber
named name child =
NamedNumber name child
type alias Context =
{ seed : Random.Seed
, floatCache : Dict String Float
}
calculate : SketchNumber -> (Float -> Float) -> SketchNumber
calculate base fn =
CalculatedNumber base fn
combine : (Float -> Float -> Float) -> SketchNumber -> SketchNumber -> SketchNumber
combine fn a b =
CombinedNumber fn a b
toFloat : SketchNumber -> Context -> ( Float, Context )
toFloat number context =
case number of
ConstantNumber value ->
( value, context )
RandomNumber { min, max } ->
let
( value, newSeed ) =
Random.step (Random.float min max) context.seed
in
( value, { context | seed = newSeed } )
NamedNumber name child ->
case Dict.get name context.floatCache of
Nothing ->
let
( value, newContext ) =
toFloat child context
in
( value
, { newContext
| floatCache =
Dict.insert name value newContext.floatCache
}
)
Just value ->
( value, context )
CalculatedNumber base fn ->
let
( value, newContext ) =
toFloat base context
in
( fn value, newContext )
CombinedNumber fn a b ->
let
( aValue, context1 ) =
toFloat a context
( bValue, context2 ) =
toFloat b context1
in
( fn aValue bValue, context2 )
hsla :
SketchNumber
-> SketchNumber
-> SketchNumber
-> SketchNumber
-> SketchColor
hsla hue saturation lightness alpha =
SketchColor
{ hue = hue
, saturation = saturation
, lightness = lightness
, alpha = alpha
}
toColor : SketchColor -> Context -> ( Color, Context )
toColor color context =
case color of
SketchColor { hue, saturation, lightness, alpha } ->
let
( hueValue, context1 ) =
toFloat hue context
( saturationValue, context2 ) =
toFloat saturation context1
( lightnessValue, context3 ) =
toFloat lightness context2
( alphaValue, context4 ) =
toFloat alpha context3
in
( Color.hsla
(degrees <| hueValue)
saturationValue
lightnessValue
alphaValue
, context4
)