Skip to content
Newer
Older
100644 109 lines (89 sloc) 4.14 KB
cea35c6 @mattetti updated readme
mattetti authored Jul 11, 2010
1 # MacRuby Graphics
1d839c2 @mattetti updated readme
mattetti authored Jul 11, 2010
2
3 MacRuby Graphics is a graphics library providing a simple object-oriented
4 interface into the power of Mac OS X's Core Graphics and Core Image drawing libraries.
5 With a few lines of easy-to-read code, you can write scripts to draw simple or complex
6 shapes, lines, and patterns, process and filter images, create abstract art or visualize
7 scientific data, and much more.
8
9 Inspiration for this project was derived from Processing and NodeBox. These excellent
10 graphics programming environments are more full-featured than MRG, but they are implemented
11 in Java and Python, respectively. MRG was created to offer similar functionality using
12 the Ruby programming language.
13
9136319 @mattetti typo
mattetti authored Jul 11, 2010
14 The original author of this library is James Reynolds, MacRuby Graphics was then called Ruby Cocoa Graphics
1d839c2 @mattetti updated readme
mattetti authored Jul 12, 2010
15 and was packaged as part of Hotcocoa. I made the choice to extract it, add more examples and specs
16 so MacRuby developers could use this library as an addon to their projects without needing HotCocoa.
17
cea35c6 @mattetti updated readme
mattetti authored Jul 12, 2010
18 ## Examples
1d839c2 @mattetti updated readme
mattetti authored Jul 12, 2010
19
20 You can see a list of examples in the examples folder, but here is a quick sample:
21
22 class CustomView < NSView
23 include MRGraphics
24
25 def drawRect(rect)
97f125c @mattetti more examples
mattetti authored Jul 12, 2010
26 dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
27 Canvas.for_current_context(:size => dimensions) do |c|
1d839c2 @mattetti updated readme
mattetti authored Jul 12, 2010
28 c.background(Color.black)
29 white = Color.white
30 c.fill(white)
31 c.stroke(0.2)
32 c.stroke_width(1)
33 c.font("Zapfino")
34
35 80.times do
36 c.font_size rand(170)
37 c.fill(white.copy.darken(rand(0.8)))
38 letters = %W{ g i a n a }
39 c.text(letters[rand(letters.size)],
40 rand(c.width),
41 rand(c.height))
42 end
43 end
44 end
80ec1a1 @mattetti better examples
mattetti authored Jul 12, 2010
45
1d839c2 @mattetti updated readme
mattetti authored Jul 12, 2010
46 end
47
48 # wrapper class to keep the examples as clean/simple as possible
49 app = AppWrapper.new
50 # assign an instance of our custiom NSView to the window's content view
51 app.window.contentView = CustomView.alloc.initWithFrame(app.frame)
52 # start the app
9cf273a @mattetti fixes and udpated README
mattetti authored Jul 11, 2010
53 app.start
54
55 ![MacRuby Graphics Canvas example](http://img.skitch.com/20100712-1x4dswurhxcqexq5tpidj29axc.png)
56
57
58 class CustomView < NSView
59 include MRGraphics
60
61 def drawRect(rect)
97f125c @mattetti more examples
mattetti authored Jul 13, 2010
62 dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
80ec1a1 @mattetti better examples
mattetti authored Jul 13, 2010
63 Canvas.for_current_context(:size => dimensions) do |c|
64 c.background(Color.white)
65 c.font('Skia')
66 c.font_size(14)
9cf273a @mattetti fixes and udpated README
mattetti authored Jul 12, 2010
67 # set image width,height
68 w, h = [95,95]
69 # set initial drawing position
80ec1a1 @mattetti better examples
mattetti authored Jul 13, 2010
70 x, y = [10, c.height - h - 10]
9cf273a @mattetti fixes and udpated README
mattetti authored Jul 12, 2010
71 # load and resize two images
72 img_a = Image.new(File.join(HERE, 'images', 'v2.jpg')).resize(w,h)
73 img_b = Image.new(File.join(HERE, 'images', 'italy.jpg')).resize(w,h)
74
75 # add image B to image A using each blending mode, and draw to canvas
76 [:normal,:multiply,:screen,:overlay,:darken,:lighten,
77 :colordodge,:colorburn,:softlight,:hardlight,:difference,:exclusion,
78 :hue,:saturation,:color,:luminosity,:maximum,:minimum,:add,:atop,
79 :in,:out,:over].each do |blendmode|
80 img_a.reset.resize(w,h)
81 img_a.blend(img_b, blendmode)
80ec1a1 @mattetti better examples
mattetti authored Jul 13, 2010
82 c.draw(img_a,x,y)
83 c.text(blendmode, x, y-15)
9cf273a @mattetti fixes and udpated README
mattetti authored Jul 12, 2010
84 x += w + 5
80ec1a1 @mattetti better examples
mattetti authored Jul 13, 2010
85 if (x > c.width - w + 5)
9cf273a @mattetti fixes and udpated README
mattetti authored Jul 12, 2010
86 x = 10
87 y -= h + 25
88 end
89 end
90 end
91 end
92
93 end
94
95 # wrapper class to keep the examples as clean/simple as possible
96 app = AppWrapper.new(415,730)
97 # assign an instance of our custiom NSView to the window's content view
98 app.window.contentView = CustomView.alloc.initWithFrame(app.frame)
99 # start the app
100 app.start
101
97164c9 @mattetti more fixes and one more example ported over
mattetti authored Jul 12, 2010
102 ![MacRuby Image blend modes](http://img.skitch.com/20100712-bedhi8i4ppuqetad263w3ehuna.png)
103
80ec1a1 @mattetti better examples
mattetti authored Jul 13, 2010
104 More example outputs:
97164c9 @mattetti more fixes and one more example ported over
mattetti authored Jul 12, 2010
105
97f125c @mattetti more examples
mattetti authored Jul 13, 2010
106 ![MacRuby Image color effects](http://img.skitch.com/20100712-jr4jfhbaw2x9nmhy7bscapgbd4.png)
2d7c482 @mattetti new particle example and more bug fixes
mattetti authored Jul 13, 2010
107 ![MacRuby Image Iterate](http://img.skitch.com/20100713-1132mmahgum65tpgj9d9mag939.png)
63c21b2 @mattetti added an rope hair example & more fixes
mattetti authored Jul 13, 2010
108 ![MacRuby particles examples](http://img.skitch.com/20100713-gb3ps8psw3ppyedx1t1x426rwa.png)
109 ![MacRuby ropes](http://img.skitch.com/20100713-mseyj6qjxp38jnm2xkxpw6ebq4.png)
Something went wrong with that request. Please try again.