Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 116 lines (96 sloc) 4.914 kb
cea35c60 »
2010-07-11 updated readme
1 # MacRuby Graphics
1d839c2b »
2010-07-11 updated readme
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
91363193 »
2010-07-11 typo
14 The original author of this library is James Reynolds, MacRuby Graphics was then called Ruby Cocoa Graphics
1d839c2b »
2010-07-11 updated readme
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
cea35c60 »
2010-07-11 updated readme
18 ## Examples
1d839c2b »
2010-07-11 updated readme
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)
97f125c4 »
2010-07-12 more examples
26 dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
27 Canvas.for_current_context(:size => dimensions) do |c|
1d839c2b »
2010-07-11 updated readme
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
80ec1a1b »
2010-07-12 better examples
45
1d839c2b »
2010-07-11 updated readme
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
9cf273af »
2010-07-11 fixes and udpated README
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)
97f125c4 »
2010-07-12 more examples
62 dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
80ec1a1b »
2010-07-12 better examples
63 Canvas.for_current_context(:size => dimensions) do |c|
64 c.background(Color.white)
65 c.font('Skia')
66 c.font_size(14)
9cf273af »
2010-07-11 fixes and udpated README
67 # set image width,height
68 w, h = [95,95]
69 # set initial drawing position
80ec1a1b »
2010-07-12 better examples
70 x, y = [10, c.height - h - 10]
9cf273af »
2010-07-11 fixes and udpated README
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)
80ec1a1b »
2010-07-12 better examples
82 c.draw(img_a,x,y)
83 c.text(blendmode, x, y-15)
9cf273af »
2010-07-11 fixes and udpated README
84 x += w + 5
80ec1a1b »
2010-07-12 better examples
85 if (x > c.width - w + 5)
9cf273af »
2010-07-11 fixes and udpated README
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
97164c93 »
2010-07-12 more fixes and one more example ported over
102 ![MacRuby Image blend modes](http://img.skitch.com/20100712-bedhi8i4ppuqetad263w3ehuna.png)
103
968128f3 »
2010-07-13 readme tweaks
104 ##More examples:
105 _see the examples folder for the source code of each image's source code._
97164c93 »
2010-07-12 more fixes and one more example ported over
106
d1441df2 »
2010-07-14 more examples and more bug fixes
107 ![MacRuby Graphics Image color effects](http://img.skitch.com/20100712-jr4jfhbaw2x9nmhy7bscapgbd4.png)
108 ![MacRuby Graphics Image Iterate](http://img.skitch.com/20100713-1132mmahgum65tpgj9d9mag939.png)
109 ![MacRuby Graphics particles examples](http://img.skitch.com/20100713-gb3ps8psw3ppyedx1t1x426rwa.png)
110 ![MacRuby Graphics ropes](http://img.skitch.com/20100713-mseyj6qjxp38jnm2xkxpw6ebq4.png)
f053351d »
2010-07-15 ported last original example and added a few fixes
111 ![MacRuby Graphics effects](http://img.skitch.com/20100716-8ma9te4tc8th723hd4t5rmbbb8.png)
d1441df2 »
2010-07-14 more examples and more bug fixes
112 ![MacRuby Graphics Image resizing](http://img.skitch.com/20100715-k8k8f1gd8rb9e1wfj4ush9i5bf.png)
113 ![MacRuby Graphics randomize](http://img.skitch.com/20100715-tycucqsgsfiy7syef8i24sw9xj.png)
114 ![MacRuby Graphics Spirograph](http://img.skitch.com/20100715-jh4nsrm193a2ttdmjjnh4g1x96.png)
cfdd6550 »
2010-07-15 new example (color sampler)
115 ![MacRuby Graphics color sampler](http://img.skitch.com/20100716-nth8dcm4ag12bcns1fgngt4ird.png)
116 ![MacRuby Graphics Rubbons](http://img.skitch.com/20100715-18f5pwc96b2gdfcdag26sjujam.png)
Something went wrong with that request. Please try again.