Using the LibraryProxy in your sketches
In the sketch you should load_library :library_proxy
and your library class should inherit
from LibraryProxy and implement pre(), draw() and post() methods (can be empty method if not
required). For simplicity initialize your processing library
in the sketch setup
.
Example library
require 'forwardable'
class CustomArray < LibraryProxy
extend Forwardable
def_delegators(:@objs, :each, :<<)
include Enumerable
attr_reader :app
def initialize(app)
@app = app
@objs = []
end
def add_object(mx, my, x, y, speed)
self << Particle.new(x.to_i, y.to_i, mx, my, Sketch::UNIT, speed, 1, 1)
end
def post
each do |obj|
update_x obj
next unless obj.y >= Sketch::UNIT || obj.x <= 0
obj.ydir *= -1
obj.y += obj.ydir
end
end
def update_x(obj)
obj.x += obj.speed * obj.xdir
return if (0..Sketch::UNIT).cover? obj.x
obj.xdir *= -1
obj.x += obj.xdir
obj.y += obj.ydir
end
def pre
end
def draw
background(0)
fill(255)
each do |obj|
app.ellipse(obj.mx + obj.x, obj.my + obj.y, 6, 6)
end
end
end
Particle = Struct.new(:x, :y, :mx, :my, :size, :speed, :xdir, :ydir)
Example sketch
load_library :library_proxy
require_relative 'custom_array'
UNIT = 40
def setup
size 640, 360
wide_count = width / UNIT
height_count = height / UNIT
custom_array = CustomArray.new(self)
height_count.times do |i|
wide_count.times do |j|
custom_array.add_object(j * UNIT, i * UNIT, UNIT / 2, UNIT / 2, rand(0.05..0.8))
end
end
no_stroke
end
def draw
end