Tutorial on the use of Ruby Processing for developing visual graphics and interactive graphics. Includes conceptual and real-world examples, including use for business data mining.
7. hello_world.rb
def setup
size 200, 200 # Set window size
fill 0 # Set fill (and text) color to black
text_font create_font("SanSerif",30) # Set text font
end
def draw
text "Hello World!", 10, 105 # Render text at (10,105)
end
8. Installation is easy
> # Check that Java is installed:
> jvm -version
java version "1.6.0_22”
…
> sudo gem install ruby-processing
Successfully installed ruby-processing-1.0.9
…
> rp5 unpack samples
> cd samples
> rp5 run getting_started.rb
9. getting_started.rb
def setup
size 200, 200
background 0
no_stroke
smooth
@rotation = 0
end
def draw
fill 0, 20
rect 0, 0, width, height
translate width/2, height/2
rotate @rotation
fill 255
ellipse 0, -60, 20, 20
@rotation += 0.1
end
10. require 'ruby-processing'
class Sketch < Processing::App
def setup
# Do this once
end
def draw
# Do this over and over...
end
end
Sketch.new
Here be magic…
Ruby-Processing
adds these, if
they’re missing
11. getting_started.rb
def setup
size 200, 200
background 0
no_stroke
smooth
@rotation = 0
end
def draw
fill 0, 20
rect 0, 0, width, height
translate width/2, height/2
rotate @rotation
fill 255
ellipse 0, -60, 20, 20
@rotation += 0.1
end
12. A word about coordinates *
* or, why you can’t get there from here
(width-1, 0)(0, 0)
(0, height-1) (width-1, height-1)
15. Examples
def draw
background 255
stroke 0
fill 175
translate 25,10
rotate radians(60)
image @jarjar,
10,10,50,50
end
def draw
background 255
stroke 0
fill 175
translate 25,10
image @jarjar,
10,10,50,50
end
def draw
background 255
stroke 0
fill 175
image @jarjar,
10,10,50,50
end
16. getting_started.rb (again)
def setup
size 200, 200 # Set canvas size: width, height
background 0 # Set background black
no_stroke # Disable drawing outlines
smooth # Draw smooth (anti-aliased) edges
@rotation = 0 # Set initial rotation to 0 radians
end
17. getting_started.rb
def draw
fill 0, 20 # Set fill color to black,
# 20% opaque
rect 0, 0, width, height # Draw rectangle over entire
# window, fading it 20%
translate width/2, height/2 # Move drawing origin to
# center of window
rotate @rotation # Rotate around new origin
fill 255 # Set fill color to white
ellipse 0, -60, 20, 20 # Draw circle, center at
# (0,-60), size 20x20
@rotation += 0.1 # Increase angle of rotation
end # and repeat …
18. Instance variables are our friends
Class Sketch < Processing::App
def setup
# Stuff...
@rotation = 0
end
def draw
# More stuff...
@rotation += 0.1
end
end
Using an instance
variable (@rotation)
allows the angle of
rotation to persist
and increment
across redrawings
21. Playing with sketches
# bounce.rb
BLUE = "#0000FF"
GREEN = "#00FF00"
RED = "#FF0000"
BALL_COLOR = BLUE
BALL_SPEED = 5
BALL_SIZE = 32
attr_accessor :ball_speed,
:ball_color, :ball_size
def setup
# …
end
def draw
# …
end
> rp5 run bounce.rb
…
> rp5 watch bounce.rb
…
> rp5 live bounce.rb
…
> rp5 app bounce.rb
…
> rp5 applet bounce.rb
…
> rp5 help
…
22. # follow_mouse.rb
def setup
color_mode HSB, 1.0 # HSB color, values 0.0..1.0
no_stroke # No outlines
ellipse_mode CENTER # Position relative to center
end
def draw
background 0.0, 0.0, 1.0 # White
fill mouse_x.to_f / width, mouse_y.to_f / height, 1.0
ellipse mouse_x, mouse_y, 50, 50 # Draw at mouse position
end
Color and interaction
33. Java or Ruby?
Java Ruby
Syntax
Types, classes Java types, POJO,
JavaBeans, etc.
Ruby classes
Metaprogramming Limited Extensive
Libraries Many Some
Speed Faster Slower
Web applets Integrated Weak
34. Ruby-Processing: East of Java *
* aka, Why we love Ruby!
// Java
// From example 18-3 of Learning Processing by Daniel Shiffman
String[] data = loadStrings("data.txt");
bubbles = new Bubble[data.length];
for (int i = 0; i < bubbles.length; i++) {
float[] values = float(split(data[i], ","));
bubbles[i] = new Bubble(values[0], values[1], values[2]);
}
# ruby
bubbles = load_strings("data.txt").map do |line|
Bubble.new(*line.split(",").map{|num| num.to_f })
end
35. Ruby + Processing = metaprogramming
Processing::Menu.new (:font=>font, :minimizes=>true) do |m|
m.add "Pick year", :pick_year
m.add "Pick item", :pick_item
m.add "Quit", :quit
m.text_size = 14
end
Processing::Graph::DateAxis.new(
:title=>"Date",
:orientation=>:horizontal,
:grid=>true,
:interval=>:month
)
ds.on_highlight do |o|
highlight_data(o)
end
36. Speed Benchmark 1
• Java vs. Ruby
• 0 and 100 iterations
• Average of 5 tests
1
1
33.0
5.4
Each frame
0 iterations
RubyJava
37. Speed Benchmark 2
• Java vs. Ruby
• 0 and 1,000 iterations
• Average of 5 tests
1
1
1.34
6.83
Each frame
0 iterations
RubyJava
46. Geek alert!
• Library and gem usage is tricky
• Both Processing and Ruby-Processing are still evolving
• There are bugs. I had to check out the Github master
branch to resolve a bug in one of these sketches
• There are some compatibility issues, e.g. with Snow
Leopard’s 64-bit JVM, and with $RUBYOPT
• You may need to monkey with the JVM’s runtime
parameters
47. Check these out, too
Processing The original Processing
framework in Java
http://processing.org/
Processing.js Javascript port of
Processing
http://processingjs.org/
Field An alternative for
graphics and visual art,
based on Python
http://openendedgroup
.com/field/wiki
NodeBox Free Mac application for
2D visuals, based on
Python
http://bit.ly/nodebox
48. Learn more *
• http://processing.org/
• https://github.com/jashkenas/ruby-processing/
• Getting Started with Processing, by Casey Reas and Ben Fry
• Learning Processing, by Daniel Shiffman
http://www.learningprocessing.com/
https://github.com/jashkenas/learning-processing-with-ruby
• Processing: A Programming Handbook for Visual Artists and
Designers, by Casey Reas and Ben Fry
• Visualizing Data, by Ben Fry
* Thanks to all of the above for many of the examples