Joy: creative coding
Joy is a small creative-coding library for OCaml by Sudha Parimala and Kaustubh Maske Patil (MIT license). You describe pictures as values: shapes that you combine and transform with ordinary functions, then render as SVG right below the code.
This page is a sandbox, not part of the guided session. There are no
problems and no checks. Use it during the game lab or afterwards if you
would rather draw than build a game. Everything you learned in Parts 1
and 2 (functions, |>, labelled arguments, lists) applies directly.
Run the cells from top to bottom: the setup cell first, then any cell you like. Edit anything and re-run it. Your edits are saved locally in this browser as you type.
This page adapts the interactive notebook from the ocaml-joy repository.
Setup
This cell defines show, which renders a list of shapes (plus a pair of
light grey axes) below the cell that calls it. Run it first.
Getting started
You create a circle by giving its radius:
Ignore the debug output val c : Joy_core.Shape.shape = ... for now.
Shapes do not render until you ask for them. Use show [ c ] to draw
the shapes in the list:
We write let _ = when we want to run a computation but do not need to
keep the result:
Basic shapes
Joy has circle, ellipse and rectangle:
show takes a list, so several shapes are separated by ;, exactly
like the lists in Part 2.
let ... in makes values local to one block, as in Part 1:
You place a shape with the labelled argument ~c (for centre).
point x y builds a point from integer coordinates:
When the variable has the same name as the label, ~c:c shortens to
~c. This is the label punning from Part 1:
line connects two points; ~a is the start (it defaults to the
origin) and the plain argument is the end:
Colors
Color shapes with with_stroke (the outline) and with_fill (the
inside):
The predefined colors are black, white, red, green, blue,
yellow and transparent. rgb makes custom colors:
Combining shapes
complex groups several shapes into one, which is useful for
transforming them together:
Transformations
Joy has translate, rotate and scale. translate moves a shape by
x and y:
|> is the pipe operator from earlier: c1 |> translate 50 0 is the
same as translate 50 0 c1.
rotate turns a shape anti-clockwise by an angle in degrees:
scale resizes a shape:
Transformations compose with more pipes:
Higher-order transformations
repeat is a higher-order function like the ones in Part 1: it takes a
transformation, applies it again and again, and keeps every intermediate
shape.
Try changing the number of repetitions or the angle:
Repeating a rotation on a square:
On a rectangle:
Or on an off-centre circle:
compose chains two transformations into one, which repeat can then
apply as a unit:
A spiral of circles:
Make something
That is the whole vocabulary: shapes, colors, complex, three
transformations, repeat and compose. Some starting points:
- A flower: repeat a rotated ellipse around the origin.
- A target: write a recursive function that builds a list of shrinking concentric circles, like the recursive functions from Part 1.
- A tunnel: repeat a composed rotate-and-scale on a rectangle, then try the same on your flower.
- Anything else. Change the numbers until it looks good; that is most of creative coding.
The ocaml-joy repository has
more examples, and the library also has polygon, map_stroke and
map_fill for you to discover.