Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Cocoa's Core Graphics & Core Image wrapper for MacRuby

branch: master

This branch is 0 commits ahead and 0 commits behind master

Added new framework to work with 10.8

CGRectMake and other core graphics function calls have been moved into
"CoreGraphics" framework. You now must load this framework to ensure
compatibility with 10.8. See
"MacRuby/MacRuby#134" for more information.
latest commit cb7c119d45
Nick Brandaleone nbrandaleone authored November 20, 2012
Octocat-spinner-32 examples new example (color sampler) July 15, 2010
Octocat-spinner-32 lib new example (color sampler) July 15, 2010
Octocat-spinner-32 specs started adding test coverage July 11, 2010
Octocat-spinner-32 README.md new example (color sampler) July 15, 2010
Octocat-spinner-32 graphics.rb Added new framework to work with 10.8 November 20, 2012
README.md

MacRuby Graphics

MacRuby Graphics is a graphics library providing a simple object-oriented interface into the power of Mac OS X's Core Graphics and Core Image drawing libraries.
With a few lines of easy-to-read code, you can write scripts to draw simple or complex shapes, lines, and patterns, process and filter images, create abstract art or visualize scientific data, and much more.

Inspiration for this project was derived from Processing and NodeBox. These excellent graphics programming environments are more full-featured than MRG, but they are implemented in Java and Python, respectively. MRG was created to offer similar functionality using the Ruby programming language.

The original author of this library is James Reynolds, MacRuby Graphics was then called Ruby Cocoa Graphics and was packaged as part of Hotcocoa. I made the choice to extract it, add more examples and specs so MacRuby developers could use this library as an addon to their projects without needing HotCocoa.

Examples

You can see a list of examples in the examples folder, but here is a quick sample:

class CustomView < NSView
  include MRGraphics

  def drawRect(rect)
    dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
    Canvas.for_current_context(:size => dimensions) do |c|
      c.background(Color.black)
      white = Color.white
      c.fill(white)
      c.stroke(0.2)
      c.stroke_width(1)   
      c.font("Zapfino")

      80.times do 
        c.font_size rand(170)
        c.fill(white.copy.darken(rand(0.8)))
        letters = %W{ g i a n a } 
        c.text(letters[rand(letters.size)],
                rand(c.width),
                rand(c.height))
      end
    end
  end

end

# wrapper class to keep the examples as clean/simple as possible
app = AppWrapper.new
# assign an instance of our custiom NSView to the window's content view
app.window.contentView = CustomView.alloc.initWithFrame(app.frame)
# start the app
app.start

MacRuby Graphics Canvas example

class CustomView < NSView
  include MRGraphics

  def drawRect(rect)
    dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
    Canvas.for_current_context(:size => dimensions) do |c|
      c.background(Color.white)
      c.font('Skia')
      c.font_size(14)
      # set image width,height
      w, h = [95,95]
      # set initial drawing position
      x, y = [10, c.height - h - 10]
      # load and resize two images
      img_a = Image.new(File.join(HERE, 'images', 'v2.jpg')).resize(w,h)
      img_b = Image.new(File.join(HERE, 'images', 'italy.jpg')).resize(w,h)

      # add image B to image A using each blending mode, and draw to canvas
      [:normal,:multiply,:screen,:overlay,:darken,:lighten,
        :colordodge,:colorburn,:softlight,:hardlight,:difference,:exclusion,
        :hue,:saturation,:color,:luminosity,:maximum,:minimum,:add,:atop,
        :in,:out,:over].each do |blendmode|
        img_a.reset.resize(w,h)
        img_a.blend(img_b, blendmode)
        c.draw(img_a,x,y)
        c.text(blendmode, x, y-15)
        x += w + 5
        if (x > c.width - w + 5)
          x = 10
          y -= h + 25
        end
      end
    end
  end

end

# wrapper class to keep the examples as clean/simple as possible
app = AppWrapper.new(415,730)
# assign an instance of our custiom NSView to the window's content view
app.window.contentView = CustomView.alloc.initWithFrame(app.frame)
# start the app
app.start

MacRuby Image blend modes

More examples:

see the examples folder for the source code of each image's source code.

MacRuby Graphics Image color effectsMacRuby Graphics Image IterateMacRuby Graphics particles examplesMacRuby Graphics ropesMacRuby Graphics effectsMacRuby Graphics Image resizingMacRuby Graphics randomizeMacRuby Graphics SpirographMacRuby Graphics color samplerMacRuby Graphics Rubbons

Something went wrong with that request. Please try again.