···
1
+
Most common SVG tags are supported and others can easily be added by
2
+
writing a small subclass of ``DrawableBasicElement`` or
3
+
``DrawableParentElement``.
8
+
drawSvg is available on PyPI:
12
+
$ pip3 install drawSvg
17
+
Basic drawing elements
18
+
~~~~~~~~~~~~~~~~~~~~~~
22
+
d = draw.Drawing(200, 100, origin='center')
24
+
d.append(draw.Lines(-80, -45,
32
+
d.append(draw.Rectangle(0,0,40,50, fill='#1248ff'))
33
+
d.append(draw.Circle(-40, -10, 30,
34
+
fill='red', stroke_width=2, stroke='black'))
36
+
p = draw.Path(stroke_width=2, stroke='green',
37
+
fill='black', fill_opacity=0.5)
38
+
p.M(-30,5) # Start path at point (-30, 5)
39
+
p.l(60,30) # Draw line to (60, 30)
40
+
p.h(-70) # Draw horizontal line to x=-70
41
+
p.Z() # Draw line to start
44
+
d.append(draw.ArcLine(60,-20,20,60,270,
45
+
stroke='red', stroke_width=5, fill='red', fill_opacity=0.2))
46
+
d.append(draw.Arc(60,-20,20,60,270,cw=False,
47
+
stroke='green', stroke_width=3, fill='none'))
48
+
d.append(draw.Arc(60,-20,20,270,60,cw=True,
49
+
stroke='blue', stroke_width=1, fill='black', fill_opacity=0.3))
51
+
d.setPixelScale(2) # Set number of pixels per geometry unit
52
+
#d.setRenderSize(400,200) # Alternative to setPixelScale
53
+
d.saveSvg('example.svg')
54
+
d.savePng('example.png')
56
+
# Display in iPython notebook
57
+
d.rasterize() # Display as PNG
60
+
.. image:: https://github.com/cduck/drawSvg/raw/master/example1.png
61
+
:alt: Example output image
68
+
d = draw.Drawing(1.5, 0.8, origin='center')
70
+
d.draw(draw.Rectangle(-0.75,-0.5,1.5,1, fill='#ddd'))
73
+
gradient = draw.RadialGradient(0,-0.35,0.7*10)
74
+
gradient.addStop(0.5/0.7/10, 'green', 1)
75
+
gradient.addStop(1/10, 'red', 0)
77
+
# Draw a shape to fill with the gradient
78
+
p = draw.Path(fill=gradient, stroke='black', stroke_width=0.002)
79
+
p.arc(0,-0.35,0.7,30,120)
80
+
p.arc(0,-0.35,0.5,120,30,cw=True, includeL=True)
84
+
# Draw another shape to fill with the same gradient
85
+
p = draw.Path(fill=gradient, stroke='red', stroke_width=0.002)
86
+
p.arc(0,-0.35,0.75,130,160)
87
+
p.arc(0,-0.35,0,160,130,cw=True, includeL=True)
92
+
gradient2 = draw.LinearGradient(0.1,-0.35,0.1+0.6,-0.35+0.2)
93
+
gradient2.addStop(0, 'green', 1)
94
+
gradient2.addStop(1, 'red', 0)
95
+
d.append(draw.Rectangle(0.1,-0.35,0.6,0.2,
96
+
stroke='black', stroke_width=0.002,
100
+
d.setRenderSize(w=600)
103
+
.. image:: https://github.com/cduck/drawSvg/raw/master/example2.png
104
+
:alt: Example output image
106
+
Duplicate geometry and clip paths
107
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111
+
d = draw.Drawing(1.4, 1.4, origin='center')
114
+
clip = draw.ClipPath()
115
+
clip.append(draw.Rectangle(-.25,.25-1,1,1))
117
+
# Draw a cropped circle
118
+
c = draw.Circle(0,0,0.5, stroke_width='0.01', stroke='black',
119
+
fill_opacity=0.3, clip_path=clip,
123
+
# Make a transparent copy, cropped again
124
+
g = draw.Group(opacity=0.5, clip_path=clip)
125
+
g.append(draw.Use('circle', 0.25,0.1))
129
+
d.setRenderSize(400)
132
+
.. image:: https://github.com/cduck/drawSvg/raw/master/example3.png
133
+
:alt: Example output image
135
+
Implementing other SVG tags
136
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
140
+
# Subclass DrawingBasicElement if it cannot have child nodes
141
+
# Subclass DrawingParentElement otherwise
142
+
# Subclass DrawingDef if it must go between <def></def> tags in an SVG
143
+
class Hyperlink(draw.DrawingParentElement):
145
+
def __init__(self, href, target=None, **kwargs):
146
+
# Other init logic...
147
+
# Keyword arguments to super().__init__() correspond to SVG node
148
+
# arguments: stroke_width=5 -> stroke-width="5"
149
+
super().__init__(href=href, target=target, **kwargs)
151
+
d = draw.Drawing(1, 1.2, origin='center')
154
+
hlink = Hyperlink('https://www.python.org', target='_blank',
155
+
transform='skewY(-30)')
156
+
# Add child elements
157
+
hlink.append(draw.Circle(0,0,0.5, fill='green'))
158
+
hlink.append(draw.Text('Hyperlink',0.2, 0,0, center=0.6, fill='white'))
162
+
d.setRenderSize(200)
165
+
.. image:: https://github.com/cduck/drawSvg/raw/master/example4.png
166
+
:alt: Example output image