-
Notifications
You must be signed in to change notification settings - Fork 9
Loading Libraries
Like ruby-processing, there is a load_library utility, that can be used to load processing libraries or pure ruby libraries (or a mixture thereof when java libraries are called by the ruby library). Libraries need to have unique names and are called in this order:-
-
local ruby library
library/grammar.rb
called usingload_library :grammar
-
local java library
library/video/{jars}
one or more jars including those inlib
sub-folder -
installed java library
{libraries}/library/video/{jars}
one or more jars including those inlib
sub-folder. In this caselibraries
defaults to~/.jruby_art/libraries
but could be configured in `~/.jruby_art/config.yml to point to wherever vanilla processing libraries are stored.
Using processing-3.0 video library
require 'jruby_art'
# Test your Webcam with this sketch
class TestCapture < Processing::App
load_library :video
include_package 'processing.video'
attr_reader :cam
def setup
size(960, 544)
cameras = Capture.list
fail 'There are no cameras available for capture.' if (cameras.length == 0)
p 'Matching cameras available:'
size_pattern = Regexp.new(format('%dx%d', width, height))
select = cameras.grep size_pattern # filter available cameras
select.uniq.map { |cam| p cam.strip }
fail 'There are no matching cameras.' if (select.length == 0)
start_capture(select[0])
end
def start_capture(cam_string)
# The camera can be initialized directly using an
# element from the array returned by list:
@cam = Capture.new(self, cam_string)
p format('Using camera %s', cam_string)
cam.start
end
def draw
return unless cam.available
cam.read
image(cam, 0, 0)
# The following does the same, and is faster when just drawing the image
# without any additional resizing, transformations, or tint.
# set(0, 0, cam)
end
end
TestCapture.new(title: 'Test Capture')
Custom ruby library 'grammar.rb'
########################################################
# Chequer implemented using a grammar library
# Lindenmayer System in ruby-processing by Martin Prout
########################################################
require 'jruby_art'
class ChequerSketch < Processing::App
load_library :grammar
attr_reader :chequer
def setup
size 600, 600
@chequer = Chequer.new(self, width * 0.9, height/10)
chequer.create_grammar 4
no_loop
end
def draw
background 0
chequer.render
end
end
class Chequer
attr_accessor :app, :axiom, :grammar, :production, :draw_length, :theta, :xpos, :ypos
DELTA = Math::PI / 2.0
def initialize(app, xpos, ypos)
@app, @xpos, @ypos = app, xpos, ypos
@axiom = 'F-F-F-F' # Axiom
@grammar = Grammar.new(axiom, 'F' => 'FF-F-F-F-FF')
@draw_length = 500
app.stroke 0, 255, 0
app.stroke_weight 2
@theta = 0
end
def render
production.each do |element|
case element
when 'F'
x_temp = xpos
y_temp = ypos
@xpos -= draw_length * Math.cos(theta)
@ypos -= draw_length * Math.sin(theta)
app.line(x_temp, y_temp, xpos, ypos)
when '+'
@theta += DELTA
when '-'
@theta -= DELTA
else
puts "Character '#{element}' is not in grammar"
end
end
end
##############################
# create grammar from axiom and
# rules (adjust scale)
##############################
def create_grammar(gen)
@draw_length /= 3**gen
@production = @grammar.generate gen
end
end
ChequerSketch.new