Sha256: 9ca82e4ce67201fd4f087a29221d1bba63c21376b947818495929c00909bf3e0

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

def setup
  
  # With Processing, buffers allow you to draw off-screen to an image,
  # and then use that image in your sketch. This can speed up rendering
  # times quite a bit, as it's not only faster to draw off-screen, but
  # it also allows you to redraw only portions of the screen.
  
  # Ruby-Processing provides a convenience method, "buffer", which
  # sets up a buffer for you with the same width and height and 
  # renderer as the current sketch, and yields it to you. It also
  # takes care of calling begin_draw and end_draw at the start and 
  # end of the block. Use it like so:
  size 800, 800, P2D
  @buffer = buffer do |b|
    b.no_stroke
    b.fill 255, 0, 0
    b.rect 100, 200, 100, 100
  end
  
  # Those lines are equivalent to the following lines:
  
  @buffer_2 = create_graphics(500, 500, P2D)
  @buffer_2.begin_draw
  @buffer_2.no_stroke
  @buffer_2.fill(255, 0, 0)
  @buffer_2.rect(100, 200, 100, 100)
  @buffer_2.end_draw
  
  # If you'd like to set the size or renderer for the buffer block,
  # just pass it in like normal:
  
  @buffer_3 = buffer(150, 150, P2D) do |b|
    # b.whatever goes here.
  end
  
  # And now we go ahead and grab the rendered image from the first buffer.
  @img = @buffer.get(0, 0, @buffer.width, @buffer.height)
end

def draw
  background 0
  image @img, frame_count, 0
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-processing-2.4.3 samples/contributed/simple_buffer.rb
ruby-processing-2.4.2 samples/contributed/simple_buffer.rb
ruby-processing-2.4.1 samples/contributed/simple_buffer.rb