Ruby Processing

Here is my blog in which I will describe my experiments with ruby-processing, find out more about ruby-processing at:- https://github.com/jashkenas/ruby-processing compatible with processing-2.2.1 and https://github.com/monkstone/cf3ruby for my version of the cfdg DSL (context-free-art)

Saturday, 20 September 2014

Using libraries from processing-3.0a4 in ruby processing

Up to and including processing-2.2.1 the path to sketchbook was/is discovered by "reading" the preferences.txt configuration file for ruby processing. For processing-3.0a4 the name has changed (to allow different libraries/sketches to co-exist for different versions?). For processing-3.0a4 you should set the "sketchbook_path" in the ".rp5rc" file, works for me on linux see yaml file below (will override preferences.txt value).
---
PROCESSING_ROOT: /home/tux/processing-3.0a4
JRUBY: true
sketchbook_path: /home/tux/sketchbook

Wednesday, 17 September 2014

Using the Range class in ruby processing

Since ruby-processing-2.6.3 the processing constrain function is actually implemented using the clip function of Range (this method has been added to Range in ruby-processing). However in ruby processing we should probably prefer to use the clip method directly, as I have here in my World class:-
# Class provides an OO way constraining a Mover in a 2D space
# use
# world = World.new((0..width), (0..height))
# world.constrain_mover(mover)
class World
  attr_reader :xrange, :yrange
  def initialize(xrange, yrange)
    @xrange, @yrange = xrange, yrange
  end

  # @param mover is expected respond to loc, vel
  # that in turn respond to x and y getter/setters (Vec2D does this)

  def constrain_mover(mover)
    # Note clip functionality, extends Range in ruby-processing
    unless xrange.cover? mover.loc.x
      mover.vel.x *= -1
      mover.loc.x = xrange.clip mover.loc.x
    end
    return if yrange.cover? mover.loc.y
    mover.vel.y *= -1
    mover.loc.y = yrange.clip mover.loc.y
  end
end

For the sketch using this code see my fork Dan Shiffmans The Nature of Code Examples.

Monday, 15 September 2014

Include Processing::Proxy to mimic the processing inner classes

If you have been using processing for some time, then you should be aware of the inner classes used by processing to make variables/methods available across classes (strictly non OOP but convenient). In the processing ide, the use of java inner classes is hidden (because the code is post-processed). However if you look at the generated java you will find that if you create a class in the processing ide, it becomes an inner class. Use of the inner class is there to allow easy access to the methods and variables of the outer class (your sketch). If you graduated from using the processing ide to using Eclipse (see tutorial) or Netbeans (other ides are available) you will know that when you create a new java class you can't use the methods and variables, unless you call the instance of the PApplet from you sketch (typically a PApplet variable parent created in your new class, and the class constructor is used to initialize that variable). To give a similar level of access to that provided by processings inner classes you should include the ruby processing Processing::Proxy module in your sketches (however sketch width, and height are not available). The more 'pure' way to to provide such access is to copy the "Eclipse" way of accessing these variables and methods, and that is what you "should" do to access sketch width and height. The "dirty" way to do it is access the sketches '$app' global variable "width = $app.width" for example. A template for creating Processing::Proxy class (in a separate file) is included in ruby-processing:-
rp5 create inner_class --inner

Of course you will give a different more relevant name to your inner classes!!
Then vim inner_class.rb, other editors are available

class InnerClass
  include Processing::Proxy
end

Thursday, 4 September 2014

New features in ruby-processing-2.6.2

# Hooky class demonstrates how to use the post_initialize hook,
# available since ruby-processing-2.6.2, to add additional attribute
# in this case :background as an array of int (splat to color)
# Not sure how clever it is to have multiple sketch instances.

class Hooky < Processing::App
  attr_reader :back

  def setup
    size 200, 200
    background(*back)
  end

  def post_initialize(args)
    @back = (args[:background])
  end
end

red = [200, 0, 0]
green = [0, 200, 0]
blue = [0, 0, 200]
colors = [red, green, blue]

colors.each_with_index do |col, i|
  Hooky.new(x: i * 90, y: i * 90, title: "Hooky #{i}", background: col)
end


Read more about post-initialization hooks in POODR by Sandi Metz.

Monday, 1 September 2014

Instrumenting ruby-processing

Enhance your geek credibility by using JMXBeans to monitor your ruby-processing sketch. See jruby wiki for details. Above was done using a modified version of JRubyArt (the development version of ruby-processing) where I sent --manage flag to jruby when running the sketch.

Getting started with ruby-processing for ruby purists

Previously in my post getting started for wizards I showed how to create a sketch that would be more familiar to people coming from processing to ruby-processing. The sketch does not need to be explicitly wrapped as a class (that extends from Processing::App), since ruby-processing does that for you under the hood. This is the DSL approach to writing ruby-processing sketches which I favour, and makes it easier to read across from other processing modes to ruby-processing. However since ruby-processing-2.6.0 it is just as easy to create a classical sketch.
rp5 create classic_sketch 200 200 --wrap
rp5 watch classic_sketch.rb


Open a new console and 'vim classic_sketch.rb' or us another suitable editor

class ClassicSketch < Processing::App
  def setup
    size 200, 200
  end

  def draw

  end
end

# ClassicSketch.new(x: 20, y: 20)

For instant gratification and to relocate the sketch on your display un-comment the last line of the sketch and save (also note this line was "not" required to run the sketch, rp5 run or rp5 watch takes care of creating a new instance of sketch for you). You only use this form to send parameters, such as 'x offset' to your sketch

class ClassicSketch < Processing::App
  def setup
    size 200, 200
  end

  def draw

  end
end

ClassicSketch.new(x: 20, y: 20)
The sketch updates auto-magically...
Already in the works for the next release is the possibility of placing the sketch on your screen using the config file '~/.rp5rc' so this is kind of redundant, but a post initialization hook might get added so you will be able to send all sorts of other parameters. Which are then available by overriding the post_initialization hook method (See Sandi Metz POODR)

Next change the background of your sketch

class ClassicSketch < Processing::App
  def setup
    size 200, 200
  end

  def draw
    background 0
  end
end

ClassicSketch.new(x: 20, y: 20)

Next create a blue box.

class ClassicSketch < Processing::App
  def setup
    size 200, 200
  end

  def draw
    background 0
    fill 0, 0, 200
    rect 40, 50, 120, 100
  end
end

ClassicSketch.new(x: 20, y: 20)


To create class wrapped P3D sketch just "rp5 create classy_sketch 200 200 p3d --wrap" actually it probably doesn't matter where you put the --wrap option after create (but the order of the other variables is important)...

Sunday, 31 August 2014

Getting started with ruby-processing for wizards (best for vim users, though other editors can be used)

You have to start somewhere and for brevity I am assuming you've installed ruby-processing-2.6.2 here:-
rp5 create my_sketch 200 200
rp5 watch my_sketch.rb


In a new terminal start up vim
vim my_sketch.rb

def setup
  size 200, 200
end

def draw

end

Edit the sketch to change its background

def setup
  size 200, 200
end

def draw
  background 0
end

On saving the sketch refreshes for you

Now add a colored ellipse

def setup
  size 200, 200
end

def draw
  background 0
  fill 200, 0, 0
  ellipse 100, 100, 120, 100
end

Save the sketch again and is update once more, how cool is that!!!

Now this all easiest done using vim (I mean you only need two consoles and you are cooking, but you could do the same with emacs, jEdit, eric, textmate to name but a few decent editors). With processing-2.2.1 can also be use with PDF, P2D and P3D modes "rp5 create my_sketch 200 200 p3d" works to create P3D sketches. Now as far as I know there is no book on ruby processing but there is Dan Shiffmans nature of code, and the examples have been translated to ruby-processing here or for the beginner there is his Learning Processing examples translated here. Then there is the 300+ examples included with ruby-processing.........
The Nature of code book is now available in Japanese.

Followers

About Me

My Photo
Consolidating my online identity as monkstone. I am a 64 bit linux user and advocate of open source software, you can sometimes find me on the processing forum.