From: Hidetoshi NAGAI Date: 2007-10-12T13:42:49+09:00 Subject: Re: ruby tk drawing a line From: Ed Redman Subject: ruby tk drawing a line Date: Fri, 12 Oct 2007 12:50:10 +0900 Message-ID: > canvas.bind('B1-Motion', proc { |e| > $endx = e.x > $endy = e.y > > $a = TkcLine.new(canvas, $startx, $starty, $endx, $endy) You write so many new lines at here. Each short line is made at each 'B1-Motion' callback. The following may be an example which you want. ----------------------------------------------------------------- require 'tk' line = nil first = nil last = nil canvas = TkCanvas.new(:relief=>:ridge, :borderwidth=>3).pack f = TkFrame.new.pack(:fill=>:x) color = 'black' TkButton.new(f, :text=>'color', :command=>proc{ color = Tk.chooseColor(:initialcolor=>color) }).pack(:side=>:right, :padx=>[10,3]) option = TkSpinbox.new(f, :from=>1, :to=>1000, :width=>4).pack(:side=>:right, :padx=>0) TkLabel.new(f, :text=>'width').pack(:side=>:right, :padx=>[10,0]) canvas.bind('1', '%x', '%y'){|*first| line = TkcLine.new(canvas, first, first, :width=>option.value, :fill=>color) } canvas.bind('B1-Motion', '%x', '%y'){|*last| line.coords(first, last)} Tk.mainloop ----------------------------------------------------------------- Ruby/Tk accepts all of the following coords pattern for a canvas item. (1) x1, y1, x2, y2, x3, y3, ... (2) [x1, y1], [x2, y2], [x3, y3], ... (3) [ [x1, y1], [x2, y2], [x3, y3], ... ] In my example, I use the pattern (2). -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)