# asobi

> Object to Art. Art to Object. Turn Ruby numbers, strings, arrays, and your own classes into pictures with to_art. Every picture returns as an immutable Ruby object you can hold, arrange, and animate.

- Gem: `asobi` (v0.2.2, Ruby >= 3.2, zero runtime dependencies, MIT License)
- Module: `Asobi` (`require "asobi"` or `require "asobi/pure"` for no core extensions)
- Website: https://asobi.myura.me/
- Japanese Website: https://asobi.myura.me/ja/
- Full LLM Context: https://asobi.myura.me/llms-full.txt
- Markdown on request: every page URL answers in Markdown with `Accept: text/markdown` (`curl -H "Accept: text/markdown" https://asobi.myura.me/reference`)
- Agent Skill: https://asobi.myura.me/.well-known/agent-skills/asobi/SKILL.md
- AI Catalog (ARD): https://asobi.myura.me/.well-known/ai-catalog.json
- Source Code: https://github.com/Hayato1031/asobi
- RubyGems: https://rubygems.org/gems/asobi

## Core Concept: Object to Art. Art to Object.

1. **Object to Art**: Ask any Ruby object for `to_art` (like `to_s` or `to_a`).
   - `Numeric`: circle of that radius (negative = outlined, `0` = nothing, or pass `as: star`).
   - `String` / `Symbol`: text picture (`"asobi".chars.to_art` for per-letter styling).
   - `Array` / `Range`: row of pictures; nested arrays stack into a grid.
   - `Hash`: key picture beside value picture, stacked by entry.
   - Custom classes: define `def to_art` (optionally `include Asobi::Art` to chain words like `spin`, `colored`, `move`).
2. **Art to Object**: Every picture is an immutable Ruby value (`Data` / `Asobi::Art`). Calling `.colored(:crimson)` or `.rotate(45)` returns a new picture without mutating the original—never mutating a global canvas.
3. **Pure-Function Motion**: Animations (`Asobi::Motion`) are pure functions of `seconds -> art`, allowing live browser reload without resetting motion phase and clean `.trail` afterimages.
4. **Reactions and Sound**: A word after a motion says who turns its frames instead of the clock: `when_clicked`, `while_hovered`, `when_pressed("a")`, `on_beat`, or `by(:mouse_x)` / `by(:mic, :low)` / `by(:music)`. `play "song.mp3"` loops a song, and `sounding(:kick)` or `sounding(:c4)` after a reaction plays a synthesized drum sound or note. Editing the code does not restart the song, for live coding and VJ sets.

## Quick Example

```ruby
require "asobi"

class Planet
  include Asobi::Art
  def initialize(size, distance, color, year) = (@size, @distance, @color, @year = size, distance, color, year)
  def to_art = ring(@distance, 0.4).colored(:dimgray) + circle(@size).colored(@color).orbit(@distance, every: @year)
end

stars = [3, 1, 4, 1, 5, 9].to_art(as: star).in_ring(38).colored(:hotpink, :gold, :cyan)
show stars.spin(every: 6).glowing, on: :black, grain: true
```

## Documentation Pages (HTML & Markdown)

- [Home (English)](https://asobi.myura.me/) · [Markdown](https://asobi.myura.me/index.md): Overview, Object ⇄ Art loop, live stage, FAQ
- [Getting Started (12 chapters)](https://asobi.myura.me/guide) · [Markdown](https://asobi.myura.me/guide.md): Step-by-step tutorial
- [Reference (79 words)](https://asobi.myura.me/reference) · [Markdown](https://asobi.myura.me/reference.md): Complete API vocabulary with live SVG examples
- [Examples (16 pieces)](https://asobi.myura.me/examples) · [Markdown](https://asobi.myura.me/examples.md): Finished sketches
- [Browser Playground (ruby.wasm)](https://asobi.myura.me/playground) · [Markdown](https://asobi.myura.me/playground.md): Zero-install live Ruby editor
- 日本語: [トップ](https://asobi.myura.me/ja/) · [はじめかた](https://asobi.myura.me/ja/guide.md) · [リファレンス](https://asobi.myura.me/ja/reference.md) · [見本集](https://asobi.myura.me/ja/examples.md) · [遊び場](https://asobi.myura.me/ja/playground.md)

## Vocabulary Index (79 words in 10 groups)

- **shapes** (22): `circle`, `ellipse`, `rect`, `square`, `triangle`, `polygon`, `star`, `line`, `text`, `path`, `arc`, `wave`, `ring`, `heart`, `petal`, `moon`, `drop`, `cloud`, `space`, `nothing`, `hsl`, `gray`
- **combine** (7): `on`, `under`, `+`, `repeat`, `*`, `beside`, `above`
- **change** (7): `move`, `rotate`, `scale`, `grow`, `shrink`, `mirror`, `flip`
- **color** (4): `colored`, `outlined`, `stroke`, `opacity`
- **effect** (5): `glowing`, `blurred`, `blended`, `shadowed`, `clipped_by`
- **arrange** (8): `in_row`, `in_column`, `in_grid`, `in_ring`, `in_fan`, `in_spiral`, `scattered`, `change_each`
- **to_art** (1): `to_art`
- **motion** (13): `spin`, `pulse`, `sway`, `orbit`, `fade`, `flash`, `bounce`, `drift`, `shift_color`, `one_by_one`, `trail`, `animate`, `at`
- **react** (7): `when_clicked`, `while_hovered`, `when_pressed`, `on_beat`, `by`, `sounding`, `play`
- **show** (5): `show`, `save`, `to_svg`, `bounds`, `turn`

---

## Getting Started Guide

### 1. Install and the first minute (インストールと、最初の1分)

Install the gem. You need Ruby 3.2 or newer, and nothing else.

Type `asobi` in a terminal. The first time, it writes a small sketch called play.rb and opens it in your browser. This is the sketch:

```ruby
# Change anything and save. The picture follows.
flower = petal.repeat(8).in_fan.colored(:crimson, :navy)

show flower.spin(every: 8)
```

Open play.rb in any editor, change a number and save. The picture in the browser changes by itself. Try 8 to 12.

```ruby
flower = petal.repeat(12).in_fan.colored(:crimson, :navy)

show flower.spin(every: 8)
```

Press Ctrl-C in the terminal to stop. Later you can run any file with `asobi file.rb` or `ruby file.rb`.

### 2. Make a shape (形を作る)

A shape word makes a picture. `show` puts it on the screen. That is all you need for a first picture.

```ruby
show circle
```

Every number can be left out, so it just works. When you want to decide, give the number a name.

```ruby
show circle(radius: 30)
```

There are many shapes. Try them one by one: `rect`, `triangle`, `star`, `heart`, `moon`, `cloud`.

```ruby
show star(points: 7, radius: 30)
```

A picture is a value, so you can keep it in a variable. Think of it as a sticker: you make it once, and then you can ask it to do things.

```ruby
dot = circle(10)
show dot
```

Lengths have no unit, and the picture is fitted to the screen. Only the proportions matter.

### 3. Make more of it (増やす)

`repeat` copies a picture. Here are five circles. They all lie in the same place, so you see only one.

```ruby
show circle.repeat(5)
```

Now put them in a row. Copying and arranging are two separate steps.

```ruby
show circle.repeat(5).in_row
```

`*` is the short way to write `repeat`. It is one of the only two symbols in asobi.

```ruby
dots = circle * 5
show dots.in_row(gap: 6)
```

### 4. Arrange it (並べる)

`in_ring` puts the copies evenly round a circle. You did not say how big the ring is, and it still works: it picks a size where the neighbours do not touch.

```ruby
show circle(5).repeat(12).in_ring
```

`in_grid` makes a table. The number of columns can be written without its name.

```ruby
show square.repeat(12).in_grid(4, gap: 4)
```

A `petal` starts at the centre and grows straight up. Copy it and turn each copy a little more with `in_fan`: that is a flower.

```ruby
show petal.repeat(8).in_fan
```

More ways to arrange: `in_spiral`, `in_row`, `in_column`, and `scattered`, which puts things in random places. A scattered picture is different every time you run it, but it does not jump around when you save.

```ruby
show circle(3).repeat(150).in_spiral
```

To change each copy a little differently, use `change_each`. The block gets the picture, `t` (0.0 for the first, up to nearly 1.0 for the last) and the number.

```ruby
dots = circle(5).repeat(12).change_each { |dot, t| dot.grow(to: 1 + t * 2) }
show dots.in_row(gap: 4)
```

### 5. Add colour (色を付ける)

`colored` paints. A colour is a CSS colour name written as a symbol: `:crimson`, `:navy`, `:skyblue`, `:gold`. With no name at all, you get crimson.

```ruby
show heart.colored(:crimson)
```

Give a group several colours and they are used in turn.

```ruby
show petal.repeat(8).in_fan.colored(:crimson, :navy)
```

For a rainbow, use `hsl`. The hue is an angle from 0 to 360, and `t` in `change_each` goes from 0 up to 1, so the hue goes once round.

```ruby
dots = circle(8).repeat(12).change_each { |dot, t| dot.colored(hsl(t * 360, 80, 55)) }
show dots.in_ring
```

`outlined` keeps only the line. `stroke` keeps the fill and adds a line. `opacity` makes it see-through.

```ruby
show star(radius: 30).colored(:gold).stroke(:orange, width: 3)
```

The page is white by default. Give `show` a background with `on:`.

```ruby
show star(radius: 30).colored(:gold), on: :midnightblue
```

### 6. Put pictures together (合わせる)

`on` puts one picture on top of another.

```ruby
show circle(8).colored(:gold).on(circle(20).colored(:navy))
```

`+` puts pictures on top of each other too. What you write later is in front. It is the other symbol in asobi.

```ruby
sun = circle(30).colored(:gold)
show sun + circle(14).colored(:orange) + circle(5).colored(:crimson)
```

`beside` puts pictures side by side, and `above` stacks them. The result is again one picture.

```ruby
show circle.beside(square).above(triangle)
```

That is the point: whatever you build is one picture, so you can copy it, arrange it and paint it again. Here is a face, built from smaller pictures.

```ruby
eyes = circle(3).repeat(2).in_row(gap: 12)
smile = arc(10, from: 90, to: 270).outlined(:black, width: 2)
face = eyes.above(smile, gap: 6).on(circle(24).colored(:gold))
show face.repeat(4).in_row(gap: 6)
```

### 7. Ask objects to be pictures (オブジェクトに「絵になって」と頼む)

Any object can be asked to become a picture with `to_art`. A number is a circle of that radius. An array is a row of pictures.

```ruby
show [4, 8, 12, 16].to_art
```

Numbers do not have to be circles. Say what shape they should be with `as:`, and the numbers become sizes.

```ruby
fib = [1, 1, 2, 3, 5, 8, 13]
show fib.to_art(as: star).colored(:gold, :orange), on: :black
```

Strings become letters. `chars` splits a word into one letter each, so each letter is its own picture you can colour.

```ruby
show "asobi".chars.to_art.colored(:crimson, :navy)
```

An array of arrays is a table: each inner array is a row.

```ruby
table = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
show table.to_art(as: square).colored(:teal, :navy)
```

`show` asks for `to_art` for you, so for a plain list of numbers you can leave it out. To paint or move it, ask first.

```ruby
show [3, 6, 9, 12]
```

### 8. Make it move (動かす)

A motion word turns a picture into a picture in motion. `every` is how many seconds one loop takes.

```ruby
show star(radius: 30).colored(:gold).spin(every: 3)
```

Motion words follow each other. The words you already know, like `colored`, work on motions too.

```ruby
flower = petal.repeat(8).in_fan.colored(:crimson, :navy)
show flower.spin(every: 8).pulse(every: 1.5)
```

`one_by_one` makes a group move one at a time instead of all together. Each dot pulses around its own centre, so the ring keeps its shape.

```ruby
dots = circle(5).repeat(24).in_ring.colored(:crimson, :orange)
show dots.pulse(every: 2, to: 2).one_by_one
```

`one_by_one` belongs right after the motion word it staggers. To stagger two motions, write it after each. Here the colour and the bounce both go one by one.

```ruby
hearts = heart(20).repeat(5).in_row(gap: 6)
show hearts.shift_color(:crimson, :orchid).one_by_one.bounce(height: 24).one_by_one
```

A moving picture and a still one go together: still on moving gives a motion.

```ruby
sun = circle(10).colored(:gold)
planet = circle(4).colored(:skyblue).orbit(radius: 30, every: 5)
show sun + planet, on: :black
```

For your own motion, `animate` gives you the picture and `t`, which runs from 0.0 up to 1.0 and starts again.

```ruby
show star(radius: 20).animate(4) { |picture, t| picture.rotate(t * 360).grow(to: 1 + t) }
```

### 9. Make it react (反応させる)

A motion can wait for you. Write `when_clicked` right after a motion: the picture rests until you click it, then plays the motion once.

```ruby
flower = petal(30, 12).repeat(8).in_fan.colored(:crimson, :navy)
show flower.spin(every: 1).when_clicked
```

`while_hovered` plays while the pointer is over the picture. `when_pressed` plays when you press a key. Name one, like `when_pressed("a")`, or leave it out for any key.

```ruby
twinkle = star(radius: 20).colored(:gold).spin(every: 1).while_hovered
ball = circle(20).colored(:dodgerblue).bounce(every: 0.6).when_pressed
show twinkle.beside(ball, gap: 30)
```

`by(:mouse_x)` lets the pointer choose the moment. From the left edge to the right edge, the wheel turns once round. `:mouse_y` goes from top to bottom.

```ruby
wheel = petal(40, 14).repeat(12).in_fan.colored(:teal, :gold)
show wheel.spin.by(:mouse_x)
```

`sounding` makes a sound each time a reaction plays: a drum, a bell, or a note like `:c4`. Browsers allow sound only after a click, so the sound starts with your first click.

```ruby
dots = %i[c4 d4 e4 g4 a4].map do |note|
  circle(12).colored(:tomato).pulse(every: 0.3, to: 1.5).when_clicked.sounding(note)
end
show dots.to_art.in_row(gap: 8)
```

The picture can listen, too. `by(:mic, :low)` follows the bass the microphone hears, and `on_beat` plays once on every beat. Click once, and the browser asks if asobi may listen.

```ruby
burst = star(points: 8, radius: 30, inner: 12).colored(:deeppink)
bass = burst.pulse(to: 1.6).by(:mic, :low)
kick = ring(radius: 50, thickness: 3).colored(:cyan).pulse(every: 0.4, to: 1.3).on_beat
show bass.on(kick), on: :black
```

Last, `play` plays a song, round and round, and `:music` listens to it instead of the microphone. Put the song next to your sketch. Here and in the playground, try `"beat.wav"`, a short drum loop.

```ruby
play "beat.wav"
burst = star(points: 8, radius: 30, inner: 12).colored(:orange)
bass = burst.pulse(to: 1.6).by(:music, :low)
kick = ring(radius: 50, thickness: 3).colored(:cyan).pulse(every: 0.4, to: 1.3).on_beat(:music)
show bass.on(kick).glowing, on: :black
```

### 10. Make it glow (光らせる)

So far everything was flat: plain colours, like ink on white paper. That is the right place to start. Richness is something you add, one word at a time. Here is a flat flower on a dark page.

```ruby
flower = petal(40, 16).repeat(8).in_fan.colored(:hotpink, :gold)
show flower, on: :black
```

Add `glowing`. The edges glow in the picture's own colour. A glow needs a dark page, which is why we chose black.

```ruby
flower = petal(40, 16).repeat(8).in_fan.colored(:hotpink, :gold)
show flower.glowing, on: :black
```

`blended` chooses how a picture mixes with what is under it. With `:screen` (the default), lights add up: where the circles overlap, the colour gets lighter. It shows only because something is underneath, here the other circle and the black page.

```ruby
red = circle(30).colored(:crimson).move(left: 14)
blue = circle(30).colored(:dodgerblue).move(right: 14)
show red.blended + blue.blended, on: :black
```

`colored` can also make a gradient. Name where it starts and ends. On a group, the gradient flows once across the whole group, and `around: true` runs it from the centre outwards.

```ruby
petals = petal(50, 18).repeat(8).in_fan.colored(from: :hotpink, to: :gold, around: true)
show petals.glowing, on: :black
```

`trail` leaves fading copies of earlier moments behind a moving picture. It does nothing to a still picture.

```ruby
comet = circle(5).colored(:gold).orbit(radius: 30, every: 3).trail(10)
show comet.glowing, on: :black
```

Last, `grain: true` in `show` lays a fine film grain over the whole picture, like paper or old film. It is a way of showing, so it is written in `show`, not on the picture.

```ruby
comet = circle(5).colored(:gold).orbit(radius: 30, every: 3).trail(10)
show comet.glowing, on: :black, grain: true
```

On a light page, use `shadowed` to make pictures look like cut-out paper lying on top of each other.

```ruby
sun = circle(22).colored(:gold).move(up: 20, right: 40)
hill = ellipse(220, 70).colored(:seagreen).move(down: 50)
show sun.shadowed + hill.shadowed, on: :oldlace
```

And `clipped_by` shows a picture only through the window of a shape. Here, stripes seen through a heart.

```ruby
stripes = rect(10, 80).repeat(10).in_row.colored(:crimson, :gold)
show stripes.clipped_by(heart(60))
```

### 11. Show it live, and record it (ライブで見る、録画する)

When you run a sketch, the browser opens and watches the file. Every time you save, the picture is swapped for the new one, and the motion carries on from where it was.

```ruby
show circle(30).colored(:crimson).pulse
```

If you make a typing mistake, nothing closes. The last good picture stays on the screen, and the error is written below it, with a hint on how to fix it. Fix it and save.

Move the mouse and small controls appear at the edge for a few seconds. Press `c` to lay the code over the picture. Press `r` to start recording, and `r` again to stop: the browser saves a video file.

`show` can also take a background (`on:`) and a paper size (`paper:`).

```ruby
show star(radius: 30).colored(:gold).spin(every: 4), on: :black, paper: [400, 400]
```

To keep a picture, use `save`. A still picture becomes an SVG file. A motion becomes an HTML page that plays by itself. (Remove the `#` to try it.)

```ruby
flower = petal.repeat(8).in_fan.colored(:crimson, :navy)

# flower.save("flower.svg")
# flower.spin(every: 8).save("flower.html")

show flower.spin(every: 8)
```

### 12. Make your own class a picture (自分のクラスを絵にする)

Here is the idea of asobi: treat objects as pictures, and pictures as objects. Write a method called `to_art` in your class and say what the object looks like.

```ruby
class Flower
  def initialize(petals, color)
    @petals = petals
    @color = color
  end

  def to_art
    petal(30, 12).repeat(@petals).in_fan.colored(@color)
  end
end

show Flower.new(8, :crimson)
```

Now your objects can go anywhere a picture can: in an array, beside another picture, on top of one.

```ruby
class Flower
  def initialize(petals, color)
    @petals = petals
    @color = color
  end

  def to_art
    petal(30, 12).repeat(@petals).in_fan.colored(@color)
  end
end

garden = [Flower.new(5, :crimson), Flower.new(8, :navy), Flower.new(12, :gold)]
show garden.to_art
```

To call picture words directly on your objects, like `flower.spin`, add one line to the class: `include Asobi::Art`. It works like `Enumerable`: you write `each`, and you get `map`, `select` and the rest. Here you write `to_art`, and you get `spin`, `move`, `colored` and the rest.

```ruby
class Flower
  include Asobi::Art

  def initialize(petals, color)
    @petals = petals
    @color = color
  end

  def to_art
    petal(30, 12).repeat(@petals).in_fan.colored(@color)
  end
end

show Flower.new(8, :navy).spin(every: 6)
```

Anything can be a picture. A class can hold numbers, names, dates, anything, and decide for itself how to look. Next, look at the gallery, or open the reference to see every word.

---

## Complete Reference (79 Words)

### Shapes (`shapes`)

Built-in geometric and organic shapes plus colour helpers (`hsl`, `gray`), all centred at the origin with optional arguments.

#### `circle` — `circle(radius: 10)`

A filled circle.

Every shape sits with its centre at the middle of the picture, and every number can be left out.

The first number can be written without its name: `circle(12)` is the same as `circle(radius: 12)`.

Lengths have no unit. Only the proportions matter, because the picture is fitted to the screen.

```ruby
show circle
```
_No numbers: it just works._

```ruby
show circle(radius: 30)
```
_A bigger one, with the number named._

#### `ellipse` — `ellipse(width: 30, height: 20)`

A filled ellipse, wider than it is tall.

`width` and `height` are the full size, not the radius. The first two numbers can be written without names: `ellipse(40, 10)`.

```ruby
show ellipse
```

```ruby
show ellipse(width: 12, height: 40)
```

#### `rect` — `rect(width: 30, height: 20)`

A filled rectangle.

For a rectangle with equal sides, `square` is shorter to write.

```ruby
show rect
```

```ruby
show rect(width: 60, height: 8)
```

#### `square` — `square(size: 20)`

A filled square.

`size` is the length of one side.

```ruby
show square
```

```ruby
show square(size: 40).rotate(45)
```

#### `triangle` — `triangle(size: 20)`

A filled triangle with equal sides, pointing up.

`size` is the length of one side. The triangle is centred on its bounding box, so it lines up with other shapes.

```ruby
show triangle
```

```ruby
show triangle(size: 50).colored(:orange)
```

#### `polygon` — `polygon(sides: 6, radius: 10)`

A filled shape with equal sides. One corner points up.

`sides` must be 3 or more. `radius` is the distance from the centre to a corner. The first two numbers can be written without names: `polygon(8, 20)`.

```ruby
show polygon
```
_A hexagon, by default._

```ruby
show polygon(sides: 8, radius: 30).colored(:teal)
```

#### `star` — `star(points: 5, radius: 10, inner: radius * 0.45)`

A filled star.

`points` is how many tips it has. `radius` reaches to the tips. `inner` is how far in the notches go: a small `inner` makes a sharp star, a big one makes a fat star.

The first two numbers can be written without names: `star(8, 30)`.

```ruby
show star
```

```ruby
show star(points: 8, radius: 30, inner: 24).colored(:gold)
```

#### `line` — `line(length: 20)`

A flat black line.

A line has no fill, only a stroke. So `colored` changes the colour of the line, and `outlined(color, width:)` changes its colour and thickness.

To draw a line between points you choose, use `path`.

```ruby
show line
```

```ruby
show line(length: 80).outlined(:crimson, width: 6)
```

#### `text` — `text(string = "asobi", size: 16, font: nil)`

Letters as a picture.

A text is a picture like any other: you can colour it, repeat it, turn it and arrange it. Strings and symbols become the same picture with `to_art`, so `"hello".to_art` is `text("hello")`.

`size` is the height of the letters. `font` is the name of a font, such as `"serif"`. Leave it out to use the page's usual font.

```ruby
show text
```
_No arguments: the word "asobi"._

```ruby
show text("hello", size: 40, font: "serif").colored(:navy)
```

#### `path` — `path(*points, closed: false, smooth: false)`

Joins points, in order, with a line.

Each point is `[x, y]`. x grows to the right and y grows downwards, like on a screen, so `[40, -30]` is to the right and up.

An open path is a black line. With `closed: true` the last point joins the first and the shape is filled. With `smooth: true` the corners become a smooth curve.

The path is centred on its own bounding box, so it lines up with other shapes. With no points, you get a small example line.

```ruby
show path
```

```ruby
show path([0, 0], [40, -60], [80, 0], [120, -60], smooth: true).outlined(:crimson, width: 3)
```

```ruby
show path([0, 0], [40, -60], [80, 0], closed: true).colored(:orange)
```

#### `arc` — `arc(radius: 20, from: 0, to: 180)`

A piece of a circle, drawn as a line.

Angles are degrees, clockwise. 0 is straight up, so `from: 0, to: 180` is the right half.

The arc is centred on the middle of its circle, so arcs of the same circle line up.

```ruby
show arc
```

```ruby
show arc(radius: 30, from: 90, to: 360).outlined(:navy, width: 5)
```

#### `wave` — `wave(length: 100, height: 10, waves: 3)`

A smooth wavy line.

`waves` is how many full ups and downs fit in `length`. `height` is measured from the top of a crest to the bottom of a trough.

```ruby
show wave
```

```ruby
show wave(length: 120, height: 20, waves: 5).outlined(:teal, width: 3)
```

#### `ring` — `ring(radius: 20, thickness: 4)`

A doughnut. The hole is really a hole.

`thickness` is the width of the band, measured inwards from `radius`. What is behind the ring shows through the hole.

```ruby
show ring
```

```ruby
show ring(radius: 40, thickness: 12).colored(:crimson)
```

#### `heart` — `heart(size: 20)`

A heart.

`size` is the width of the whole heart.

```ruby
show heart.colored(:crimson)
```

```ruby
show heart(size: 60).colored(:hotpink)
```

#### `petal` — `petal(length: 20, width: 10)`

A leaf shape that starts at the centre and grows straight up.

Unlike the other shapes, a petal is not centred: its base is at the centre of the picture. That is on purpose. Rotate copies of it around the centre and you get a flower:

`petal.repeat(8).in_fan`

```ruby
show petal
```

```ruby
show petal(length: 40, width: 16).repeat(8).in_fan.colored(:crimson, :navy)
```
_A flower._

#### `moon` — `moon(size: 20)`

A crescent moon, opening to the right.

`size` is the height of the moon. Use `mirror` to open it to the left.

```ruby
show moon.colored(:gold)
```

```ruby
show moon(size: 60).mirror.colored(:ivory), on: :midnightblue
```

#### `drop` — `drop(size: 20)`

A water drop, with its point up.

`size` is the height of the drop.

```ruby
show drop.colored(:deepskyblue)
```

```ruby
show drop(size: 50).colored(:steelblue).flip
```

#### `cloud` — `cloud(size: 30)`

A soft cloud with a flat bottom.

`size` is the width of the cloud.

```ruby
show cloud.colored(:lightsteelblue)
```

```ruby
show cloud(size: 80).colored(:white), on: :skyblue
```

#### `space` — `space(width: 10, height: width)`

Empty room. You cannot see it, but it takes up space.

Use it to push things apart in `beside`, `above` or `in_row`. `space(20)` is a 20 by 20 gap.

```ruby
show circle.beside(space(30)).beside(circle)
```

```ruby
show square.above(space(width: 10, height: 40)).above(square)
```

#### `nothing` — `nothing`

The empty picture.

It has no size and takes up no room. It is handy inside `change_each`, to leave some pictures out. `nil` and `0` also become nothing with `to_art`.

```ruby
dots = circle(8).repeat(9).in_row(gap: 4)
show dots.change_each { |dot, t, i| i.even? ? dot : nothing }
```

#### `hsl` — `hsl(hue = 0, saturation = 70, lightness = 50)`

A colour made from hue, saturation and lightness.

`hue` is an angle from 0 to 360 (0 is red, 120 is green, 240 is blue). `saturation` and `lightness` go from 0 to 100.

Use it anywhere a colour is expected. It is the easiest way to make a rainbow.

```ruby
show circle(30).colored(hsl(200))
```

```ruby
dots = circle(8).repeat(12).change_each { |dot, t| dot.colored(hsl(t * 360, 80, 55)) }
show dots.in_ring
```
_t goes from 0 up to 1, so the hue goes once round._

#### `gray` — `gray(lightness = 50)`

A gray: 0 is black and 100 is white.

Use it anywhere a colour is expected.

```ruby
show circle(30).colored(gray(30))
```

```ruby
squares = square(24).repeat(5).change_each { |s, t| s.colored(gray(t * 100)) }
show squares.in_row.stroke(:black)
```
_Five grays from black towards white._

### Combine (`combine`)

Layer, repeat, and align pictures side by side or stacked—everything returns a single immutable picture.

#### `on` — `a.on(b)`

Put a on top of b.

The result is again one picture. `a` is in front.

`b` can be anything that can become a picture, such as a number. If either one is a motion, the result is a motion.

```ruby
show circle(8).colored(:gold).on(circle(20).colored(:navy))
```

```ruby
show star(radius: 20).colored(:gold).on(30)
```
_30 is a picture too: a circle of radius 30._

#### `under` — `a.under(b)`

Put b on top of a.

The opposite of `on`: what you write later is in front. `a + b` means the same.

```ruby
show circle(20).colored(:navy).under(circle(8).colored(:gold))
```

#### `+` — `a + b`

Put pictures on top of each other. What you write later is in front.

The same as `a.under(b)`. It is one of the only two symbols in asobi (the other is `*`).

```ruby
sun = circle(30).colored(:gold)
show sun + circle(14).colored(:orange) + circle(5).colored(:crimson)
```

#### `repeat` — `repeat(count = 2)`

Make copies of a picture.

The copies are all in the same place, so you cannot see the difference yet. Arrange them with `in_row`, `in_ring`, `in_fan` and the other arranging words.

The result is a group of pictures (a `Many`): one picture that is also a list. `a * 12` is the same as `a.repeat(12)`.

```ruby
show circle.repeat(5).in_row
```
_Five circles in a row._

```ruby
show star.repeat(times: 7).in_ring(radius: 40)
```

#### `*` — `a * count`

Make copies of a picture, like repeat.

`circle * 12` is `circle.repeat(12)`. It is one of the only two symbols in asobi (the other is `+`).

```ruby
hearts = heart * 6
show hearts.in_row(gap: 4).colored(:crimson)
```

#### `beside` — `a.beside(b, gap: 0)`

Put b to the right of a.

The result is one picture, centred as a whole. `gap` is the space between them.

```ruby
show circle.beside(square)
```

```ruby
show circle.colored(:crimson).beside(square.colored(:navy), gap: 8).beside(star)
```

#### `above` — `a.above(b, gap: 0)`

Put b under a, so that a is on top.

The result is one picture, centred as a whole. `gap` is the space between them.

```ruby
show circle.above(square)
```

```ruby
show text("asobi", size: 24).above(wave(60, 6, 3), gap: 6)
```

### Change (`change`)

Translate, rotate, scale, and mirror pictures around the origin without mutating the original value.

#### `move` — `move(right: 0, left: 0, up: 0, down: 0)`

Move a picture.

Say where to, not how to count: `right`, `left`, `up`, `down`. You never think about which way y goes.

A picture alone on the screen is always fitted to the middle, so moving it by itself shows nothing. Moving matters when there is something else to compare with, or when you will turn it around the centre (see `rotate`).

```ruby
show square(40).colored(:gold) + circle(8).colored(:navy).move(right: 20, up: 20)
```

```ruby
show circle(6).repeat(3).change_each { |dot, t, i| dot.move(right: i * 20, down: i * 10) }
```

#### `rotate` — `rotate(degrees = 45)`

Turn a picture clockwise.

Angles are degrees. `rotate(90)` is a quarter turn. To write a turn, use `0.25.turn`, which is 90.0.

Turning happens around the centre of the picture, the point where a fresh shape sits. So if you move a shape off the centre first and then rotate it, it swings around the centre. That is how `petal` and `in_fan` make a flower.

```ruby
show rect(50, 10).rotate(30)
```

```ruby
stick = rect(50, 8).repeat(6).change_each { |r, t| r.rotate(t * 180) }
show stick.colored(:teal).opacity(0.6)
```

#### `scale` — `scale(factor = 2, width: factor, height: factor)`

Make a picture bigger or smaller.

`scale(2)` makes it twice as big, `scale(0.5)` half as big. To stretch one way only, name the direction: `scale(width: 2, height: 1)`. A negative number flips the picture over.

`grow` and `shrink` say the same thing in words.

```ruby
show circle.scale
```
_No number: twice as big._

```ruby
show circle(20).scale(width: 2, height: 0.5).colored(:orange)
```

#### `grow` — `grow(to: 2)`

Make a picture bigger. A readable form of scale.

`grow(to: 3)` makes it three times as big. `grow(3)` works too.

```ruby
show star.grow
```

```ruby
show star.grow(to: 3).colored(:gold)
```

#### `shrink` — `shrink(to: 0.5)`

Make a picture smaller. A readable form of scale.

`shrink(to: 0.25)` makes it a quarter of the size. `shrink(0.25)` works too.

```ruby
show circle(30) + circle(30).shrink.colored(:crimson)
```

```ruby
show circle(30) + circle(30).shrink(to: 0.2).colored(:crimson)
```

#### `mirror` — `mirror`

Swap left and right.

Mirroring happens around the centre of the picture.

```ruby
leaf = petal.rotate(30)
show leaf.beside(leaf.mirror)
```

#### `flip` — `flip`

Swap top and bottom.

Flipping happens around the centre of the picture.

```ruby
show petal.beside(petal.flip)
```

### Colour (`color`)

Apply solid fills, rotating multi-colour palettes, linear or radial gradients, strokes, and opacity.

#### `colored` — `colored(*colors, from: nil, to: nil, angle: 0, around: false, all: false)`

Paint a picture, with one colour, several, or a gradient.

A colour is a CSS colour name as a symbol (`:crimson`, `:navy`, `:skyblue`), a string such as `"#ff0066"`, or the result of `hsl` or `gray`. With no colour at all you get `:crimson`.

Give a group of pictures several colours and they are used in turn, round and round: `petal.repeat(8).in_fan.colored(:crimson, :navy)`. A single picture takes the first colour.

Pictures are black by default, on a white page. If you paint the same picture twice, the later colour wins. A colour given to a whole group does not overwrite colours given to pictures inside it.

To repaint everything inside a group too, add `all: true`: `colored(:navy, all: true)`. Then line-only pictures such as `line` get their lines recoloured as well.

For a gradient, name where it starts and where it ends: `colored(from: :gold, to: :crimson)`. It runs from the top to the bottom. `angle:` turns the direction, in degrees clockwise: `angle: 90` runs from the right to the left. `around: true` runs from the centre outwards. If you leave out `from`, it starts at crimson; if you leave out `to`, it ends at navy.

On a group, the gradient flows once across the whole group, not once per picture. The colours inside the group give way to it. The gradient belongs to the picture: move it, turn it or resize it, and the gradient goes along.

```ruby
show heart.colored
```
_No colour: crimson._

```ruby
show square(20).repeat(6).in_row(gap: 4).colored(:gold, :teal, :crimson)
```

```ruby
show circle(40).colored(from: :gold, to: :crimson)
```
_A gradient, top to bottom: a sunset._

```ruby
show circle(40).colored(from: :white, to: :navy, around: true)
```
_around: true goes from the centre outwards._

```ruby
dots = circle(8).repeat(12).in_row(gap: 4)
show dots.colored(from: :gold, to: :crimson, angle: 90)
```
_Across a whole group, once. angle: 90 runs from right to left._

```ruby
scene = [star(radius: 16).colored(:gold), circle(12), line(length: 24)].to_art.in_row(gap: 8)
show scene.colored(:navy).above(scene.colored(:navy, all: true), gap: 12)
```
_Top: the gold star and the black line keep their colours. Bottom: all: true paints everything navy._

#### `outlined` — `outlined(color = :black, width: 1)`

Remove the fill and keep only the line.

Lines have no fill, so `outlined` is also how you set the colour and thickness of a `line`, `arc`, `wave` or `path`.

```ruby
show star.outlined
```

```ruby
show star(radius: 30).outlined(:crimson, width: 4)
```

#### `stroke` — `stroke(color = :black, width: 1)`

Keep the fill and add a line around it.

The difference from `outlined`: `outlined` throws the fill away, `stroke` keeps it.

```ruby
show heart(40).colored(:hotpink).stroke
```

```ruby
show heart(40).colored(:hotpink).stroke(:crimson, width: 4)
```

#### `opacity` — `opacity(value = 0.5)`

Make a picture see-through.

`0` is invisible and `1` is solid. Overlapping see-through pictures mix their colours.

```ruby
show circle(20).colored(:navy) + circle(20).colored(:crimson).opacity.move(right: 16)
```

```ruby
show circle(20).colored(:navy) + circle(20).colored(:crimson).opacity(0.2).move(right: 16)
```

### Effects (`effect`)

Add SVG glow, Gaussian blur, blend modes, drop shadows, and shape clipping masks.

#### `glowing` — `glowing(size: 4, color: nil)`

Make the edges glow.

The glow is the picture's own colour, blurred and spread outwards. Give `color:` to glow in another colour. `size` is in the same units as the picture. You can write it without its name: `glowing(8)`.

A glow needs a dark page to show: use `on: :black` or a dark blue. On white paper there is nothing to glow against.

The glow spills outside the picture, and the picture is fitted to the screen with the spill included. All the effect words work on motions too.

```ruby
petals = petal(40, 16).repeat(8).in_fan.colored(:hotpink, :gold)
show petals.glowing, on: :black
```

```ruby
show star(radius: 30).colored(:white).glowing(size: 12, color: :orange), on: :black
```
_A white star with an orange glow._

#### `blurred` — `blurred(amount: 2)`

Blur a picture.

`amount` is in the same units as the picture. You can write it without its name: `blurred(6)`.

The blur spills outside the picture, and the picture is fitted to the screen with the spill included. A big blur on a few shapes makes soft light, like an aurora.

```ruby
show circle(30).colored(:crimson).blurred, on: :black
```

```ruby
show star(radius: 30).colored(:gold).blurred(amount: 5), on: :navy
```

#### `blended` — `blended(mode = :screen)`

Choose how a picture mixes with what is under it.

Without a name, you get `:screen`: where pictures overlap, the colours get lighter, like lights shining on a wall. The other modes are `:multiply` (darker, like inks), `:overlay`, `:lighten`, `:darken`, `:difference` and `:normal` (no mixing).

`blended` changes how a picture mixes with what is under it. So it shows only when something is underneath, and the page colour counts: `:screen` on a white page does nothing, because white is already as light as it gets. Use `:screen` on a dark page and `:multiply` on a light one.

A wrong mode name gets an error that lists the right ones.

```ruby
red = circle(30).colored(:crimson).move(left: 14)
blue = circle(30).colored(:dodgerblue).move(right: 14)
show red.blended + blue.blended, on: :black
```
_Screen: where the circles overlap, the light adds up._

```ruby
yellow = circle(30).colored(:gold).move(left: 14)
cyan = circle(30).colored(:deepskyblue).move(right: 14)
show yellow + cyan.blended(:multiply)
```
_Multiply on white paper: where they overlap, the inks mix to green._

#### `shadowed` — `shadowed(down: 2, right: 0, blur: 3, color: :black, opacity: 0.4)`

Drop a soft shadow under a picture.

`down` and `right` say where the shadow falls, `blur` how soft it is, and `opacity` how dark. The first number can be written without its name: `shadowed(6)` is `shadowed(down: 6)`. Sizes are in the same units as the picture.

A shadow needs a light page to show. It makes pictures look like cut-out paper lying on top of each other.

```ruby
show star(radius: 30).colored(:tomato).shadowed, on: :linen
```

```ruby
show star(radius: 30).colored(:tomato).shadowed(down: 8, right: 6, blur: 6, opacity: 0.5), on: :linen
```

#### `clipped_by` — `clipped_by(shape = nil)`

Show a picture only through the window of a shape.

The shape is used where it is, so you can move it to choose which part shows. A number works as a round window: `clipped_by(8)` is `clipped_by(circle(8))`. With nothing, you get a round window, a bit smaller than the picture.

The shape can be anything that becomes a picture, even your own class. Combine it with a pattern, a gradient or a motion and the picture shows through the shape.

```ruby
stripes = rect(10, 80).repeat(10).in_row.colored(:crimson, :gold)
show stripes.clipped_by
```
_No shape: a round window._

```ruby
stripes = rect(10, 80).repeat(10).in_row.colored(:crimson, :gold)
show stripes.clipped_by(heart(60))
```

```ruby
show wave(120, 20, 4).outlined(:white, width: 8).clipped_by(circle(40)), on: :navy
```

### Arrange (`arrange`)

Distribute groups of pictures in rows, columns, grids, rings, fans, spirals, or stable random scatter.

#### `in_row` — `in_row(gap: 0)`

Put a group in a row, left to right.

`gap` is the space between neighbours. The whole row is centred.

Arranging words work on a group of pictures, which you get from `repeat` or from an array's `to_art`. They also work on one picture alone.

```ruby
show circle.repeat(5).in_row
```

```ruby
show star.repeat(5).in_row(gap: 6).colored(:gold)
```

#### `in_column` — `in_column(gap: 0)`

Put a group in a column, top to bottom.

`gap` is the space between neighbours. The whole column is centred.

```ruby
show circle.repeat(4).in_column
```

```ruby
show rect(40, 8).repeat(4).in_column(gap: 6).colored(:teal)
```

#### `in_grid` — `in_grid(columns: nil, gap: 0)`

Put a group in a table, filled row by row.

Without `columns`, the table is about as wide as it is tall. `gap` is the space between cells.

Every cell is as big as the biggest picture. You can write the number of columns without its name: `in_grid(4)`.

```ruby
show square.repeat(9).in_grid
```

```ruby
show circle(8).repeat(20).in_grid(columns: 5, gap: 4).colored(:crimson, :navy)
```

#### `in_ring` — `in_ring(radius: nil, facing_out: false)`

Put a group evenly round a circle, clockwise from the top.

Without `radius`, the circle is just big enough that neighbours do not touch. With `facing_out: true`, each picture turns so that its top points away from the centre.

You can write the radius without its name: `in_ring(60)`. Even one picture can go on a ring: it sits at the top.

```ruby
show circle(5).repeat(12).in_ring
```

```ruby
show triangle(14).repeat(10).in_ring(radius: 40, facing_out: true).colored(:orange)
```

#### `in_fan` — `in_fan(over: 360)`

Turn each picture a little more than the one before, around the centre.

Made for petals: a picture that sits off the centre, copied and fanned out, is a flower.

`over` is how many degrees the fan covers. With a full 360 the pictures go all the way round. With less, they spread evenly and the fan is centred on straight up.

```ruby
show petal.repeat(8).in_fan
```

```ruby
show petal(50, 8).repeat(7).in_fan(over: 120).colored(:teal)
```
_A fan of 120 degrees._

#### `in_spiral` — `in_spiral(spacing: nil)`

Put a group in a sunflower-seed spiral, from the centre outwards.

Each picture is turned by the golden angle from the one before, and moves a little further out. Without `spacing`, it is the size of the biggest picture.

You can write it without its name: `in_spiral(8)`.

```ruby
show circle(3).repeat(100).in_spiral
```

```ruby
show circle(2).repeat(200).in_spiral(spacing: 5).colored(:gold, :orange)
```

#### `scattered` — `scattered(over: nil, seed: nil)`

Put a group in random places.

Without `over`, the area is chosen from how many pictures there are and how big they are. `over: 200` is a 200 by 200 square. `over: [300, 100]` is 300 wide and 100 tall. The area is centred.

Each run looks different. But while the live view is running, it stays put: saving the file again does not shuffle the pictures. Write `seed: 7` (any whole number) to get the same picture every time, on every machine.

```ruby
show circle(3).repeat(30).scattered
```

```ruby
show star(radius: 4).repeat(40).scattered(over: [200, 100], seed: 7).colored(:gold), on: :midnightblue
```

#### `change_each` — `change_each { |one, t, i| ... }`

Change the pictures of a group one at a time.

The block gets each picture (`one`), how far along it is (`t`: 0.0 for the first, creeping up towards 1.0 for the last) and its number (`i`, from 0). It answers the changed picture.

The pictures stay where they are, so `change_each` can come before or after the arranging words. One thing to know: moving, turning and scaling happen around the centre of the whole picture. If you want each picture to turn or grow around itself, do it before arranging, as in the example.

```ruby
dots = circle(6).repeat(10).change_each { |dot, t| dot.grow(to: 1 + t * 2) }
show dots.in_row(gap: 4)
```

```ruby
tiles = square(18).repeat(25).change_each { |s, t, i| s.rotate(t * 90).colored(i.even? ? :tomato : :navy) }
show tiles.in_grid(5, gap: 4)
```

### Object to Art (`to_art`)

Object to Art: convert numbers, strings, arrays, ranges, hashes, and your own Ruby classes into composable pictures.

#### `to_art` — `to_art(as: nil)`

Ask any object to become a picture.

This is the heart of asobi: treat objects as pictures, and pictures as objects.

What each thing becomes:

A number is a circle of that radius. A negative number is the same circle, as a line only. `0` is nothing.

With `as:` you hand over the shape, and the number says how big it should be (half of its larger side). `8.to_art(as: star)` is a star of radius 8.

An array or a range turns each item into a picture and puts them in a row, with a small gap. `as:` goes to every item. An array of arrays is a table: each inner array is a row. A string or a symbol is its letters, like `text`. A hash makes one row per pair: the key's picture beside the value's picture. `nil` is nothing.

An array that holds something in motion becomes a motion.

You do not need to call `to_art` yourself to show things: `show` and the words `on`, `under`, `beside` and `above` ask for it for you.

Your own class only has to write a `to_art` method (see the last example). Add `include Asobi::Art` to the class when you want to call picture words directly on its objects, such as `planet.rotate(20)`. This is like `Enumerable`, which asks only for `each`.

```ruby
show 20.to_art
```
_A number: a circle._

```ruby
show [3, 6, 9, -12, 0, 12].to_art
```
_An array: a row. A negative number is only a line. Zero is empty._

```ruby
show [1, 2, 3, 5, 8].to_art(as: star).colored(:gold, :orange), on: :black
```
_as: gives the shape; the numbers give the sizes._

```ruby
hearts = (1..5).to_art(as: heart)
show hearts.colored(:crimson)
```
_A range works like an array._

```ruby
show [[1, 2, 3], [4, 5, 6], [7, 8, 9]].to_art(as: square).colored(:teal, :navy)
```
_An array of arrays: a table._

```ruby
show "asobi".chars.to_art.colored(:crimson, :navy)
```
_A string: its letters. Here, split into one picture per letter._

```ruby
show({ 3 => 12, 5 => 6 }.to_art.colored(:teal))
```
_A hash: one row for each key and value._

```ruby
class Planet
  def initialize(size, color)
    @size = size
    @color = color
  end

  def to_art
    circle(@size).colored(@color)
  end
end

show [Planet.new(6, :tomato), Planet.new(10, :gold), Planet.new(4, :skyblue)].to_art
```
_Your own class: write to_art, and arrays and show can use it._

```ruby
class Planet
  include Asobi::Art

  def initialize(size, color)
    @size = size
    @color = color
  end

  def to_art
    circle(@size).colored(@color)
  end
end

show Planet.new(10, :gold).spin(every: 4).move(right: 20).on(40)
```
_With include Asobi::Art, picture words work on the object itself._

### Motion (`motion`)

Pure-function animations (`seconds -> art`) that loop seamlessly, stagger `one_by_one`, and leave fading trails.

#### `spin` — `spin(every: 4, clockwise: true)`

Turn round and round.

`every` is the seconds for one full turn. With `clockwise: false` it turns the other way.

A motion is a function from time to a picture. It has no frame counter and no memory, so the same moment is always the same picture. Motion words can follow each other: `flower.spin(every: 8).pulse(every: 1)`. All the picture words work on a motion too: `flower.spin.colored(:crimson)`.

The loop length of a combination is the least common multiple of the parts (or the longest part, if that would be more than a minute).

```ruby
show star.spin
```

```ruby
show star(radius: 30).spin(every: 2, clockwise: false).colored(:gold)
```

```ruby
flower = petal(40, 16).repeat(8).in_fan.colored(:crimson, :navy)
show flower.spin(every: 8).pulse(every: 1)
```
_Two motion words, one after the other._

#### `pulse` — `pulse(every: 1, to: 1.2)`

Swell up and come back.

`to` is how big it gets, as a multiple (`to: 2` is twice as big). `every` is the seconds for one pulse. You can write `every` without its name: `pulse(2)`.

```ruby
show heart.colored(:crimson).pulse
```

```ruby
show heart(40).colored(:crimson).pulse(every: 0.8, to: 1.5)
```

#### `sway` — `sway(every: 2, by: 10)`

Rock to one side and the other.

`by` is how many degrees it rocks each way. `every` is the seconds for one swing there and back.

It rocks around the centre of the picture. To make something hang from a point, put that point at the centre: `petal.flip.sway` swings like a bell.

```ruby
show petal(40, 16).flip.colored(:teal).sway
```

```ruby
show petal(40, 16).repeat(3).in_fan(over: 90).colored(:crimson).sway(every: 3, by: 20)
```

#### `orbit` — `orbit(radius: nil, every: 4)`

Go round a circle.

The picture itself does not turn. It travels round a circle of `radius`, clockwise, starting from the top. Without `radius`, the circle is as big as the picture's larger side.

You can write the radius without its name: `orbit(40)`.

```ruby
show circle(5).colored(:crimson).orbit
```

```ruby
show circle(10).colored(:gold) + circle(4).colored(:skyblue).orbit(radius: 30, every: 3), on: :black
```

#### `fade` — `fade(every: 2, from: nil, to: 0.2)`

Fade out and come back.

`to` is how faint it gets: `0` is invisible and `1` does nothing. Pass `from:` to fade from `from` to `to` (default `1`) and back, replacing any opacity already on the picture—handy for `fade(from: 0, to: 1).by(:mic)`.

```ruby
show circle(20).colored(:navy).fade
```

```ruby
show star(radius: 20).repeat(6).in_ring.colored(:gold).fade(every: 3, to: 0).one_by_one, on: :black
```

```ruby
show circle(24).colored(:cyan).fade(from: 0, to: 1).by(:mouse_x), on: :black
```
_With from: 0, to: 1, fade in from invisible._

#### `flash` — `flash(every: 0.5, from: 0, to: 1)`

Stay hidden at rest, flash bright on start, and fade out.

At rest (the start of the loop) the picture sits at `from` (`0`, invisible, by default). As soon as it plays it jumps up to `to` (`1`) and fades smoothly back down to `from`.

Built for beats, clicks and keys (`on_beat`, `when_clicked`, `when_pressed`), and blinks on the clock too. Pass `from: 0.15` to keep a faint glow at rest.

```ruby
show star(radius: 24).colored(:gold).flash(every: 0.8), on: :black
```
_Blinking on the clock._

```ruby
play "beat.wav"
ring_hit = ring(radius: 40, thickness: 4).colored(:cyan).flash(every: 0.35).on_beat(:music)
show circle(10).colored(:deeppink) + ring_hit.glowing(6), on: :black
```
_Flashing on every beat of the music._

#### `bounce` — `bounce(height: 20, every: 1)`

Jump up and fall back, like a ball.

`height` is how high it jumps. It is slow at the top and fast near the ground. You can write the height without its name: `bounce(40)`.

```ruby
show circle(8).colored(:tomato).bounce
```

```ruby
show circle(6).repeat(10).in_row(gap: 4).colored(:teal).bounce(height: 30, every: 2).one_by_one
```

#### `drift` — `drift(right: nil, left: nil, up: nil, down: nil, every: 4)`

Go somewhere and come back, smoothly.

Name the way, like `move`: `drift(right: 40)`. With nothing named, it drifts to the right by the width of the picture.

```ruby
show circle.colored(:orange).drift
```

```ruby
show square(20).colored(:navy).drift(right: 30, up: 20, every: 3)
```

#### `shift_color` — `shift_color(from: nil, to: :crimson, every: 4, all: false)`

Slowly change colour and come back.

Give it two colours, `shift_color(:navy, :gold)`, to go from the first to the second and back. Give it one colour, `shift_color(:gold)`, to go from black to that colour. With nothing, it goes from black to crimson.

You can also name them: `shift_color(from: :navy, to: :gold, every: 6)`.

On a group, pictures inside that have a colour of their own keep it. Add `all: true` to shift them too, as with `colored`.

```ruby
show circle(20).shift_color
```

```ruby
show heart(40).shift_color(:navy, :hotpink, every: 3)
```

#### `one_by_one` — `one_by_one(gap: nil)`

Make a group move one at a time, instead of all together.

Write it right after a motion word (`spin`, `pulse`, `bounce` and so on) on a group of pictures. Each picture does the motion a little after the one before. By default, the loop is shared out evenly, and that makes a wave.

`gap` is the seconds between one picture and the next. Each picture does the motion around its own centre, so a ring of dots that pulses one by one stays a ring.

`one_by_one` belongs right after the motion word it staggers. If you stagger two motions, write it after each of them, as in the hearts example below: one for the colour, one for the bounce.

```ruby
show circle(6).repeat(12).in_ring(40).colored(:crimson).pulse(every: 1.5, to: 1.8).one_by_one
```

```ruby
show circle(5).repeat(16).in_row(gap: 5).colored(:teal).bounce(height: 20, every: 2).one_by_one(gap: 0.1)
```

```ruby
hearts = heart(20).repeat(5).in_row(gap: 6)
show hearts.shift_color(:crimson, :orchid).one_by_one.bounce(height: 24).one_by_one
```
_Two motions, so two one_by_one: one right after shift_color, one right after bounce._

#### `trail` — `trail(count: 5, gap: nil)`

Leave fading copies of earlier moments behind a moving picture.

The copies come from `gap` seconds earlier, then twice that, and so on, each fainter than the last, drawn underneath. Without `gap` it is a short moment, which looks good. `count` is how many copies. You can write it without its name: `trail(10)`.

On a still picture it does nothing: there is no earlier moment. Each copy is a whole picture, so a big picture with a long trail makes a heavy animation.

```ruby
show circle(5).colored(:gold).orbit(30).trail, on: :black
```

```ruby
show circle(5).colored(:hotpink).orbit(radius: 30, every: 3).trail(count: 12).glowing, on: :black
```
_A long trail, with a glow._

#### `animate` — `animate(seconds = 4) { |picture, t| ... }`

Make your own motion.

The block gets the picture and `t`, which runs from 0.0 up to (not including) 1.0 and then starts again, once every `seconds` seconds. It answers the picture for that moment.

Use the other words inside the block. Anything they can do, your motion can do.

```ruby
show star.animate(4) { |picture, t| picture.rotate(t * 360).grow(to: 1 + t) }
```

```ruby
dots = circle(5).repeat(8).in_ring(30)
show dots.animate(3) { |ring, t| ring.change_each { |dot, u| dot.colored(hsl((t + u) * 360)) } }
```
_Colours run round the ring._

#### `at` — `at(seconds = 0)`

The picture a motion shows at one moment. Also: every, frames.

`motion.at(1.5)` is the still picture 1.5 seconds in. Time is in seconds, and it repeats: a motion with `every` of 4 shows the same picture at 0.5 and at 4.5.

`motion.every` is how many seconds one loop takes. `motion.frames(fps: 30)` is one loop as a lazy list of pictures, at 30 pictures a second.

You never need them to show a motion: just `show` it.

```ruby
show circle(5).colored(:crimson).orbit(radius: 30, every: 4).at(1)
```
_One moment: a quarter of the way round._

```ruby
trail = circle(3).colored(:navy).orbit(radius: 30, every: 4).frames(fps: 6)
show trail.reduce(:+)
```
_Every frame on top of each other draws the path._

### React and sound (`react`)

Let a click, the pointer, a key, the microphone or a song turn a motion, and make notes and drum sounds as it does.

#### `when_clicked` — `when_clicked`

Play a motion once when the picture is clicked.

Write it right after a motion. Until you click, the picture rests on the first moment of the motion. A tap on a phone works too.

The answer is a picture, so the other words still work on it. With `repeat`, only the copy you click plays.

```ruby
show heart(30).colored(:crimson).pulse(every: 0.5, to: 1.4).when_clicked
```
_Click the heart._

```ruby
dot = circle(8).colored(:teal).pulse(every: 0.4, to: 1.8).when_clicked
show dot.repeat(12).in_ring(40)
```
_Only the dot you click plays._

#### `while_hovered` — `while_hovered`

Play a motion while the pointer is over the picture.

Write it right after a motion. The motion plays round and round while the pointer is over the picture. When the pointer leaves, it goes back to the first moment.

```ruby
twinkle = star(radius: 14).colored(:gold).spin(every: 1).while_hovered
show twinkle.repeat(5).in_row(gap: 12)
```
_Move the pointer along the stars._

#### `when_pressed` — `when_pressed(key = nil)`

Play a motion once when a key is pressed.

Write it right after a motion. Name the key as a string, like `when_pressed("a")` or `when_pressed("ArrowUp")`, or `:space` for the space bar. Leave it out, and any key plays it.

Holding a key down plays it once. Every copy of the picture plays together.

```ruby
show circle(25).colored(:dodgerblue).bounce(height: 30, every: 0.6).when_pressed("a")
```
_Press a._

#### `on_beat` — `on_beat(from = :mic)`

Play a motion once on every beat.

Write it right after a motion. A beat is a sudden low sound, like a kick drum or a knock on the table.

`:mic` (the default) listens to the microphone. Click the picture once, and the browser asks if asobi may listen. `:music` listens to the sound asobi makes itself: its song (`play`) and its sounds (`sounding`).

```ruby
show ring(radius: 40, thickness: 4).colored(:deeppink).pulse(every: 0.4, to: 1.3).on_beat, on: :black
```
_Click once, allow the microphone, then knock on the table._

#### `by` — `by(from, what = nil)`

Let a value choose the moment of a motion.

Write it right after a motion. The value goes from 0 to 1, and the motion follows it instead of the clock.

`from` is where the value comes from. `:mouse_x` is the pointer from left (0) to right (1). `:mouse_y` is from top (0) to bottom (1). `:mic` is the microphone, and `:music` is the sound asobi makes itself (`play` and `sounding`).

For sound, `what` chooses what to follow: `:low` (the bass), `:mid`, `:high`, or `:pitch` (how high the sound is). Leave it out to follow the loudness.

A motion that goes and comes back, like `pulse`, only goes: a bigger value takes it further. A motion that goes one way, like `spin`, goes once round from 0 to 1.

```ruby
flower = petal(40, 14).repeat(12).in_fan.colored(:crimson, :navy)
show flower.spin.by(:mouse_x)
```
_Move the pointer from left to right._

```ruby
burst = star(points: 8, radius: 30, inner: 12).colored(:orange)
show burst.pulse(to: 1.6).by(:mic, :low), on: :black
```
_Click once, allow the microphone, and play some music._

#### `sounding` — `sounding(sound)`

Make a sound each time a reaction plays.

Write it right after `when_clicked`, `while_hovered`, `when_pressed` or `on_beat`. It cannot follow `by`: a value has no moment to make a sound.

There are six sounds by name: `:pop`, `:kick`, `:snare`, `:hat`, `:clap` and `:bell`. A note is a letter from a to g, then `s` for sharp or `b` for flat if you like, then an octave from 1 to 7: `:c4` is middle C, `:cs4` is C sharp. A string works too: `"C#4"`, `"Bb3"`.

The sound starts after the first click on the picture. Browsers allow sound only after you click.

```ruby
show circle(20).colored(:tomato).pulse(every: 0.3, to: 1.5).when_clicked.sounding(:pop)
```
_Click for a pop._

```ruby
bars = %i[c4 e4 g4 c5].each_with_index.map do |note, i|
  rect(18, 40 + i * 10).colored(:teal).pulse(every: 0.3, to: 1.2).when_clicked.sounding(note)
end
show bars.to_art.in_row(gap: 8)
```
_Four bars, four notes._

#### `play` — `play(file, volume: 1)`

Play a song while the sketch shows, round and round.

Write it on a line of its own, like `show`. Put the file next to your sketch. It can be .mp3, .wav, .m4a, .aac, .ogg, .opus, .flac or .webm. `volume:` goes from 0 to 1.

A sketch has one song, which loops. A later `play` replaces it, and taking the line out stops it. Change the rest of the code and the song carries on.

The sound starts after the first click on the picture. `by(:music)` and `on_beat(:music)` follow the song.

Here and in the playground there is one song to try: `"beat.wav"`, a short drum loop. Your own songs work when you run asobi on your computer.

```ruby
play "beat.wav"
burst = star(points: 8, radius: 30, inner: 12).colored(:orange)
show burst.pulse(to: 1.6).by(:music, :low), on: :black
```
_Click once to start the song. The star swells with the bass._

```ruby
play "beat.wav"
show ring(radius: 40, thickness: 4).colored(:cyan).pulse(every: 0.4, to: 1.3).on_beat(:music), on: :black
```
_The ring jumps on every beat of the song._

### Show (`show`)

Display live-reloading sketches in the browser, export SVG or standalone HTML, and measure bounding boxes.

#### `show` — `show(picture, on: :white, paper: nil, grain: nil, controls: true)`

Show a picture (or a motion) on screen.

The picture is fitted to the screen, so you never have to think about the size of the window.

`on:` is the background colour. `paper: [800, 800]` shows a paper of that size (width and height) with the picture in the middle; anything that sticks out is cut off.

`grain:` lays a fine film grain over the whole picture, like paper or old film. `grain: true` is a gentle grain (0.12), and a number from 0 to 1 sets the strength: `grain: 0.3`. It belongs to the way the picture is shown, not to the picture, so it covers everything, background included.

If you call `show` several times, the last one is what appears.

Run a sketch with `ruby flower.rb` or `asobi flower.rb` and a browser opens. Save the file, and the picture changes. Press Ctrl-C to stop. A mistake in your code does not close anything: the last good picture stays, and the error is written below it, with a hint.

Move the mouse and small controls appear at the edge for a few seconds. Press `c` to lay the code over the picture, and `r` to start and stop recording a video (the browser saves the file). A recording has asobi's own sound in it too (`play` and `sounding`), but never the microphone.

Press `f` for full screen, and `m` to mute asobi's sound. Muting quiets only the speakers: the pictures still follow the sound, and a recording still has it.

`controls: false` keeps the controls hidden, even when the mouse moves. It suits a picture on a big screen. The keys still work.

```ruby
show star(radius: 30).colored(:gold)
```

```ruby
show star(radius: 30).colored(:gold), on: :midnightblue
```
_A background colour._

```ruby
show circle(40).colored(:crimson), paper: [300, 200]
```
_A 300 by 200 paper. The circle is cut off at the edge._

```ruby
show star(radius: 30).colored(:gold).glowing, on: :black, grain: true
```
_A film grain over everything. Try grain: 0.3 for a rougher one._

```ruby
show star(radius: 30).colored(:gold).spin(every: 6).glowing, on: :black, controls: false
```
_No controls, even when the mouse moves._

#### `save` — `save(path, on: :white, paper: nil, grain: nil, controls: true)`

Write a picture to a file.

A still picture goes to an SVG file: `flower.save("flower.svg")`. A motion goes to an HTML page that plays by itself in any browser: `flower.spin.save("flower.html")`. Both accept `on:`, `paper:` and `grain:` as `show` does: `flower.save("flower.svg", grain: true)`.

To get one moment of a motion as SVG, ask for it with `at`: `flower.spin.at(1).save("still.svg")`.

A picture that reacts keeps reacting in a saved `.html` page: `dot.pulse.when_clicked.save("dot.html")`. If the sketch has a `play` line above, the song goes inside the page, so the page plays it anywhere (a song of up to 20 MB). A `.svg` has no sound and does not react.

```ruby
flower = petal(40, 16).repeat(8).in_fan.colored(:crimson, :navy)

# Remove the "#" to write the file:
# flower.save("flower.svg")

show flower
```
_The line starting with # is a comment; Ruby skips it._

#### `to_svg` — `to_svg(on: :white, paper: nil)`

The picture as SVG text.

It answers a string. `save("x.svg")` writes the same text to a file. It works on still pictures. For a motion, take a moment with `at` first.

```ruby
svg = circle(10).colored(:crimson).to_svg
show text("#{svg.length} characters of SVG", size: 8)
```

#### `bounds` — `bounds, width, height`

Measure a picture.

`width` and `height` are the size of the smallest box around the picture. `bounds` is that box, with `x0`, `y0`, `x1` and `y1`. For a motion, they cover everything it shows over one loop.

```ruby
shape = star(radius: 20)
show text("#{shape.width.round(1)} x #{shape.height.round(1)}", size: 12)
```

#### `turn` — `turn`

An angle written as a number of turns.

Angles in asobi are degrees, clockwise. `0.25.turn` is 90.0, so `rotate(0.25.turn)` is a quarter turn. `turns` means the same: `2.turns` is 720.0.

```ruby
show rect(50, 10).colored(:navy).on(rect(50, 10).colored(:crimson).rotate(0.25.turn))
```

---

## Examples Gallery (16 Sketches)

### Flower (`flower.rb`)
A petal, copied and fanned out. Two colours take turns.

```ruby
require "asobi"

flower = petal(40, 16).repeat(8).in_fan.colored(:crimson, :navy)
bloom = flower + circle(5).colored(:gold)
show bloom.spin(every: 8)
```

### Night sky (`night_sky.rb`)
Stars are scattered at random, glow, and twinkle one after another. Each run is different.

```ruby
require "asobi"

stars = star(radius: 4).repeat(40).change_each { |s, t| s.grow(to: 0.6 + t) }
sky = stars.colored(:gold).scattered(over: [320, 180]).glowing.fade(every: 3, to: 0.4).one_by_one
show sky + moon(size: 40).colored(:ivory).glowing(8).move(right: 110, up: 50), on: :midnightblue
```

### Wave of dots (`dot_wave.rb`)
One bounce, started a little later for each dot.

```ruby
require "asobi"

dots = circle(6).repeat(16).in_row(gap: 6).colored(:teal)
show dots.bounce(height: 30, every: 2).one_by_one
```

### Ring of dots (`ring_pulse.rb`)
Each dot pulses around its own centre, so the ring keeps its shape.

```ruby
require "asobi"

dots = circle(4).repeat(24).in_ring(60).colored(:crimson, :orange)
show dots.pulse(every: 2, to: 2).one_by_one
```

### Concentric rings (`rings.rb`)
One ring, copied and shrunk a little more each time.

```ruby
require "asobi"

rings = ring(80, 4).repeat(10).change_each { |r, t| r.scale(1 - t * 0.9) }
show rings.colored(:navy, :skyblue).pulse(every: 2, to: 1.1).one_by_one
```

### A number sequence (`sequence.rb`)
The Fibonacci numbers, asked to be stars. The numbers become the sizes.

```ruby
require "asobi"

fib = [1, 1, 2, 3, 5, 8, 13, 21]
show fib.to_art(as: star).colored(:gold, :orange), on: :black
```

### Letters (`typography.rb`)
A word split into letters. Each letter is bigger than the last.

```ruby
require "asobi"

letters = "asobi".chars.to_art.change_each { |c, t| c.grow(to: 1 + t * 2) }
show letters.in_row(gap: 4).colored(:crimson, :navy)
```

### Tiles (`tiles.rb`)
A grid of squares. Each one is turned and shrunk a little more.

```ruby
require "asobi"

tiles = square(18).repeat(36).change_each { |s, t| s.rotate(t * 90).grow(to: 1 - t * 0.5) }
show tiles.in_grid(6, gap: 4).colored(:tomato, :navy)
```

### Bouncing hearts (`hearts.rb`)
Colour and bounce, both one by one.

```ruby
require "asobi"

hearts = heart(20).repeat(5).in_row(gap: 6)
show hearts.shift_color(:crimson, :orchid).one_by_one.bounce(height: 24).one_by_one
```

### Solar system (`planets.rb`)
Your own class, Planet, only has to say what it looks like.

```ruby
require "asobi"

class Planet
  def initialize(size, distance, color, year)
    @size, @distance, @color, @year = size, distance, color, year
  end

  def to_art
    ring(@distance, 0.4).colored(:dimgray) +
      circle(@size).colored(@color).orbit(@distance, every: @year)
  end
end

sun = circle(10).colored(:gold)
planets = [Planet.new(3, 22, :skyblue, 3), Planet.new(5, 38, :tomato, 7), Planet.new(4, 54, :orchid, 12)]
show planets.reduce(sun) { |system, planet| system + planet }, on: :black
```

### Sunflower spiral (`spiral.rb`)
300 seeds at the golden angle, growing and changing colour as they go out.

```ruby
require "asobi"

seeds = circle(3).repeat(300).change_each { |d, t| d.grow(to: 0.4 + t).colored(hsl(20 + t * 40)) }
show seeds.in_spiral(5)
```

### Neon flower (`neon_flower.rb`)
Two bright colours on black. Glow, screen blending and a film grain make it shine.

```ruby
require "asobi"

petals = petal(50, 18).repeat(8).in_fan.colored(:deeppink, :cyan)
spinning = petals.spin(every: 4)
show spinning.trail(4).glowing.blended(:screen), on: :black, grain: true
```

### Aurora (`aurora.rb`)
Three wavy lines, blurred, with screen blending, drifting slowly.

```ruby
require "asobi"

ribbon = wave(240, 30, 2).outlined(:springgreen, width: 14)
bands = ribbon.repeat(3).change_each { |w, t, i| w.move(down: i * 14).colored(hsl(150 + i * 50)) }
show bands.drift(right: 40, every: 6).blurred(4).blended(:screen), on: :midnightblue, grain: true
```

### Paper cut-outs (`paper_cutouts.rb`)
Flat shapes, each with a soft shadow, on a light page: they look like paper lying on paper.

```ruby
require "asobi"

sun = circle(22).colored(:gold).move(up: 20, right: 40)
far = ellipse(240, 80).colored(:lightseagreen).move(down: 45, left: 30)
near = ellipse(220, 70).colored(:seagreen).move(down: 62, right: 40)
show [sun, far, near].map(&:shadowed).reduce(:+), on: :oldlace
```

### Through a heart (`through_a_heart.rb`)
Gradient stripes spin behind a heart-shaped window.

```ruby
require "asobi"

stripes = rect(10, 90).repeat(12).in_row(gap: 4).colored(from: :crimson, to: :gold, angle: 90)
show stripes.spin(every: 6).clipped_by(heart(70)), on: :navy
```

### Chimes (`chimes.rb`)
Eight notes in a ring, and a bell in the middle. Click them to play a tune: in any order, they sound good together.

```ruby
require "asobi"

notes = %i[c4 d4 e4 g4 a4 c5 d5 e5]
chimes = notes.each_with_index.map do |note, i|
  chime = circle(13).colored(hsl(i * 45, 75, 62))
  chime.pulse(every: 0.5, to: 1.4).when_clicked.sounding(note)
end
bell = star(points: 8, radius: 22, inner: 9).colored(:gold).spin(every: 1).when_clicked.sounding(:bell)
music_box = chimes.to_art.in_ring(58) + bell
show music_box.glowing, on: :midnightblue
```

---

## Frequently Asked Questions

**Q: What is asobi?**
asobi (Japanese for “play”) is a zero-dependency Ruby 3.2+ gem for creative coding, generative SVG art, and looping animations that can react to clicks, the pointer, keys, a microphone, or a song. You can run sketches locally with live browser reload or try them in the browser playground powered by ruby.wasm.

**Q: What does “Object to Art. Art to Object.” mean?**
Object to Art means any Ruby value—numbers, strings, arrays, hashes, or your own classes defining `to_art`—can become a picture. Art to Object means the returned picture is itself an immutable Ruby object (`Asobi::Art`), so you can store it in variables, pass it to methods, put it back into arrays, and chain layouts or animations without canvas state bugs.

**Q: How is asobi different from p5.js, Processing, or Ruby 2D?**
p5.js and Processing draw imperatively inside `setup` and `draw` loops with mutable canvas state (`fill`, `translate`, `push`/`pop`), and Ruby 2D mutates drawable objects on an SDL window. asobi has no canvas state and no native library dependencies: every shape and motion is an immutable Ruby value composed with English-like words.

**Q: Can asobi react to clicks, music, or a microphone, and can it make sound?**
Yes. A word after a motion says who turns its frames instead of the clock: `when_clicked`, `while_hovered`, `when_pressed("a")`, `on_beat`, and `by(:mouse_x)`, `by(:mic, :low)` or `by(:music)`. `play "song.mp3"` plays a song in a loop, and `sounding(:kick)` or `sounding(:c4)` after a reaction plays a drum sound or a note made in the browser, with no sound files. Editing the code does not restart the song, so it suits live coding and VJ sets, and a saved HTML page still reacts and carries its song.

**Q: Do I need external tools like ImageMagick, SDL, or Node.js?**
No. asobi uses only the Ruby standard library (Ruby 3.2 or newer). Run `gem install asobi` and `asobi` to start live coding in your browser, or open the online playground with zero installation.

**Q: How do I turn my own Ruby class into art, and what formats can I export?**
Define a `to_art` method returning any asobi picture on your class (and optionally `include Asobi::Art` to call methods like `spin` or `colored` directly on instances). You can save stills as clean SVG files with `save` (`art.svg`), save looping animations as standalone HTML (`art.html`), or record MP4/WebM video in the live browser view.
