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
-
.close ⇒ Object
Send an
exit
command to the wish shell, which closes the drawing window and terminates the shell. -
.degrees(rad) ⇒ Object
Convert an angle from radians to degrees.
-
.height ⇒ Object
Return the current height of the canvas.
-
.init(width, height, title, *opts) ⇒ Object
Initialize a drawing canvas with the specified width and height.
-
.move(obj, dx, dy, option = nil) ⇒ Object
Move an object by a distance
dx
vertically and a distancedy
horizontally. -
.OS ⇒ Object
Return a string that uses the
RUBY_PLATFORM
environment variable to determine the host operating system type. -
.palette(first, last, n) ⇒ Object
Make a range of colors starting from
first
and going tolast
inn
steps. -
.pipe ⇒ Object
Return a reference to the Pipe object used to communicate with the wish shell.
-
.radians(deg) ⇒ Object
Convert an angle from degrees to radians.
-
.width ⇒ Object
Return the current width of the canvas.
Class Method Details
.close ⇒ Object
Send an exit
command to the wish shell, which closes the drawing window and terminates the shell.
:call-seq:
Canvas.close()
1186 1187 1188 1189 1190 1191 1192 |
# File 'lib/rubylabs.rb', line 1186 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
1330 1331 1332 |
# File 'lib/rubylabs.rb', line 1330 def Canvas.degrees(rad) 180 * rad / Math::PI end |
.height ⇒ Object
1210 1211 1212 |
# File 'lib/rubylabs.rb', line 1210 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
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 |
# File 'lib/rubylabs.rb', line 1148 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)
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 |
# File 'lib/rubylabs.rb', line 1288 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 |
.OS ⇒ Object
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.
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 |
# File 'lib/rubylabs.rb', line 1261 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
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
# File 'lib/rubylabs.rb', line 1348 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 |
.pipe ⇒ Object
Return a reference to the Pipe object used to communicate with the wish shell.
:call-seq:
Canvas.pipe() => IO
1220 1221 1222 |
# File 'lib/rubylabs.rb', line 1220 def Canvas.pipe @@tkpipe end |