Canvas¶
A canvas is the central workspace in PyX. It acts both as a drawing
surface—providing methods to stroke, fill, and draw paths and to typeset
text—and as a hierarchical container that groups canvas items (paths, text boxes,
sub-canvases, bitmaps, and embedded EPS, PDF, or SVG files) with shared visual
attributes, transformations, clipping paths, and drawing layers.
Working with canvases¶
Drawing and grouping¶
Every drawing operation on a canvas (canvas.stroke(), canvas.fill(),
canvas.draw(), canvas.text()) creates a baseclasses.canvasitem
and inserts it into the canvas via canvas.insert(), returning the inserted
item.
Because a canvas is itself a baseclasses.canvasitem, you can
create sub-canvases, draw on them in local coordinates, and insert them into a
parent canvas with transformations or styles applied to the entire group:
from pyx import *
# Create a reusable sub-canvas in local coordinates
badge = canvas.canvas()
badge.fill(path.circle(0, 0, 0.5), [color.rgb.red])
badge.text(0, 0, "1", [text.halign.center, text.valign.middle, color.gray.white])
# Insert the sub-canvas multiple times at different positions and scales
c = canvas.canvas()
c.insert(badge, [trafo.translate(1, 1)])
c.insert(badge, [trafo.scale(1.5), trafo.translate(3, 1)])
c.writePDFfile("badges")
Layers¶
By default, items on a canvas are drawn in the exact order in which they are
inserted (later items paint over earlier items). When constructing complex
figures, it is often convenient to organize drawing operations into named
layers (such as "background", "fill", "main", "labels") whose
back-to-front order is independent of when individual items are added.
The canvas.layer() method creates or retrieves a sub-canvas associated with
a layer name and allows you to position it relative to existing layers using the
above and below parameters:
c = canvas.canvas()
bg = c.layer("background")
fg = c.layer("foreground", above="background")
# Even if we draw on fg first and bg second, bg is rendered behind fg
fg.text(1, 1, "Foreground text", [text.halign.center, text.valign.middle])
bg.fill(path.rect(0, 0.5, 2, 1), [color.rgb.yellow])
Clipping¶
A canvas can be clipped to the interior of an arbitrary closed path.path
by passing a clip instance in the canvas constructor’s attrs list:
clipped = canvas.canvas([canvas.clip(path.circle(1, 1, 1))])
clipped.fill(path.rect(0, 0, 2, 2), [color.rgb.blue])
c = canvas.canvas()
c.insert(clipped)
Class canvas¶
- class canvas.canvas(attrs=[], textengine=None)¶
Constructs a new canvas.
attrs: Optional list of canvas-wide attributes applied to all contents of the canvas. Allowed attributes are
style.strokestyle,style.fillstyle(includingcolor.colorandpattern.pattern),trafo.trafo_pt, andclip.textengine: Optional text engine instance (such as a custom
text.TexEngine,text.LatexEngine, ortext.UnicodeEngine) used bytext()andtext_pt()on this canvas. WhenNone(the default),text.defaulttextengineis used at the timetext()is called.
- items¶
List of
baseclasses.canvasitemobjects contained in the canvas.
- layers¶
Dictionary mapping layer names to their corresponding sub-canvas instances.
- trafo¶
The cumulative
trafo.trafo_pttransformation applied to the canvas.
- bbox()¶
Returns the enclosing
bbox.bboxof all items in the canvas (taking canvas transformations and anyclippath into account). Note that for stroked paths, the bounding box reflects the geometric path rather than the stroke width.
- draw(path, attrs)¶
Draws path on the canvas according to the attributes in attrs, which may contain
baseclasses.deformer,baseclasses.deco,style.strokestyle,style.fillstyle, andtrafo.trafo_ptinstances. Returns the inserteddeco.decoratedpathcanvas item.
- stroke(path, attrs=[])¶
Strokes path on the canvas; equivalent to
self.draw(path, [deco.stroked] + list(attrs)). Returns the inserteddeco.decoratedpathcanvas item.
- fill(path, attrs=[])¶
Fills path on the canvas; equivalent to
self.draw(path, [deco.filled] + list(attrs)). Returns the inserteddeco.decoratedpathcanvas item.
- insert(item, attrs=[])¶
Inserts item (an instance of
baseclasses.canvasitem, such as anothercanvas, atext.textbox_pt, abitmap.bitmap, anepsfile.epsfile, apdffile.pdffile, or ansvgfile.svgfile) into the canvas. If attrs is non-empty, item is wrapped in a sub-canvas with attrs applied. Returns item.
- layer(name, above=None, below=None)¶
Creates or retrieves the layer sub-canvas identified by the string name. Dotted names (such as
"axes.ticks") create nested sub-layers inside the parent layer"axes".above: Name of an existing layer immediately above which the layer should be positioned.
below: Name of an existing layer immediately below which the layer should be positioned (cannot be combined with above).
Returns the
canvasinstance representing the layer.
- text(x, y, expr, textattrs=[], **kwargs)¶
- text_pt(x_pt, y_pt, expr, textattrs=[], **kwargs)¶
Typesets expr at \((x, y)\) (in PyX user lengths or points) using the canvas’s text engine, inserts the resulting text box into the canvas, and returns it. See
text.SingleEngine.text_pt()for details on textattrs and kwargs.
- settextengine(textengine)¶
Sets the text engine (
text.TexEngine,text.LatexEngine, ortext.UnicodeEngine) to be used by subsequenttext()andtext_pt()calls on this canvas.
Output and Ghostscript conversion methods¶
For single-page figures, canvas provides shortcut methods that wrap the
canvas in a document.page and a single-page document.document
and write the output directly. Any keyword arguments accepted by
document.page (such as paperformat, rotated, centered,
fittosize, margin, bboxenlarge, bbox) as well as any keyword
arguments accepted by the respective writer (such as title, stripfonts,
textaspath, writebbox; see Documents) can be passed directly to
these methods:
- canvas.writeEPSfile(file=None, **kwargs)¶
- canvas.writePSfile(file=None, **kwargs)¶
- canvas.writePDFfile(file=None, **kwargs)¶
- canvas.writeSVGfile(file=None, **kwargs)¶
Writes the canvas to an EPS, PostScript, PDF, or SVG file. file can be a filename string (the extension
.eps,.ps,.svgis appended automatically if omitted), a binary writable stream,"-"to write tosys.stdout.buffer, orNoneto derive the filename automatically fromsys.argv[0].
- canvas.writetofile(filename, **kwargs)¶
Determines the output format from the extension of filename (
.eps,.ps,.svg) and writes the canvas accordingly.
- canvas.pipeGS(filename=None, device='png16m', resolution=100, gs='gs', gsoptions=[], textalphabits=4, graphicsalphabits=4, ciecolor=False, input='eps', **kwargs)¶
Renders the canvas using Ghostscript (
gs) into any output format supported by Ghostscript (such as PNG or JPEG).
filename: Output filename. If filename does not end with a file extension and device starts with
"png"or"jpeg", the extension.pngor.jpgis appended automatically. If filename isNone, the base name is derived fromsys.argv[0]. If filename is"-", Ghostscript writes to standard output andpipeGS()returns the resulting output as abytesobject!device: Ghostscript output device (default
"png16m"for 24-bit RGB PNG; use e.g."pngalpha"for PNG with transparent background or"jpeg"for JPEG).resolution: Rendering resolution in DPI (default
100).gs: Ghostscript executable name (default
"gs").gsoptions: Additional command-line option strings passed to Ghostscript.
textalphabits, graphicsalphabits: Antialiasing subsampling factors (default
4; passNoneto omit).ciecolor: When
True, passes-dUseCIEColorto Ghostscript.input: Format piped from PyX into Ghostscript:
"eps"(default) or"pdf".kwargs: Additional keyword arguments forwarded to
document.pageand the EPS/PDF writer.