From: Hidetoshi NAGAI Date: 2011-02-28T00:52:59+09:00 Subject: Re: tk canvas drag items Hi, From: Ignatz Hafflegab Subject: tk canvas drag items Date: Sat, 26 Feb 2011 13:38:03 +0900 Message-ID: > Could somebody kindly direct me to a Ruby Tk example of dragging a > tagged canvas item using canvas coordinates? > > There are one or two examples supplied in Ruby 1.8.6 (Probably thanks to > Nagai-san) that allow for selecting canvas items and dragging them, but > they use the window's coordinates, and I can't figure out how to change > the code so it uses canvasx and canvasy. Could you tell me your purpose? If you want to drag items only, you don't need coordinates but difference of coordinates to move. The difference doesn't depend on whether it is based on canvas coordinates or screen coordinates (see ext/tk/sample/demoes-en/plot.rb on ruby's source tree). > Also, I'd like to know how to query a canvas item to determine its x > or y coordinate. For example, -------------------------------------------------------------- c = TkCanvas.new ... c.bind('1', :x, :y){|x,y| # (x,y) is screen coordinates cx = c.canvasx x # get canvas x coordinate cy = c.canvasy y # get canvas y coordinate p c.find_closest(cx,cy) # find an item which is closest to (cx,cy) p c.find_overlapping(cx,cy,cx,cy) # find items which is under (cx,cy) p c.find_overlapping(cx-2,cy-2,cx+2,cy+2) # find items loosely about clicked position } -------------------------------------------------------------- If you find items which have given coords in coords list, -------------------------------------------------------------- c.find_overlapping(cx,cy,cx,cy).find_all{|item| item.coords.each_slice(2).collect.include? [cx, cy] } -------------------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp) Department of Artificial Intelligence, Kyushu Institute of Technology