Coder Social home page Coder Social logo

macruby_graphics's Introduction

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 effects MacRuby Graphics Image Iterate MacRuby Graphics particles examples MacRuby Graphics ropes MacRuby Graphics effects MacRuby Graphics Image resizing MacRuby Graphics randomize MacRuby Graphics Spirograph MacRuby Graphics color sampler MacRuby Graphics Rubbons

macruby_graphics's People

Contributors

mattetti avatar nbrandaleone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

macruby_graphics's Issues

Typo in image.rb

Found a typo in Line 447 of image.rb

It reads: r,b,b,a = [nscolor.redComponent,nscolor.greenComponent,nscolor.blueComponent,nscolor.alphaComponent]

Should be:
r,g,b,a = [nscolor.redComponent,nscolor.greenComponent,nscolor.blueComponent,nscolor.alphaComponent]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.