Replacing createGraphics with buffer convenience method

In vanilla processing there is a createGraphics method that allows you to create a graphics into an off-screen buffer, that should only really be called outside the draw loop (to avoid memory issues). The buffer method reduces the boilerplate code required by wrapping the user provided block with begin_draw and end_draw, in practice you probably won’t use this method much, however it does illustrates how can use ruby blocks in your code.

The buffer method

Here is the buffer method extracted from the HelperMethods module see helper_methods.rb

# Nice block method to draw to a buffer.
# You can optionally pass it a width, a height, and a renderer.
# Takes care of starting and ending the draw for you.
def buffer(buf_width = width, buf_height = height, renderer = @render_mode)
  create_graphics(buf_width, buf_height, renderer).tap do |buffer|
    yield buffer

A simple example

attr_reader :pg

def setup
  sketch_title 'Create graphics using :buffer'
  @pg = buffer(60, 70, P2D) do |buf|
    buf.background 51
    buf.stroke 255
    buf.rect 0, 0, 59, 69

def draw
  fill 0, 12
  rect 0, 0, width, height
  image pg, mouse_x - 60, mouse_y - 70

def settings
  size 640, 380, P2D

For real example usage see luciernagas, trefoil sketches