Module: RubyLabs::Canvas

Defined in:
lib/rubylabs.rb

Overview

Canvas

The Canvas module defines a graphics window that can be used for interactive visualizations. Classes in this module describe objects that are drawn in the window; for example, objects of type Canvas::Circle are circles on the canvas.

In the current implementation all drawing objects are derived from a base class named TkObject, which provides an interface to the Tk library of ActiveTcl. Instances of TkObject are proxies for the actual objects defined in Tk. When the user calls a method that opens the RubyLabs Canvas, the method initializes a pipe to a new process running the Tcl shell (wish). If a TkObject is updated, it sends a Tcl command over the pipe, and the wish process updates the window.

Defined Under Namespace

Classes: Circle, Font, Line, Polygon, Rectangle, Text, TkObject

Constant Summary collapse

@@tkpipe =
nil
@@title =
""
@@height =
0
@@width =
0

Class Method Summary collapse

Class Method Details

.closeObject

Send an exit command to the wish shell, which closes the drawing window and terminates the shell.

:call-seq:

Canvas.close()


1193
1194
1195
1196
1197
1198
1199
# File 'lib/rubylabs.rb', line 1193

def Canvas.close
  if @@tkpipe
    @@tkpipe.puts "exit"
    @@tkpipe = nil
    TkObject.reset(nil)
  end
end

.degrees(rad) ⇒ Object

Convert an angle from radians to degrees. Example:

>> Canvas.degrees( Math::PI / 2 )
=> 90.0

:call-seq:

Canvas.degrees(rad) => Float


1337
1338
1339
# File 'lib/rubylabs.rb', line 1337

def Canvas.degrees(rad)
  180 * rad / Math::PI
end

.heightObject

Return the current height of the canvas.

:call-seq:

Canvas.height() => Fixnum


1217
1218
1219
# File 'lib/rubylabs.rb', line 1217

def Canvas.height
  @@height
end

.init(width, height, title, *opts) ⇒ Object

Initialize a drawing canvas with the specified width and height. If this is the first call in an IRB session, open a connection to a wish shell and send it Tcl commands to make a window with a single widget, a canvas centered in the middle. The title argument passed to Canvas.init becomes part of the new window.

If this is not the first call to Canvas.init, the existing Tk canvas is resized according to the new width and height arguments and the window is renamed using the new name.

An optional fourth argument can be the symbol :debug, in which case the return value is the Ruby Pipe object used to communicate with Tk. The Pipe can be useful for developing new TkObject objects, since it can be used to see how Tcl commands are processed. Example:

>> p = Canvas.init(200, 200, "Test", :debug)
=> #<IO:0x1012a1cd0>
>> p.puts ".canvas create text 30 30 -text hello"
=> nil

:call-seq:

Canvas.init(width, height, title, opts = nil)

TODO Read the path to wish from a configuration file, so users can alter it
TODO there are probably other configuration options that can go there, too
TODO use popen3 on OSX, capture stderr


1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
# File 'lib/rubylabs.rb', line 1155

def Canvas.init(width, height, title, *opts)
  
  @@title = "RubyLabs::#{title}"
  @@width = width
  @@height = height
  pad = 12
  
  if @@tkpipe.nil?
    if Canvas.OS == "Windows"
      @@tkpipe = IO.popen("wish", "w")
    elsif Canvas.OS == "Linux"
      @@tkpipe = IO.popen("/opt/ActiveTcl-8.5/bin/wish", "w")
    else
      @@tkpipe = IO.popen("/usr/local/bin/wish", "w")
    end
    at_exit { Canvas.close }
    @@tkpipe.puts "tk::canvas .canvas -bg white -width #{width} -height #{height}"
    @@tkpipe.puts ". configure -bg gray"
    @@tkpipe.puts "pack .canvas -padx #{pad} -pady #{pad}"
    TkObject.reset(@@tkpipe)
  else
    @@tkpipe.puts ".canvas delete all"
    @@tkpipe.puts ".canvas configure -width #{width} -height #{height}"
  end
  
  @@tkpipe.puts "wm geometry . #{width+2*pad}x#{height+2*pad}+30+50"
  @@tkpipe.puts "wm title . #{@@title}"
    
  return opts[0] == :debug ? @@tkpipe : true
end

.move(obj, dx, dy, option = nil) ⇒ Object

Move an object by a distance dx vertically and a distance dy horizontally. If the fourth argument is the symbol :track a line segment is drawn connecting the pen position of the object’s previous location with the pen position of its new location. The return value is an array with the new coordinates.

Example: Suppose x is a Polygon with coordinates (10,10), (20,20), and (30,10). This call moves it down and to the right by 10 pixels and returns the new location:

>> Canvas.move(x, 10, 10)
=> [20, 20, 30, 30, 40, 20]

:call-seq:

Canvas.move(obj, dx, dy, track = nil)


1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
# File 'lib/rubylabs.rb', line 1295

def Canvas.move(obj, dx, dy, option = nil)
  a = obj.coords
  if option == :track
    x0 = a[0] + obj.penpoint[0]
    y0 = a[1] + obj.penpoint[1]
  end
  (0...a.length).step(2) do |i| 
    a[i] += dx
    a[i+1] += dy
  end
  obj.coords = a
  if option == :track
    x1 = a[0] + obj.penpoint[0]
    y1 = a[1] + obj.penpoint[1]
    Canvas::Line.new( x0, y0, x1, y1, :width => 1, :fill => '#777777' )
    obj.raise
  end
  return a
end

.OSObject

Return a string that uses the RUBY_PLATFORM environment variable to determine the host operating system type. Possible return values are “Mac OS X”, “Linux”, or “Windows”.

:call-seq:

Canvas.OS() => String

– TODO: find out what the platform string is for the legacy windows one-click installer; the new installer uses the “mingw” compiler.



1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
# File 'lib/rubylabs.rb', line 1268

def Canvas.OS
  if RUBY_PLATFORM =~ %r{darwin}
    return "Mac OS X"
  elsif RUBY_PLATFORM =~ %r{linux}
    return "Linux"
  elsif RUBY_PLATFORM =~ %r{mingw}
    return "Windows"
  else
    return "Unknown"
  end
end

.palette(first, last, n) ⇒ Object

Make a range of colors starting from first and going to last in n steps. Color arguments should be be 3-tuples of integer RGB values. The result is an array that starts with first, has n-1 intermediate colors, and ends with last.

Example:

>> Canvas.palette( [255,0,0], [0,0,0], 10)
=> ["#FF0000", "#E60000", "#CD0000", ... "#1E0000", "#000000"]

The return value is an array of 11 colors starting with red and ending with black.

:call-seq:

Canvas.palette(first, last, n) => Array


1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
# File 'lib/rubylabs.rb', line 1355

def Canvas.palette(first, last, n)
  d = Array.new(3)
  3.times { |i| d[i] = (first[i] - last[i]) / n }
  a = [first]
  (n-1).times do |i|
    a << a.last.clone 
    3.times { |j| a.last[j] -= d[j] }
  end
  a << last
  a.map { |c| sprintf("#%02X%02X%02X",c[0],c[1],c[2]) }
end

.pipeObject

Return a reference to the Pipe object used to communicate with the wish shell.

:call-seq:

Canvas.pipe() => IO


1227
1228
1229
# File 'lib/rubylabs.rb', line 1227

def Canvas.pipe
  @@tkpipe
end

.radians(deg) ⇒ Object

Convert an angle from degrees to radians. Example:

>> Math::PI / 4
=> 0.785398163397448
>> Canvas.radians(45)
=> 0.785398163397448

:call-seq:

Canvas.radians(deg) => Float


1325
1326
1327
# File 'lib/rubylabs.rb', line 1325

def Canvas.radians(deg)
  deg * Math::PI / 180
end

.widthObject

Return the current width of the canvas.

:call-seq:

Canvas.width() => Fixnum


1207
1208
1209
# File 'lib/rubylabs.rb', line 1207

def Canvas.width
  @@width
end