Kevin Ryde
and 1 contributors


Image::Base::Tk::Canvas -- draw into Tk::Canvas


 use Image::Base::Tk::Canvas;
 my $image = Image::Base::Tk::Canvas->new (-for_widget => $parent,
                                           -width => 100,
                                           -height => 100);
 $image->rectangle (0,0, 99,99, 'white');
 $image->xy (20,20, 'black');
 $image->line (50,50, 70,70, '#FF00FF');
 $image->line (50,70, 70,50, '#0000AAAA9999');
 $image->save ('/some/filename.eps');


Image::Base::Tk::Canvas is a subclass of Image::Base,



Image::Base::Tk::Canvas extends Image::Base to add items to a Tk::Canvas widget.

There's no file reading, but encapsulated postscript (EPS) can be written, or "fig" format (as for the xfig program) if you have Tk::CanvasFig.

Tk::Canvas has many more features than available here, but this module is a cute way to point some Image::Base code at a canvas. There's no limit on how many items a canvas can hold, in principle, but if drawing lots of individual pixels then Tk::Photo and Image::Base::Tk::Photo may be better.


Colour names are anything recognised by Tk_GetColor(3tk),

    X server names      usually /etc/X11/rgb.txt
    #RGB                hex
    #RRGGBB             hex
    #RRRGGGBBB          hex
    #RRRRGGGGBBBB       hex

The hex forms end up going to Xlib which means the shorter ones are padded with zeros, so "#FFF" is only "#F000F000F000" which is a light grey rather than white (see X(7) "COLOR NAMES").


See "FUNCTIONS" in Image::Base for the behaviour common to all Image-Base classes.

$image = Image::Base::Tk::Canvas->new (key=>value,...)

Create and return a new canvas image object. A new canvas can be created with -width, -height, and a -for_widget which is its parent

    $image = Image::Base::Tk::Canvas->new (-for_widget => $parent,
                                           -width => 200,
                                           -height => 100);

Or an existing Tk::Canvas object can be given,

    $image = Image::Base::Tk::Canvas->new (-tkcanvas => $tkcanvas);
$colour = $image->xy ($x, $y)
$image->xy ($x, $y, $colour)

Get or set an individual pixel.

Getting a pixel is currently implemented by a find() of an item at $x,$y and picking out its colour. This works well enough for the item types added by this module but might not work for others -- in particular an item's outline is not distinguished from its fill interior. "window" items are examined with a Tk::WinPhoto and may be a bit slow, and could even induce an Xlib error if the window is off the edge of the screen (would like WinPhoto to avoid that for the benefit of all WinPhoto uses). "bitmap" items are not read at all yet.

$image->diamond ($x0, $y0, $x1, $y1, $colour)

Draw a diamond shape within the rectangle top left ($x0,$y0) and bottom right ($x1,$y1) using $colour. If optional argument $fill is true then the diamond is filled.

In the current code a filled diamond uses a "polygon" item but an unfilled uses a "line" segments item. Line segments ensure interior points are not part of the diamond for the purposes of find("overlapping") etc, the same as from an unfilled ellipse or rectangle. Is that the best way?

$image->load ()
$image->load ($filename)

There's no file reading for a canvas.

$image->save ()
$image->save ($filename)

Save the canvas to -file, or with a $filename argument set -file then save to that.

-file_format below controls the output format. The default "eps" is encapsulated postscript using $tkcanvas->postscript(). It might be limited to items currently visible in the window. The postscript() method has various options not available with this save() and can of course be used directly.

Format "fig" uses $tkcanvas->fig() from Tk::CanvasFig if available, to produce fig files for the xfig program.


-width (integer)
-height (integer)

Setting these changes the size of the image.


The underlying Tk::Canvas object.

-file_format (string, default "eps")

The file format for saving, as a string either

    "eps"     encapsulated postscript
    "fig"     xfig format, using Tk::CanvasFig


Tk::Canvas, Image::Base, Image::Base::Tk::Photo

Tk::CanvasFig, xfig(1)



Image-Base-Tk is Copyright 2010, 2011, 2012 Kevin Ryde

Image-Base-Tk is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Image-Base-Tk is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Image-Base-Tk. If not, see <>.