Skip to content

A Python class for drawing polygons and saving as an SVG file

Notifications You must be signed in to change notification settings

timojuez/svg-polygons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

svg-polygons

A Python class for drawing polygons and saving as an SVG file

Usage

First import the module:

import svg_polygons

Let's say you want to draw two triangles. These should be represented as a list of three tuples. Each tuple gives the x and y coordinates for a vertex of the triangle.

triangle1 = [(100, 70), (325, 210), (60, 300)]
triangle2 = [(455, 346), (39, 231), (80, 312)]

Now create a Canvas object specifying its width and height (in this case the canvas is 500×500):

my_drawing = svg_polygons.Canvas(500, 500)

Now you can draw your triangles to the canvas, optionally specifying a border colour, fill colour, and opacity level:

my_drawing.polygon(triangle1, 'red', 'blue', 1.0)
my_drawing.polygon(triangle2, 'green', 'yellow', 0.75)

If you want to draw a circle, specify the position of the circle and its radius, followed by the border colour, fill colour and opacity level:

my_drawing.circle((250, 250), 20, 'black', 'black', 0.5)

Finally, save your drawing to an SVG file. See example_drawing.svg (part of this repo) for the resulting image.

my_drawing.save('example_drawing')

If you need to clear your canvas, use:

my_drawing.clear()

If you don't want a border or fill colour, use None to leave it transparent:

my_drawing.draw(triangle1, 'red', None)

You can also specify colours using a hex triplet:

my_drawing.polygon(triangle1, 'black', '#2E578C', 0.8)

Notes

Be aware that svg-polgons follows the convention of placing the origin (0, 0) in the top left corner of the canvas, not the bottom left.

About

A Python class for drawing polygons and saving as an SVG file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages