-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
660 lines (582 loc) · 19.5 KB
/
index.html
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>
Let's Make Functional Generative Art! — YOW! Lambda Jam 2018 — Xavier Ho
</title>
<link rel="stylesheet" href="css/reveal.css" />
<link rel="stylesheet" href="css/theme/d61.css" />
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/solarized-light.css" />
<!-- Printing and PDF exports -->
<script>
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = window.location.search.match(/print-pdf/gi)
? "css/print/pdf.css"
: "css/print/paper.css";
document.getElementsByTagName("head")[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section
data-background-image="data61-theme/img/DATA61_CSIRO_OnWhite_RGB_trans.png"
data-background-position="1% 99%"
data-background-size="10%"
data-background-repeat="no-repeat"
data-markdown
>
<textarea data-template>
# Let's Make Functional Generative Art!
### [@Xavier_Ho](https://twitter.com/xavier_ho)
</textarea>
</section>
<!--
Outline
* Generative Art
* Shepherding Random Numbers
* L-Systems
* Pen Plotter
-->
<section data-background-image="img/first.jpg" background-size="cover">
<div style="display: flex; justify-content: space-around;">
<p class="tape fragment">#plottertwitter, #generative</p>
</div>
</section>
<section>
<div style="display: flex; justify-content: space-around;">
<div class="">
<img data-src="img/Elm_logo.svg" width="105" height="100" />
<h4>Elm</h4>
</div>
<div
class="fragment"
style="display: flex; justify-content: space-center; align-items: center; font-size: 200%"
>
<p>➡</p>
</div>
<div class="fragment">
<img
data-src="img/parallel-random.svg"
width="100"
height="100"
/>
<h4>SVG</h4>
</div>
<div
class="fragment"
style="display: flex; justify-content: space-center; align-items: center; font-size: 200%"
>
<p>➡</p>
</div>
<div class="fragment">
<img
data-src="img/Unofficial_JavaScript_logo_2.svg"
width="100"
height="100"
/>
<h4>Javascript</h4>
</div>
<div
class="fragment"
style="display: flex; justify-content: space-center; align-items: center; font-size: 200%"
>
<p>➡</p>
</div>
<div class="fragment">
<img data-src="img/axidraw_v3.jpg" width="100" height="100" />
<h4>Pen Plotter</h4>
</div>
</div>
</section>
<section>
<h2>demystify creative coding <a>for functional programmers</a></h2>
</section>
<section>
<h3>I. Random grid</h3>
<h3 style="color: silver;">II. Shepherding random numbers</h3>
<h3 style="color: silver;">III. L-Systems</h3>
</section>
<section
data-background-image="img/examples/grid/01.svg"
data-background-size="50% 50%"
></section>
<section
data-background-image="img/examples/grid/02.svg"
data-background-size="75% 75%"
></section>
<section
data-background-image="img/examples/grid/03.svg"
data-background-size="75% 75%"
></section>
<section
data-background-image="img/examples/grid/00.svg"
data-background-size="75% 75%"
></section>
<section>
<h3>
How to draw a <a>grid</a> of <a>line</a>s, with
<a>random rotations</a>
</h3>
<p class="fragment">
Please 🐻 with me. This will be code heavy.<br /><br />Full code
example is link in the slide at the end.
</p>
</section>
<section>
<h4>Making a grid</h4>
<pre><code class="elm" data-trim data-noescape>
import List.Extra exposing (initialize, andThen)
makeGrid : Int -> Int -> List ( Float, Float )
makeGrid m n =
initialize m toFloat
|> andThen (\x -> initialize n toFloat
|> andThen (\y -> [ ( x, y ) ]))
</code></pre>
<p>
<small
>Recipe taken from
<a
href="http://package.elm-lang.org/packages/elm-community/list-extra/7.1.0/List-Extra#andThen"
>Elm-Packages : List.Extra</a
></small
>
</p>
<pre><code class="elm" data-trim data-noescape>
> initialize 2 toFloat
[0,1] : List Float
> makeGrid 2 2
[(0,0),(0,1),(1,0),(1,1)] : List ( Float, Float )
</code></pre>
</section>
<section>
<h4>Drawing a line</h4>
<pre><code class="elm" data-trim data-noescape>
import Svg exposing (Svg)
import Svg.Attributes as Attributes
line : Float -> Float -> Float -> Float -> Svg msg
line x1 y1 x2 y2 =
Svg.line
[ Attributes.x1 <| toString x1
, Attributes.y1 <| toString y1
, Attributes.x2 <| toString x2
, Attributes.y2 <| toString y2
]
[]
</code></pre>
</section>
<section>
<h4>Generating random rotations</h4>
<img class="fragment" data-src="img/plunger.jpg" height="300" />
</section>
<section>
<h4>Generating random rotations</h4>
<img class="spin" data-src="img/plunger.jpg" height="300" />
</section>
<section>
<h3>The program structure we are about to plumb</h3>
<pre><code class="elm" data-trim data-noescape>
type Model
= Setup ...
| Model ... (...)
type Msg
= Generate
| Draw (...)
init = update Generate (Setup ...)
update msg model =
case ( msg, model ) of
( Generate, Setup ... ) ->
-- | Tell Elm to populate random numbers into Draw
( Draw, Setup ... ) ->
-- | Create our Model for rendering
view (Model ...) =
List.mapN drawFunction initialShape randomValues
</code></pre>
</section>
<section>
<h4>1. Define our own generator & model</h4>
<pre><code class="elm" data-trim data-noescape>
type Model
= Setup Int Int -- | Grid (m x n) in size
| Model Int Int (List Float) -- | Mix in random values
type Msg
= Generate
| Draw (List Float)
</code></pre>
<pre><code class="elm" data-trim data-noescape>
init : ( Model, Cmd Msg )
init =
update Generate (Setup 16 16)
</code></pre>
</section>
<section>
<h4>2. Random generation</h4>
<pre><code class="elm" data-trim data-noescape>
import Random exposing (Generator)
random : Generator Float
random =
Random.float -0.5 0.5 -- | Generate a value between -0.5 and 0.5
randomList : Int -> Generator (List Float)
randomList n =
Random.list n random
</code></pre>
<pre><code class="elm fragment" data-trim data-noescape>
update : Msg -> Model -> ( Model, Cmd Msg ) -- | Calls `view` each time
update msg model =
case ( msg, model ) of
( Generate, Setup m n ) ) ->
( model, Random.generate Draw <| randomList (m * n) )
( Draw rs, Setup m n ) ->
( Model m n rs, Cmd.none )
_ ->
( model, Cmd.none )
</code></pre>
</section>
<section>
<h4>
3. Drawing our <a>grid</a> of <a>line</a>s, with
<a>random rotations</a>
</h4>
<p>
<small
><a
href="https://spaxe.github.io/elm-generative/#grid"
target="_blank"
>View online</a
>
|
<a
href="https://github.com/spaxe/elm-generative/blob/master/example/Grid.elm"
target="_blank"
>Source</a
></small
>
</p>
<pre><code class="elm" data-trim data-noescape>
import Svg.Attributes exposing (transform)
view : Model -> Svg Msg
view (Model m n rs) =
let
flip r = if r > 0 then 0 else 90
in
List.map3
(\( dx, dy ) (( x1, y1 ), ( x2, y2 )) r ->
Svg.g
[ transform <|
(" translate(" ++ toString dx ++ "," ++ toString dy ++ ") ")
++ " rotate(" ++ toString (flip r) ++ ") "
]
[ line x1 y1 x2 y2 ]
)
(makeGrid m n)
(List.repeat (m * n) ( ( -0.5, -0.5 ), ( 0.5, 0.5 ) ))
rs
</code></pre>
</section>
<section>
<h3 style="color: silver;">I. Random grid</h3>
<h3>II. Shepherding random numbers</h3>
<h3 style="color: silver;">III. L-Systems</h3>
</section>
<section>
<h3>Shepherding random numbers</h3>
<p>Creating the <a>illusion of order</a> out of pure randomness.</p>
<ol>
<li class="fragment">
Generate a list of random values between -0.5 and 0.5. <br /><em
>e.g. A = [ 0.3, 0.5, -0.4, -0.2, 0.7 ]</em
>
</li>
<li class="fragment">
Accumulate each random value into a new list. <br /><em
>e.g. B = [ 0.3, 0.8, 0.4, 0.2, 0.9 ]</em
>
</li>
<li class="fragment">
Add both lists to some starting values. <br /><em
>e.g. C = [ 0, 1, 2, 3, 4 ] + A + B = [ 0.6, 2.3, 2.0, 3.0, 5.6
]</em
>
</li>
</ol>
</section>
<section
data-background-image="img/examples/curtain/01.svg"
data-background-size="75% 75%"
></section>
<section
data-background-image="img/examples/curtain/02.svg"
data-background-size="75% 75%"
></section>
<section
data-background-image="img/examples/curtain/00.svg"
data-background-size="75% 75%"
></section>
<section>
<h4>Shepherding values in practice</h4>
<p>
<small
><a
href="https://spaxe.github.io/elm-generative/#curtain"
target="_blank"
>View online</a
>
|
<a
href="https://github.com/spaxe/elm-generative/blob/master/example/Curtain.elm"
target="_blank"
>Source</a
></small
>
</p>
<pre><code class="elm" data-trim data-noescape>
type Model =
Setup Int | Model Int Curtain
type Curtain =
Curtain (List (List Float)) (List (List Float))
view : Model -> Svg Msg
view (Model n (Curtain a b)) =
Svg.g
[]
-- | creates `List (List (Float, Float))` with 100 segments each.
(initialiseLines 100
-- | Add random and shepherded random values to each list of points
|> List.map2 (map2First (+)) a
|> List.map2 (map2First (+)) (accumulateList a)
|> List.map2 (map2First (+)) b
|> List.map2 (map2Second (+)) (accumulateList b)
-- | Draw each line
|> List.map lines
)
</code></pre>
</section>
<section
data-background-image="img/examples/landscape/00.svg"
data-background-size="cover"
></section>
<section
data-background-image="img/examples/sun/00.svg"
data-background-size="cover"
></section>
<section>
<h3 style="color: silver;">I. Random grid</h3>
<h3 style="color: silver;">II. Shepherding random numbers</h3>
<h3>III. L-Systems</h3>
</section>
<section>
<h3>L-System is a <a>rewriting</a> ruleset</h3>
<p class="fragment">
<small
><em
>Trivia: it was originally created to simulate plants in
nature.</em
></small
>
</p>
</section>
<section>
<h3>🐢 The <a>Turtle Graphics</a> L-System 🐢</h3>
<ul>
<li>D: Draw forward 1 step</li>
<li>S: Skip forward 1 step without drawing</li>
<li>L: Turn left</li>
<li>R: Turn right</li>
</ul>
<p>
<small
>Exmaple taken from
<a
href="http://algorithmicbotany.org/papers/abop/abop.pdf"
target="_blank"
>The Algorithm Beauty of Plants</a
></small
>
</p>
</section>
<section>
<h3>🐢 Example: Rectangles</h3>
<p>Starting point: (0, 0), angle: 0° (->)</p>
<p>Starting state: DRDRDRD</p>
<img data-src="img/examples/rectangles/01.svg" height="100" />
</section>
<section>
<h3>Apply this ruleset</h3>
<p>Starting state: DRDRDRD</p>
<p>Rule: D -> <a>DDRDLDRDRDD</a></p>
<h4>We get</h4>
<p>
<a>DDRDLDRDRDD</a>R<a>DDRDLDRDRDD</a>R<a>DDRDLDRDRDD</a>R<a
>DDRDLDRDRDD</a
>
</p>
<img data-src="img/examples/rectangles/02.svg" height="200" />
</section>
<section
data-background-image="img/examples/rectangles/03.svg"
data-background-size="200%"
></section>
<section
data-background-image="img/examples/rectangles/04.svg"
data-background-size="200%"
></section>
<section
data-background-image="img/examples/rectangles/05.svg"
data-background-size="cover"
></section>
<section
data-background-image="img/examples/rectangles/00.svg"
data-background-size="cover"
></section>
<section>
<h3>Implementing a simple L-System</h3>
<pre><code class="elm" data-trim data-noescape>
type alias Rule a =
a -> List a
apply : Rule a -> List a -> List a
apply rule states =
List.concatMap (\s -> rule s) states
</code></pre>
</section>
<section>
<pre><code class="elm" data-trim data-noescape>
type State
= D
| S
| L
| R
turtle : List State -> ( Float, Float ) -> Float -> List Segment
turtle states point angle =
let
move ( x, y ) a =
( x + cos (degrees a), y + sin (degrees a) )
next ( p, a ) state =
case state of
D -> ( ( move p a, a ), [ PathD.L (move p a) ] )
S -> ( ( move p a, a ), [ PathD.M (move p a) ] )
L -> ( ( p, a - 90 ), [] )
R -> ( ( p, a + 90 ), [] )
in
List.concat (Tuple.second <| mapAccuml next ( point, angle ) states)
</code></pre>
</section>
<section
data-background-image="img/spaxe-lsystem.png"
data-background-size="cover"
></section>
<section
data-background-image="img/examples/hilbert/00.svg"
data-background-size="cover"
></section>
<section>
<img data-src="img/axidraw_v3.jpg" width="100" height="100" />
<h3>Interfacing with a pen plotter</h3>
<p>Because Elm doesn't (yet) have:</p>
<ol>
<li class="fragment">Conversion to HTML raw string</li>
<li class="fragment">Access to DOM.innerHTML()</li>
</ol>
<p class="fragment">
We use
<a
href="https://guide.elm-lang.org/interop/javascript.html"
target="_blank"
>port</a
>
— Elm's foreign function interface to Javascript.
</p>
</section>
<section>
<img
data-src="img/Unofficial_JavaScript_logo_2.svg"
width="100"
height="100"
/>
<div>
<h4>1. Specify Elm port function name</h4>
<pre><code class="elm" data-trim data-noescape>
port print : String -> Cmd msg
</code></pre>
<br />
</div>
<div class="fragment">
<h4>2. Connect Javascript function to port</h4>
<pre><code class="elm" data-trim data-noescape>
app.ports.print.subscribe( () => {
const node = document.getElementById('frame');
const payload = { svg: node.outerHTML };
http('POST', api(port, '/print'), payload)
.then(console.log)
.catch(console.error);
});
</code></pre>
</div>
</section>
<section>
<div
class="tweet"
data-src="https://twitter.com/Xavier_Ho/status/998247706819641344"
></div>
</section>
<section data-markdown>
<textarea data-template>
### Resources
* [Shepherding random numbers](https://inconvergent.net/generative/) by Inconvergent
* [The Algorithm Beauty of Plants](http://algorithmicbotany.org/papers/abop/abop.pdf) by Przemyslaw Prusinkiewicz & Aristid Lindenmayer
* [Live Coding Music](https://www.youtube.com/embed/TxQJPNSzl9s?start=450) by Andrew Sorensen
* [Generating fantasy maps](http://mewo2.com/notes/terrain/) by Martin O'Leary
* [Daily Art](http://sasj.tumblr.com/) by Saskia Freeke
* [Generated Space](http://generated.space/) by Kjetil Golid
</textarea>
</section>
<section data-markdown>
<textarea data-template>
Slide deck at [tinyurl.com/LJ2018-FGA](https://tinyurl.com/LJ2018-FGA)<br>
Play with the examples online at [spaxe.github.io/elm-generative](https://spaxe.github.io/elm-generative/)
<br><br>
### Xavier Ho
* 💌 [xavier.ho@data61.csiro.au](mailto:xavier.ho@data61.csiro.au)
* 🐙 [github.com/Spaxe](https://github.com/Spaxe)
* 🐦 [@Xavier_Ho](https://twitter.com/Xavier_Ho)
</textarea>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: "plugin/markdown/marked.js" },
{ src: "plugin/markdown/markdown.js" },
{ src: "plugin/notes/notes.js", async: true },
{
src: "plugin/zoom-js/zoom.js",
async: true,
condition: function() {
return !!document.body.classList;
}
},
{
src: "plugin/highlight/highlight.js",
async: true,
callback: function() {
hljs.initHighlightingOnLoad();
}
},
{ src: "plugin/embed-tweet/embed-tweet.js" }
]
});
</script>
</body>
</html>