-
Notifications
You must be signed in to change notification settings - Fork 0
Using the Processing API
All of the Processing methods, as explained in the Processing Language API, are available as instance methods on your Processing::App. (frame_rate, ellipse, and the 158 others.) This makes it easy as pie to use them within your sketch.
# Triangles gone wild def setup color_mode RGB, 1.0 frame_rate 30 fill 0.8, 0.6 smooth end def draw triangle(rand(width), rand(height), rand(width), rand(height), rand(width), rand(height)) end
Many methods that you might expect to find under their Processing names will be available by more Rubyish monikers. “keyPressed” becomes “key_pressed?”, for instance. And some things are better done with regular Ruby than with Processing; instead of using load_strings("file.txt") to read in a file, consider File.readlines("file.txt").
Because of this method madness, Processing::Apps have a convenience method for searching through them. $app.find_method("ellipse") will return a list of the method names that may match what you’re looking for: “ellipse”, “ellipseMode”, and “ellipse_mode”.
While Ruby-Processing will eventually support all of the APIs of the Processing language, in order to experience the best of both worlds we strongly recommend use of Ruby idioms whenever and wherever possible.
Doing so not only makes your Ruby-Processing based sketches clearer (and less verbose) to Rubyists, but also lets you take advantage of the power of Ruby’s language facilities.
For example, if the Processing code was:
// Each line is split into an array of String
String[] values = split(line, ",");Though it could be translated like so:
values = split(line, ",")Might be better written:
values = line.split(",")Another example. If the original Processing code was:
// code snippet 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]);
}A literal translation in Ruby-Processing would be only slightly less clunky:
data = loadStrings("data.txt")
bubbles = Array.new(data.length)
for i in 0 .. bubbles.length - 1 do
values = float(split(data[i], ","))
bubbles[i] = Bubble.new(values[0], values[1], values[2])
endRuby-fying the code above, gives you:
data = load_strings("data.txt") # Or: File.read("data/data.txt")
bubbles = []
data.each do |datum|
values = float(datum.split(",")) # Or: datum.split(",").collect{ |n| n.to_f }
bubbles << Bubble.new(*values)
endThe sample sketches bundled with Ruby-Processing are transliterated from original Processing language based sketches. This was done on purpose so as not to throw off users who are new to Processing and are following the illustrations from the Processing website or the examples from the book: Learning Processing