Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Goo Canvas Scaling and X-Y coordinates

by renegadex (Beadle)
on Jul 28, 2008 at 04:59 UTC ( #700459=perlquestion: print w/replies, xml ) Need Help??

renegadex has asked for the wisdom of the Perl Monks concerning the following question:

i was using the scale method of goo canvas and when i increase the scale by 1% the item moves by a certain amount. is there a way to avoid the moving? i was trying to counter the moving by using the translate method. but i couldnt get the right amount. also is there a way to get the x and y coordinate of an item inside the canvas? and also getting the x and y coordinates of the mouse inside the item. meaning the 0,0 coordinate is located at the top left most corner of the item.

Replies are listed 'Best First'.
Re: Goo Canvas Scaling and X-Y coordinates
by zentara (Archbishop) on Jul 28, 2008 at 13:41 UTC
    Scaling and rotations can be tricky because they are predefined to the group's origin (usually root group at 0,0, but it may be the canvas center point, depends on the canvas type and setup). The first time you try to rotate something, people ar usually surprised to see the item fly off the screen because the $cx and $cy is 0,0 (unless you moved the root group). I havn't tried it yet, but it looks like you want to use
    $item->set_simple_transform ($x, $y, $scale, $rotation)
    where $x and $y will be the center point of the scale; presumably the weighted center of your item. Computing the weighted center can be tricky for odd shapes, so you may have to resort to making a special group for each item to be scaled, add the item to the group centered, move the group, and scale it.

    It's one of those things you just need to play with to get right. Look at the 'animation' section of the Goo demo.

    As far as the x,y coordinates of the mouse I think you need to setup an event handler on the canvas and the items.

    sub event_handler{ my ( $widget, $event ) = @_; print $widget ,' ',$event->type,"\n"; my ($x,$y) = ($event->x,$event->y); print "$x $y\n"; }
    Once again, look at the demo and see how it does the Events tab.

    Just to show you how difficult a centered rotation can be, here is an example on the Gnome2::Canvas, you will then appreciate Goo's set_simple_transform :-)


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Goo Canvas Scaling and X-Y coordinates
by zentara (Archbishop) on Jul 28, 2008 at 19:27 UTC
    Here is an example of doing a centered rotation in Goo. There is a line in the timeout, that does a set_simple_transform.
    #!/usr/bin/perl -w use strict; use warnings; use Goo::Canvas; use Gtk2 '-init'; use Glib qw(TRUE FALSE); my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_size_request(640, 600); my $swin = Gtk2::ScrolledWindow->new; $swin->set_shadow_type('in'); $window->add($swin); my $canvas = Goo::Canvas->new(); $canvas->set_size_request(800, 650); $canvas->set_bounds(0, 0, 1000, 1000); $swin->add($canvas); my $root = $canvas->get_root_item(); # first offset set my $pts_ref = [50,50,180,120,90,100,50,50]; my $line = Goo::Canvas::Polyline->new( $root, TRUE, $pts_ref, 'stroke-color' => 'black', 'line-width' => 3, 'fill-color-rgba' => 0x3cb37180, ); my ($midx, $midy) = _get_CM( @$pts_ref ); my $ellipse = Goo::Canvas::Ellipse->new( $root, $midx-2, $midy-2,$midx+2, $midy+2, 'stroke-color' => 'goldenrod', 'line-width' => 8 ); my $ellipse1 = Goo::Canvas::Ellipse->new( $root, -2, -2, +2, +2, 'stroke-color' => 'black', 'line-width' => 4 ); $ellipse1->translate($midx,$midy); # end first set my $xd = 100; my $yd = 100; $root->translate($xd,$yd); my $id = Glib::Timeout->add (10, sub { $xd += .1; $yd += .1; $line->rotate (10, $midx, $midy); $root->set_simple_transform ($xd, $yd,1,0); #or a group # $item->set_simple_transform ($x, $y, $scale, $rotation) + return 1; }); $window->show_all(); Gtk2->main; ################################################ # This sub finds the center of mass of a polygon. # I grabbed the algorithm somewhere from the web. # I grabbed it from Ala Qumsieh's RotCanvas :-) sub _get_CM { my ($x, $y, $area); my $i = 0; while ($i < $#_) { my $x0 = $_[$i]; my $y0 = $_[$i+1]; my ($x1, $y1); if ($i+2 > $#_) { $x1 = $_[0]; $y1 = $_[1]; } else { $x1 = $_[$i+2]; $y1 = $_[$i+3]; } $i += 2; my $a1 = 0.5*($x0 + $x1); my $a2 = ($x0**2 + $x0*$x1 + $x1**2)/6; my $a3 = ($x0*$y1 + $y0*$x1 + 2*($x1*$y1 + $x0*$y0))/6; my $b0 = $y1 - $y0; $area += $a1 * $b0; $x += $a2 * $b0; $y += $a3 * $b0; } return split ' ', sprintf "%.0f %0.f" => $x/$area, $y/$area; } ############################################# __END__

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Goo Canvas Scaling and X-Y coordinates
by Anonymous Monk on Jul 28, 2008 at 06:23 UTC
    i was using the scale method of goo canvas and when i increase the scale by 1% the item moves by a certain amount. is there a way to avoid the moving?

    Don't scale the canvas, scale the item.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700459]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-05-25 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?