omnicanvas.canvas
(The canvas)¶
This module contains the main Canvas class.
-
class
omnicanvas.canvas.
Canvas
(width, height, background_color=None)[source]¶ A backdrop on which other
Graphic
objects are painted.Parameters: - width – The canvas’s width in pixels.
- height – The canvas’s height in pixels.
- background_color – The canvas’s background colour - the default is white
-
width
(width=None)[source]¶ The canvas’s width in pixels. Passing a value will update the width property.
Parameters: width – If given, the canvas’s width will be set to this. Return type: int
-
height
(height=None)[source]¶ The canvas’s height in pixels. Passing a value will update the height property.
Parameters: height – If given, the canvas’s height will be set to this. Return type: int
-
background_color
(background_color=None)[source]¶ The canvas’s background colour, as a hex string. Passing a value will update the background_color property (as a hex string).
Parameters: background_color (str) – If given, the canvas’s background_color will be set to this. Return type: str
-
get_graphic_by_name
(name)[source]¶ Searches the canvas’s
Graphic
objects and returns the first one with a matching name. ReturnsNone
if there are no matches.Parameters: name (str) – The name to search by. Return type: str
-
get_graphics_by_name
(name)[source]¶ Searches the canvas’s
Graphic
objects and returns all the ones with a matching name. Returns an empty list if there are no matches.Parameters: name (str) – The name to search by. Returns: list
ofGraphic
-
add_rectangle
(*args, **kwargs)[source]¶ Adds a
Rectangle
to the canvas.Parameters: - x – The x-coordinate of the Rectangle’s upper left corner.
- y – The y-coordinate of the Rectangle’s upper left corner.
- width – The Rectangle’s width.
- height – The Rectangle’s height.
- fill_color (str) – The Rectangle’s interior colour.
- opacity – The degree of transparency, from 0 to 1 (0 being invisible).
- line_width – The width of the edge of the Rectangle in pixels.
- line_style (str) – The pattern of the edges. Acceptable values are
-
(default),..
(dotted) or--
(dashed). - line_color (str) – The colour of the edge.
- rotation (tuple) – Any rotation to be applied, in the format (x of rotation point, y of rotation point, angle).
- data (dict) – Any data to be associated with the Rectangle.
Return type:
-
add_line
(*args, **kwargs)[source]¶ Adds a
Line
to the canvas.Parameters: - x1 – The x-coordinate of the Line’s start point.
- y1 – The y-coordinate of the Line’s start point.
- x2 – The x-coordinate of the Line’s end point.
- y2 – The y-coordinate of the Line’s end point.
- line_width – The width of the Line in pixels.
- line_style (str) – The pattern of the Line. Acceptable values are
-
(default),..
(dotted) or--
(dashed). - line_color (str) – The colour of the Line.
- rotation (tuple) – Any rotation to be applied, in the format (x of rotation point, y of rotation point, angle).
- data (dict) – Any data to be associated with the Line.
Return type:
-
add_polygon
(*args, **kwargs)[source]¶ Adds a
Polygon
to the canvas.Parameters: - *points – The alternating x and y values of the Polygon’s corners.
- fill_color (str) – The Polygon’s interior colour.
- opacity – The degree of transparency, from 0 to 1 (0 being invisible).
- line_width – The width of the edge of the Polygon in pixels.
- line_style (str) – The pattern of the edges. Acceptable values are
-
(default),..
(dotted) or--
(dashed). - line_color (str) – The colour of the edge.
- rotation (tuple) – Any rotation to be applied, in the format (x of rotation point, y of rotation point, angle).
- data (dict) – Any data to be associated with the Polygon.
Return type:
-
add_text
(*args, **kwargs)[source]¶ Adds a
Text
to the canvas.Parameters: - x – The Text’s x location.
- y – The Text’s y location.
- text (str) – The text to display.
- font_size – The font size of the Text when displayed.
- horizontal_align – The horizontal alignment of the Text. Acceptable values are
left
,center
(default) andright
. - vertical_align – The vertical alignment of the Text. Acceptable values are
top
,middle
(default) andbottom
. - fill_color (str) – Defaults to ‘#FFFFFF’.
- opacity – The degree of transparency, from 0 to 1 (0 being invisible).
- line_width – Defaults to 0.
- line_style (str) – The line pattern. Acceptable values are
-
(default),..
(dotted) or--
(dashed). - line_color (str) – Defaults to ‘#000000’.
- rotation (tuple) – Any rotation to be applied, in the format (x of rotation point, y of rotation point, angle), in degrees.
- data (dict) – Any data to be associated with the Text.
Return type:
-
add_polyline
(*args, **kwargs)[source]¶ Adds a
Polyline
to the canvas.Parameters: - *points – The alternating x and y values of the Polyline’s corners.
- line_width – The width of the edge of the Polyline in pixels.
- line_style (str) – The pattern of the edges. Acceptable values are
-
(default),..
(dotted) or--
(dashed). - line_color (str) – The colour of the edge.
- rotation (tuple) – Any rotation to be applied, in the format (x of rotation point, y of rotation point, angle).
- data (dict) – Any data to be associated with the Polyline.
Return type:
-
save
(path)[source]¶ Saves the canvas to file as an SVG file.
Parameters: path (str) – The location and filename to save to.
-
to_svg
(canvas)¶ Returns the SVG text of the canvas.
Any
data
attributes of the Graphics contained will be rendered as SVG attributes.Return type: str