Class Gruff::Scene
In: lib/gruff/scene.rb
Parent: Gruff::Base

A scene is a non-linear graph that assembles layers together to tell a story. Layers are folders with appropriately named files (see below). You can group layers and control them together or just set their values individually.


  • A city scene that changes with the time of day and the weather conditions.
  • A traffic map that shows red lines on streets that are crowded and green on free-flowing ones.


 g ="500x100", "path/to/city_scene_directory")

 # Define order of layers, back to front
 g.layers = %w(background haze sky clouds)

 # Define groups that will be controlled by the same input
 g.weather_group = %w(clouds)
 g.time_group = %w(background sky)

 # Set values for the layers or groups = "cloudy"
 g.time =
 g.haze = true

 # Write the final graph to disk
 g.write "hazy_daytime_city_scene.png"

There are several rules that will magically select a layer when possible.

  • Numbered files will be selected according to the closest value that is less than the input value.
  • ‘true.png’ and ‘false.png’ will be used as booleans.
  • Other named files will be used if the input matches the filename (without the filetype extension).
  • If there is a file named ‘default.png’, it will be used unless other input values are set for the corresponding layer.


draw   layers=   method_missing   new  


layers  [R]  An array listing the foldernames that will be rendered, from back to front.
 g.layers = %w(sky clouds buildings street people)

Public Class methods


# File lib/gruff/scene.rb, line 50
  def initialize(target_width, base_dir)
    @base_dir = base_dir
    @groups = {}
    @layers = []    
    super target_width

Public Instance methods


# File lib/gruff/scene.rb, line 57
  def draw
    # Join all the custom paths and filter out the empty ones
    image_paths = { |layer| layer.path }.select { |path| !path.empty? }
    images =*image_paths)
    @base_image = images.flatten_images


# File lib/gruff/scene.rb, line 64
  def layers=(ordered_list)
    ordered_list.each do |layer_name|
      @layers <<, layer_name)

Group layers to input values

 g.weather_group = ["sky", "sea", "clouds"]

Set input values = "cloudy"


# File lib/gruff/scene.rb, line 78
  def method_missing(method_name, *args)
    case method_name.to_s
    when /^(\w+)_group=$/
      add_group $1, *args
    when /^(\w+)=$/
      set_input $1, args.first