RMagick Portfolio - Drawings
Drawings

RMagick supports a full set of 2D drawing methods. Here are some examples of the drawing API.

Go back to the special effects page, or to the transformations page.
Shapes Shapes and lines
require 'RMagick'

canvas = Magick::Image.new(240, 300,
              Magick::HatchFill.new('white','lightcyan2'))
gc = Magick::Draw.new

# Draw ellipse
gc.stroke('red')
gc.stroke_width(3)
gc.fill_opacity(0)
gc.ellipse(120, 150, 80, 120, 0, 270)

# Draw endpoints
gc.stroke('gray50')
gc.stroke_width(1)
gc.circle(120, 150, 124, 150)
gc.circle(200, 150, 204, 150)
gc.circle(120,  30, 124,  30)

# Draw lines
gc.line(120, 150, 200, 150)
gc.line(120, 150, 120,  30)

# Annotate
gc.stroke('transparent')
gc.fill('black')
gc.text(130, 35, "End")
gc.text(188, 135, "Start")
gc.text(130, 95, "'Height=120'")
gc.text(55, 155, "'Width=80'")

gc.draw(canvas)
canvas.write('shapes.gif')
Bezier curves Bezier curves
require 'RMagick'

canvas = Magick::Image.new(240, 300,
              Magick::HatchFill.new('white','lightcyan2'))
gc = Magick::Draw.new

# Draw curve
gc.stroke('blue')
gc.stroke_width(3)
gc.fill_opacity(0)
gc.bezier(45,150, 45,20, 195,280, 195,150)

# Draw endpoints
gc.stroke('gray50')
gc.stroke_width(1)
gc.circle(45,150, 49,150)
gc.circle(195,150, 199,150)

# Draw control points
gc.fill_opacity(1)
gc.fill('gray50')
gc.circle(45,17, 49,17)
gc.circle(195,280, 199,280)

# Connect the points
gc.line(45,150, 45,17)
gc.line(195,280, 195,150)

# Annotate
gc.stroke('transparent').fill('black')
gc.text(27, 175, "'45,150'")
gc.text(175,138, "'195,150'")
gc.text(55,22, "'45,20'")
gc.text(143,285, "'195,280'")
gc.draw(canvas)

canvas.write('bezier.gif')
SVG paths SVG paths
require 'RMagick'

canvas = Magick::Image.new(240, 300,
              Magick::HatchFill.new('white','lightcyan2'))
gc = Magick::Draw.new

gc.fill('red')
gc.stroke('blue')
gc.stroke_width(2)
gc.path('M120,150 h-75 a75,75 0 1, 0 75,-75 z')
gc.fill('yellow')
gc.path('M108.5,138.5 v-75 a75,75 0 0,0 -75,75 z')
gc.draw(canvas)

canvas.write('path.gif')
Polygons Polygons
require 'RMagick'

canvas = Magick::Image.new(240, 300,
              Magick::HatchFill.new('white','lightcyan2'))
gc = Magick::Draw.new

gc.stroke('#001aff')
gc.stroke_width(3)
gc.fill('#00ff00')

x = 120
y = 32.5
gc.polygon(x,    y,     x+29, y+86,  x+109, y+86,
           x+47, y+140, x+73, y+226, x,     y+175,
           x-73, y+226, x-47, y+140, x-109, y+86,
           x-29, y+86)

gc.draw(canvas)
canvas.write('polygon.gif')
<- Special Effects | RMagick Home Page ->