Project

asobi

0.0
The project is in a healthy, maintained state
asobi (Japanese for 'play') is a Ruby library for creative coding and generative art with zero dependencies: it makes pictures and looping animations. A picture is a Ruby object: repeat a petal eight times, arrange the petals into a flower, and spin the whole flower. Give your own objects a visual form by defining to_art. Let a motion react to a click, the pointer, a key, the microphone or a song, and make notes and drum sounds, for live coding and VJ sets. Run the asobi command to see your sketch update in the browser as you edit. Export a still as SVG, or an animation as a standalone HTML file that still reacts and carries its song.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme
asobi

English · 日本語

Object to Art. Art to Object.

asobi (Japanese for “play”) is a zero-dependency creative coding gem for making geometric pictures and looping animations in Ruby.
Turn Ruby values and your own classes into pictures with to_art — Object to Art. Keep, compose, and animate every picture as an immutable Ruby object — Art to Object.

Gem Version Downloads Ruby 3.2 or newer MIT License PRs welcome Featured in Ruby Weekly #827

Try it in your browser Guide Reference Examples 32-second film


Gold, pink, cyan, orange and violet fireworks open one after another in a dark night sky, then fade.

The fireworks above are drawn by the Ruby below, exactly as written. It makes one spark, fans copies of it into a burst, scatters six bursts across the sky, and opens them one by one.

require "asobi"

spark = petal(15, 1.8).move(up: 28)
burst = spark.repeat(26).in_fan + spark.repeat(13).in_fan.scale(0.5)
colors = %i[gold deeppink cyan orange violet]
sky = burst.repeat(6).change_each { |b, t, i| b.scale(0.75 + i % 3 * 0.3) }
           .scattered(over: [290, 130], seed: 5).colored(*colors)
bloom = sky.animate(3) { |b, t| b.scale(0.05 + t**0.35).opacity(1 - t**4) }

show bloom.one_by_one.trail(4).glowing(3), on: "#070812", paper: [360, 200]

Made with asobi

Every piece below is one Ruby file that runs as it is. Select a picture to read its code.

Thin cyan, magenta and gold hexagons rush out of a dark centre, twisting, while a light sweeps round a fan of violet rays. Hundreds of hairline ellipses in cyan, violet and pink fold into ribbons that wave around the centre.
Neon tunnel
24 hexagons fly out one_by_one · 7 lines
Lace
132 hairline ellipses folding in waves · 6 lines
A ring of 240 thin bars, cyan to violet to pink, leaps like a music visualiser, with a faint echo of itself around it. Eleven hundred gold-to-violet seeds on a sunflower spiral wind into turning galaxy arms over twinkling white stars.
Spectrum without a song
240 bars in_fan, each on its own beat · 10 lines
Galaxy
1,100 seeds in_spiral, winding into arms · 10 lines
Eight rings of cyan and pink letters reading RUBY IS PLAY, ASOBI and あそび surround a glowing あそび; waves of light run round them. A wall of 2,040 small round lights, pink to gold, turns a three-armed spiral of light.
Type poster
840 letters on eight rings, lit by waves · 14 lines
LED wall
2,040 lights, each a moment behind the next · 8 lines
On the left, the Ruby source of the sketch. On the right, that same source set in glowing rings of letters around a pink heart, with bands of light sweeping round to the music.
Self-portrait
reads its own code and draws every line as a ring of letters that follows the music · 18 lines

Contents

  • Made with asobi
  • How asobi thinks
  • Run your first sketch
  • Why asobi
  • Key features
  • Sketch gallery
  • Every word at a glance
  • Export and record
  • Use asobi from an existing Ruby program
  • FAQ · Documentation · Development · Issues and pull requests

How asobi thinks

Object to Art
Numbers become discs, strings become type, arrays and ranges become visual rows, and your own classes gain visual form through to_art. Once converted, they behave like any other picture.
Art to Object
Every picture is an immutable Ruby object. Hold it in a variable, pass it to a method, or reuse it across a composition — flower.scale(0.6) returns a new flower and leaves the original unchanged, with no setup or draw loop to wire up.
Reads like English
petal.repeat(8).in_fan.colored(:crimson, :navy) reads from left to right: repeat a petal eight times, fan the copies out, colour them crimson and navy.
Nothing but Ruby
Zero external dependencies. Run gem install asobi and asobi to live-code in your browser with automatic reload, or run sketches straight in the browser playground.

Run your first sketch

For a local sketch, you'll need Ruby 3.2 or newer and a browser. Run these commands in the folder where you'd like to keep your sketch.

gem install asobi
asobi

asobi creates play.rb with the sketch below and opens the spinning flower in your browser. If play.rb already exists, it opens that sketch.

require "asobi"

flower = petal.repeat(8).in_fan.colored(:crimson, :navy)
show flower.spin(every: 8)
A flower of eight crimson and navy petals turns slowly.

Try a change. In play.rb, change repeat(8) to repeat(12) and save. Four more petals appear in the browser. Change every: 8 to every: 2 and the flower completes a turn in two seconds. Press Ctrl-C in the terminal to stop the preview.

Open another sketch or choose a port

To open another sketch, pass its path to asobi, for example asobi sketch.rb. A file that contains require "asobi" and calls show also runs with ruby sketch.rb. Add --no-open to the asobi command to open the URL printed in the terminal yourself. The server looks for a free port starting at 4567. Use --port 5000 to start the search at 5000.

Why asobi

Here is one picture: 6×6 square tiles, rotating and shrinking, in two colors. This is all of it in asobi.

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)

And this is the same picture in p5.js, written out in full.

function setup() {
  createCanvas(400, 400);
  angleMode(DEGREES);
  rectMode(CENTER);
  noStroke();
  noLoop();
}

function draw() {
  background(255);
  translate(width / 2, height / 2);
  const n = 6, size = 18, step = size + 4;
  const origin = ((n - 1) * step) / 2;
  for (let i = 0; i < 36; i++) {
    const t = i / 36;
    push();
    translate((i % n) * step - origin, Math.floor(i / n) * step - origin);
    rotate(t * 90);
    fill(i % 2 === 0 ? "tomato" : "navy");
    square(0, 0, size * (1 - t * 0.5));
    pop();
  }
}

In asobi, in_grid does the layout, change_each does the gradual change, and colored(:tomato, :navy) alternates the two colors. Open the asobi version in the playground and change a number to see what moves.

Key features

To try each example in this section, keep require "asobi" at the top of play.rb and replace the lines beneath it.

Arrange and combine shapes

repeat makes copies in the same spot. in_fan turns them around a shared centre. With a + b, the second picture sits on top, so a small circle added last becomes the centre of the flower. scale(0.6) returns a smaller flower, and beside puts the two side by side as one picture.

petals = petal(40, 16).repeat(8).in_fan.colored(:crimson, :navy)
flower = petals + circle(5).colored(:gold)
show flower.beside(flower.scale(0.6), gap: 12)
A large and a small flower side by side, each with crimson and navy petals around a gold centre.

Lengths are relative to the other shapes in the sketch, and the preview fits the drawing to the screen. Angles are measured in degrees clockwise, so rotate(90) makes a quarter turn.

Object to Art: turn Ruby values into pictures

With to_art, any Ruby value becomes a shape: numbers become discs, strings and symbols become typography, arrays and ranges become visual rows in element order, and hashes become key-value tables. Pass as: to use each number as the size of another shape — here the Fibonacci sequence becomes a row of growing stars that you can colour and set in motion.

fib = [1, 1, 2, 3, 5, 8, 13, 21]
stars = fib.to_art(as: star).colored(:gold, :orange)
show stars.spin(every: 4).one_by_one, on: :black
Eight gold and orange stars in a row on black, from small to large, each turning a little after the one before.

Art to Object: give your own class a visual form

Define to_art to return the picture for an instance, and you can pass any domain object directly to show. Include Asobi::Art when you want the reverse direction too — calling composition and animation methods like spin or beside straight on the object itself.

class Flower
  include Asobi::Art

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

  def to_art
    petal.repeat(@count).in_fan.colored(@color)
  end
end

show Flower.new(7, :coral).spin(every: 4)
A flower of seven coral petals turns.

Ask for motion by name

Motion comes from words too: spin, pulse, sway, orbit. The dots below grow to twice their size and shrink back over two seconds. Placing one_by_one after pulse offsets the start of each dot's motion, sending a pulse around the ring. For a motion of your own, write it in an animate block.

dots = circle(5).repeat(24).in_ring.colored(:gold, :orange)
show dots.pulse(every: 2, to: 2).one_by_one, on: :black
A ring of gold and orange dots on black. A swelling pulse travels around the ring.

Glow, trails and blending

Effects chain the same way. trail(4) keeps four fainter copies of where the picture just was. glowing lights its edges, and blended(:screen) brightens the places where shapes overlap. Pass grain: true to show for film grain over the whole picture.

petals = petal(50, 18).repeat(8).in_fan.colored(:deeppink, :cyan)
glow = petals.spin(every: 4).trail(4).glowing.blended(:screen)
show glow, on: :black, grain: true
A glowing pink and cyan flower spins on black, leaving a short trail.

React to clicks, the pointer and sound

Write who drives a motion after it, and a click, the pointer or sound turns it instead of the clock. when_clicked plays the motion once on a click, while_hovered plays it while the pointer is over the picture, and when_pressed("a") plays it once when the key is pressed. by picks a moment of the motion from a value: by(:mouse_x) follows the pointer from left to right.

dot = circle(8).colored(:deeppink)
dots = dot.pulse(to: 2).when_clicked.repeat(8).in_ring(60)
needle = star(radius: 30).colored(:gold).spin.by(:mouse_x)
show dots + needle, on: :black

Only the dot you click swells, and the star turns as the pointer moves across. A picture that reacts is still a picture, so you can arrange it or set it in motion again. No Ruby runs in the browser, so a saved HTML file reacts in the same way.

Pictures can follow sound too. This sketch is for VJ use: it moves with what the microphone hears.

burst = star(points: 8, radius: 60, inner: 24).colored(:deeppink)
bass = burst.pulse(to: 1.6).by(:mic, :low).spin(every: 8)
kick = ring(radius: 90, thickness: 3).colored(:cyan).pulse(every: 0.4, to: 1.3).on_beat
show bass.on(kick).glowing, on: :black, controls: false

by(:mic, :low) swells the star with the strength of the bass. :mid and :high follow the middle and high sounds, :pitch follows how high the sound is, and with no second word it follows the volume. on_beat plays the motion once on every beat. Browsers open the microphone only after a click, so click the page once. controls: false hides the record and code buttons, and F switches to full screen.

Make sound

play plays a song file in a loop, and sounding plays a sound each time a reaction starts.

play "beat.wav"
notes = [:c4, :e4, :g4, :c5]
keys = notes.map { |note| circle(18).colored(:gold).pulse(to: 1.4).when_clicked.sounding(note) }
beat = ring(radius: 90, thickness: 3).colored(:cyan).pulse(every: 0.4, to: 1.2).on_beat(:music)
show keys.to_art.in_row(gap: 16).on(beat), on: :black

Click the gold circles to hear C, E, G and C. Write a note as :c4 or "C#4". There are also six named sounds: :pop, :kick, :snare, :hat, :clap and :bell. asobi makes all of them in the browser, so you need no sound files.

by(:music) and on_beat(:music) listen to the sound asobi itself plays. The ring in this sketch jumps on each beat of the song, so pictures can follow music without a microphone.

beat.wav is a four-second loop in examples/reactions. To use your own song, put the file in the same folder as the sketch and change the name. When you edit and save the sketch, the same song keeps playing without a break. Sound starts after you click the page once, and M mutes it.

Save the file, see the change

While a sketch runs, asobi watches its file. Save it in your editor and the picture in the browser is replaced without a page reload. The animation clock keeps running, so you can tune a number while watching the motion.

In the live preview What happens
Press C The sketch's source code appears over the picture, like subtitles. Press again to hide it.
Press R, then R again Start recording, then download the clip as MP4 or WebM in a supported browser. The clip includes the sound asobi plays.
Press F Switch to full screen. Press again to go back.
Press M Mute the sound asobi plays. Press again to hear it.
Save a sketch with an error The last picture stays visible, with the error message underneath. The message includes an example of how to fix it.

Sketch gallery

Each piece is a single Ruby file. Select a picture to read its code. From a checkout of the repository, run one with a command such as asobi examples/showcase/phyllotaxis.rb.

Four hundred seeds, shading from gold to pink, sit on a sunflower spiral and swell in a wave from the centre outwards. On cream paper, English and Japanese letters form three rings that turn against each other around a beating red heart. Four planets with fading tails circle a glowing sun.
Sunflower seeds
400 circles placed with in_spiral · 3 lines
Type rings
letters set in_ring, one by one · 12 lines
Orbits
your own Planet drawn through to_art · 10 lines
Two sets of fine cyan-to-pink rings overlap; one circles and breathes, and curved moiré stripes ripple across the frame. Seigaiha, the Japanese wave pattern, in indigo lines. Pale blue washes through it row by row. A row of shuffled, many-coloured bars swaps neighbours again and again until the bars stand in order of length.
Moiré
two sets of rings, one moving with orbit · 4 lines
Seigaiha
overlapping rings and a tide of shift_color · 7 lines
Bubble sort
every swap of an array, drawn with to_art · 12 lines

The examples on the website can be edited right in the browser. Select an animation, then Open in playground.

Flower Through a heart Night sky
Crimson and navy petals turn around a gold centre. Red and gold stripes rotate behind a heart-shaped window. Gold stars twinkle beside a glowing crescent moon.
in_fan clipped_by fade

Find more in the examples folder and on the examples page.

Every word at a glance

Look up arguments and defaults in the method reference. When you leave out a number, asobi uses one that fits.

To do this Words
Start with a shape circle ellipse rect square triangle polygon star line text
Playful shapes heart petal moon drop cloud
Draw lines arc wave ring path
Layer and combine + on under beside above repeat (*)
Arrange in_row in_column in_grid in_ring in_fan in_spiral scattered change_each
Transform move rotate scale grow shrink mirror flip
Colour colored outlined stroke opacity hsl gray, and colored(from: :gold, to: :crimson) for a gradient
Add effects glowing blurred blended shadowed clipped_by trail, and grain: true in show
Set in motion spin pulse sway orbit fade bounce drift shift_color animate one_by_one
React when_clicked while_hovered when_pressed on_beat, and by to follow a value (:mouse_x :mouse_y :mic :music)
Make sound play for a song, sounding for effect sounds and notes
Turn values into pictures to_art on numbers, arrays, ranges, hashes, strings and symbols, and the to_art you define in your own class
Show and save show (on: for the background, paper: for page dimensions, controls: false for no buttons), save for SVG or HTML, at for one moment of a motion

Export and record

Use save to write a still picture as SVG or a playable animation as a self-contained HTML file. You can open the HTML file directly in a browser. The example writes both files to the current directory, then shows the same animation live.

flower = petal.repeat(8).in_fan.colored(:crimson, :navy)
motion = flower.spin(every: 4)

flower.save("flower.svg")
motion.save("flower.html")
show motion

To save a particular frame, use motion.at(1).save("frame.svg") for the picture one second into the loop. For a video, press R in the live preview to record.

A picture that reacts keeps reacting in the saved HTML file. A song you play is stored inside the file too (up to 20 MB), so one file carries the picture and its sound. An SVG file holds the still picture from before any reaction.

Use asobi from an existing Ruby program

Load asobi/pure to keep shape names under Asobi and leave Ruby's built-in classes untouched. Save the following as export.rb and run ruby export.rb from the same folder. It writes three navy stars to stars.svg.

require "asobi/pure"

stars = Asobi.art([3, 1, 4], as: Asobi.star)
stars.colored(:navy).save("stars.svg")

Documentation

To do this Go to
Learn Object to Art / Art to Object step by step Guide (Markdown)
Look up shapes, layouts, motion, and defaults Method reference (Markdown)
Try asobi in the browser without installing it Playground
Read complete one-file sketches Examples
Give AI coding agents full API context llms.txt · llms-full.txt · SKILL.md
See why asobi is designed this way Design notes (Japanese)
See what changed in each release Changelog

FAQ

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.

Development

From the repository root, use ruby exe/asobi examples/flower.rb to try changes to the library. Run the following commands to test the library and build the documentation site locally.

bundle install
bundle exec rake test
bundle exec rake site

Open _site/index.html in a browser for English or _site/ja/index.html for Japanese. Editing and running code in the playground requires an internet connection to load its Ruby WebAssembly runtime from a CDN.

The suite checks the library and CLI, builds the site with test data, and runs the sketches from the gallery and both READMEs. To rebuild the GIFs in this README, use the scripts in docs/readme/tools.

Issues and pull requests

Issues are welcome in English or Japanese. For a bug, a small sketch that shows the problem helps us reproduce it. For an idea, tell us what you'd like to draw or animate. Open an issue

Want to fix a bug, improve the docs, or share a sketch? Fork asobi, make your change on a branch in your fork, and open a pull request against Hayato1031/asobi:main. English and Japanese are both welcome. For a larger change, an issue is a good place to discuss the idea first.

License

MIT · RubyGems · Official website · llms.txt