Skip to content

Commit

Permalink
better examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Jul 13, 2010
1 parent 97164c9 commit 80ec1a1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 37 deletions.
31 changes: 11 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can see a list of examples in the examples folder, but here is a quick sampl
include MRGraphics

def drawRect(rect)
canvas = Canvas.for_image(:size => [400,400]) do |c|
Canvas.for_current_context(:size => [400,400]) do |c|
c.background(Color.black)
white = Color.white
c.fill(white)
Expand All @@ -40,12 +40,8 @@ You can see a list of examples in the examples folder, but here is a quick sampl
rand(c.height))
end
end

# set the image viewer
img = NSImage.alloc.initWithCGImage(canvas.cgimage, size: NSZeroSize)
img.drawAtPoint([0,0], fromRect: NSZeroRect, operation: NSCompositeSourceOver, fraction: 1)
end

end

# wrapper class to keep the examples as clean/simple as possible
Expand All @@ -58,20 +54,19 @@ You can see a list of examples in the examples folder, but here is a quick sampl
![MacRuby Graphics Canvas example](http://img.skitch.com/20100712-1x4dswurhxcqexq5tpidj29axc.png)



class CustomView < NSView
include MRGraphics

def drawRect(rect)
dimensions = [415,730]
canvas = Canvas.for_image(:size => dimensions) do
background(Color.white)
font('Skia')
font_size(14)
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, height - h - 10]
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)
Expand All @@ -83,19 +78,15 @@ You can see a list of examples in the examples folder, but here is a quick sampl
:in,:out,:over].each do |blendmode|
img_a.reset.resize(w,h)
img_a.blend(img_b, blendmode)
draw(img_a,x,y)
text(blendmode, x, y-15)
c.draw(img_a,x,y)
c.text(blendmode, x, y-15)
x += w + 5
if (x > width - w + 5)
if (x > c.width - w + 5)
x = 10
y -= h + 25
end
end
end

# set the image viewer
img = NSImage.alloc.initWithCGImage(canvas.cgimage, size: NSZeroSize)
img.drawAtPoint([0,0], fromRect: NSZeroRect, operation: NSCompositeSourceOver, fraction: 1)
end

end
Expand All @@ -109,6 +100,6 @@ You can see a list of examples in the examples folder, but here is a quick sampl

![MacRuby Image blend modes](http://img.skitch.com/20100712-bedhi8i4ppuqetad263w3ehuna.png)

More output:
More example outputs:

![MacRuby Image color effects](http://img.skitch.com/20100712-jr4jfhbaw2x9nmhy7bscapgbd4.png)
6 changes: 1 addition & 5 deletions examples/canvas_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomView < NSView
include MRGraphics

def drawRect(rect)
canvas = Canvas.for_image(:size => [400,400]) do |c|
Canvas.for_current_context(:size => [400,400]) do |c|
c.background(Color.black)
white = Color.white
c.fill(white)
Expand All @@ -24,10 +24,6 @@ def drawRect(rect)
rand(c.height))
end
end

# set the image viewer
img = NSImage.alloc.initWithCGImage(canvas.cgimage, size: NSZeroSize)
img.drawAtPoint([0,0], fromRect: NSZeroRect, operation: NSCompositeSourceOver, fraction: 1)
end

end
Expand Down
6 changes: 1 addition & 5 deletions examples/image_blend_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CustomView < NSView

def drawRect(rect)
dimensions = [415,730]
canvas = Canvas.for_image(:size => dimensions) do |c|
Canvas.for_current_context(:size => dimensions) do |c|
c.background(Color.white)
c.font('Skia')
c.font_size(14)
Expand Down Expand Up @@ -37,10 +37,6 @@ def drawRect(rect)
end
end
end

# set the image viewer
img = NSImage.alloc.initWithCGImage(canvas.cgimage, size: NSZeroSize)
img.drawAtPoint([0,0], fromRect: NSZeroRect, operation: NSCompositeSourceOver, fraction: 1)
end

end
Expand Down
9 changes: 2 additions & 7 deletions examples/image_colors_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class CustomView < NSView
include MRGraphics

def drawRect(rect)
# set up the canvas
canvas = Canvas.for_rendering(:size => [CGRectGetWidth(rect), CGRectGetHeight(rect)]) do |c|
Canvas.for_current_context(:size => [CGRectGetWidth(rect), CGRectGetHeight(rect)]) do |c|
c.background(Color.white)
c.font('Skia')
c.font_size(14)
Expand Down Expand Up @@ -104,11 +103,7 @@ def drawRect(rect)
c.text("get colors",x,y-15) # add text label
x += x_offset
end

# set the image viewer
img = NSImage.alloc.initWithCGImage(canvas.cgimage, size: NSZeroSize)
img.drawAtPoint([0,0], fromRect: NSZeroRect, operation: NSCompositeSourceOver, fraction: 1)
end
end

end

Expand Down

0 comments on commit 80ec1a1

Please sign in to comment.