# Ruby Tk Canvas Drawing

**URL:** https://rubytalk.org/t/ruby-tk-canvas-drawing/45637
**Category:** ruby-talk
**Created:** [3 April 2008 15:09 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637 "2008-04-03T15:09:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bryan\_Richardson1](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bryan\_Richardson1](https://rubytalk.org/u/Bryan_Richardson1)
#### Post date: [3 April 2008 15:09 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637/1 "2008-04-03T15:09:18Z")

</div>

Hello all,

I'm trying to draw a network graph in Ruby using Tk. I've figured out  
how to create the canvas and draw the nodes and edges. I'm in the  
process of implementing a spring graph layout algorithm, and I'm  
wondering how I can redraw the nodes and edges at new coordinates when  
the new coordinates are calculated using the spring algorithm. I had  
planned on creating a new thread every half second or so to call the  
spring algorithm and repaint the nodes and edges.

Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?

> **···**
>
> --  
> Thanks!  
> Bryan  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Jayson\_Williams](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jayson\_Williams](https://rubytalk.org/u/Jayson_Williams)
#### Post date: [3 April 2008 16:54 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637/2 "2008-04-03T16:54:04Z")

</div>

I believe you can tag objects on the canvas, and designate x & y  
positions for the tags. If you tag your 'oval' you should be able to  
move it wherever you want. There are also two very good resources for  
Tk development in Ruby. They are both groups on Google. The first  
group is called "Ruby and the Tk Toolkit". The other is called "Tk  
Documentation and Resources". You should be able to get considerable  
help between these two groups.

--Jay

> **···**
>
> On Thu, Apr 3, 2008 at 11:09 AM, Bryan Richardson \<btrichardson@gmail.com\> wrote:
> 
> > Hello all,
> > 
> > Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?
> > 
> > --  
> > Thanks!  
> > Bryan  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)
#### Post date: [3 April 2008 17:16 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637/3 "2008-04-03T17:16:03Z")

</div>

Message-ID: \<c60598da19be2f08aeb92d8ab1e6fc1b@ruby-forum.com\>

> Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?

For example,

> **···**
>
> From: Bryan Richardson \<btrichardson@gmail.com\>  
> Subject: Ruby Tk Canvas Drawing  
> Date: Fri, 4 Apr 2008 00:09:18 +0900  
> ------------------------------------------------------------  
> canvas = TkCanvas.new.pack
> 
> tag = TkcTag.new(canvas)
> 
> oval = TkcOval.new(canvas, [100,100], [150,150], :tags=\>tag)  
> line = TkcLine.new(canvas, [100,100], [150,150], :tags=\>tag)
> 
> oval.move(20, -15) # move  
> line.move(20, -15) # move
> 
> tag.move(30, 20) # move items with tag
> 
> oval.coords([50,100], [200,140]) # transform  
> line.coords([50,50], [100,50], [50,100], [200,140]) # transform  
> ------------------------------------------------------------  
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

### Author: ![Bryan\_Richardson1](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bryan\_Richardson1](https://rubytalk.org/u/Bryan_Richardson1)
#### Post date: [3 April 2008 21:35 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637/4 "2008-04-03T21:35:55Z")

</div>

Thanks for the help guys. Is it possible to only move one end of a line  
without moving the other end?

> **···**
>
> --  
> Bryan
> 
> Hidetoshi NAGAI wrote:
> 
> > From: Bryan Richardson \<btrichardson@gmail.com\>  
> > Subject: Ruby Tk Canvas Drawing  
> > Date: Fri, 4 Apr 2008 00:09:18 +0900  
> > Message-ID: \<c60598da19be2f08aeb92d8ab1e6fc1b@ruby-forum.com\>
> > 
> > > Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?
> > 
> > For example,  
> > ------------------------------------------------------------  
> > canvas = TkCanvas.new.pack
> > 
> > tag = TkcTag.new(canvas)
> > 
> > oval = TkcOval.new(canvas, [100,100], [150,150], :tags=\>tag)  
> > line = TkcLine.new(canvas, [100,100], [150,150], :tags=\>tag)
> > 
> > oval.move(20, -15) # move  
> > line.move(20, -15) # move
> > 
> > tag.move(30, 20) # move items with tag
> > 
> > oval.coords([50,100], [200,140]) # transform  
> > line.coords([50,50], [100,50], [50,100], [200,140]) # transform
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)
#### Post date: [4 April 2008 01:44 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637/5 "2008-04-04T01:44:11Z")

</div>

Message-ID: \<b5c8f55b404ca2eaf9aaef3291d3f857@ruby-forum.com\>

> Thanks for the help guys. Is it possible to only move one end of a line  
> without moving the other end?

Of course. 🙂

> **···**
>
> From: Bryan Richardson \<btrichardson@gmail.com\>  
> Subject: Re: Ruby Tk Canvas Drawing  
> Date: Fri, 4 Apr 2008 06:35:55 +0900  
> ---------------------------------------------------------------------  
> def move\_tail(line, dx, dy)  
> &nbsp;&nbsp;coords = line.coords  
> &nbsp;&nbsp;coords[-2] += dx  
> &nbsp;&nbsp;coords[-1] += dy  
> &nbsp;&nbsp;line.coords = coords  
> end
> 
> def set\_tail(line, x, y)  
> &nbsp;&nbsp;coords = line.coords  
> &nbsp;&nbsp;coords[-2] = x  
> &nbsp;&nbsp;coords[-1] = y  
> &nbsp;&nbsp;line.coords = coords  
> end
> 
> def add\_tail(line, x, y)  
> &nbsp;&nbsp;line.coords \<\<= [x, y]  
> end
> 
> def remove\_tail(line)  
> &nbsp;&nbsp;coords = line.coords  
> &nbsp;&nbsp;coords.pop; coords.pop  
> &nbsp;&nbsp;line.coords = coords  
> end  
> ---------------------------------------------------------------------  
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

### Author: ![Bryan\_Richardson1](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@Bryan\_Richardson1](https://rubytalk.org/u/Bryan_Richardson1)
#### Post date: [4 April 2008 02:54 UTC](https://rubytalk.org/t/ruby-tk-canvas-drawing/45637/6 "2008-04-04T02:54:43Z")

</div>

Ah yes, I see. I guess I should have asked if there was a 'built-in'  
method for moving just one end of a line. Thanks for the code though!  
🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
