drawSvg 1.2.2__tar.gz → 2.4.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- drawsvg-2.4.2/.gitignore +6 -0
- drawsvg-2.4.2/FUNDING.yml +3 -0
- drawsvg-2.4.2/LICENSE.txt +8 -0
- drawsvg-2.4.2/PKG-INFO +543 -0
- drawsvg-2.4.2/README.md +504 -0
- drawsvg-2.4.2/docs/img/01_circ.svg +9 -0
- drawsvg-2.4.2/docs/img/01_ellip.svg +9 -0
- drawsvg-2.4.2/docs/img/01_line.svg +7 -0
- drawsvg-2.4.2/docs/img/01_multilines.svg +7 -0
- drawsvg-2.4.2/docs/img/01_multilines2.svg +7 -0
- drawsvg-2.4.2/docs/img/01_polygon.svg +7 -0
- drawsvg-2.4.2/docs/img/01_rect.svg +10 -0
- drawsvg-2.4.2/docs/img/01_rectround.svg +11 -0
- drawsvg-2.4.2/docs/img/02_dash.svg +9 -0
- drawsvg-2.4.2/docs/img/02_foso.svg +16 -0
- drawsvg-2.4.2/docs/img/02_fsc.svg +11 -0
- drawsvg-2.4.2/docs/img/02_join.svg +12 -0
- drawsvg-2.4.2/docs/img/02_linecap.svg +11 -0
- drawsvg-2.4.2/docs/img/02_mlimit.svg +12 -0
- drawsvg-2.4.2/docs/img/02_strokewdth.svg +26 -0
- drawsvg-2.4.2/docs/img/03_pA.svg +10 -0
- drawsvg-2.4.2/docs/img/03_pC.svg +48 -0
- drawsvg-2.4.2/docs/img/03_pHV.svg +8 -0
- drawsvg-2.4.2/docs/img/03_pL.svg +11 -0
- drawsvg-2.4.2/docs/img/03_pQ.svg +21 -0
- drawsvg-2.4.2/docs/img/03_pS.svg +16 -0
- drawsvg-2.4.2/docs/img/03_pT.svg +8 -0
- drawsvg-2.4.2/docs/img/03_pZ.svg +8 -0
- drawsvg-2.4.2/docs/img/04_align.svg +16 -0
- drawsvg-2.4.2/docs/img/04_fill.svg +13 -0
- drawsvg-2.4.2/docs/img/04_fonts1.png +0 -0
- drawsvg-2.4.2/docs/img/04_fonts2.png +0 -0
- drawsvg-2.4.2/docs/img/04_len.svg +14 -0
- drawsvg-2.4.2/docs/img/04_multiline_text.svg +8 -0
- drawsvg-2.4.2/docs/img/04_mutiline_text.svg +8 -0
- drawsvg-2.4.2/docs/img/04_path.svg +31 -0
- drawsvg-2.4.2/docs/img/04_rot.svg +8 -0
- drawsvg-2.4.2/docs/img/04_rot2.svg +7 -0
- drawsvg-2.4.2/docs/img/04_tspan.svg +7 -0
- drawsvg-2.4.2/docs/img/04_tspan2.svg +7 -0
- drawsvg-2.4.2/docs/img/04_tspan3.svg +7 -0
- drawsvg-2.4.2/docs/img/04_weight.svg +16 -0
- drawsvg-2.4.2/docs/img/05_clip.svg +12 -0
- drawsvg-2.4.2/docs/img/05_clip2.svg +12 -0
- drawsvg-2.4.2/docs/img/05_clip3.svg +29 -0
- drawsvg-2.4.2/docs/img/05_lingrad.svg +11 -0
- drawsvg-2.4.2/docs/img/05_mask.svg +15 -0
- drawsvg-2.4.2/docs/img/05_mask2.svg +38 -0
- drawsvg-2.4.2/docs/img/05_mask3.svg +34 -0
- drawsvg-2.4.2/docs/img/05_radgrad.svg +11 -0
- drawsvg-2.4.2/docs/img/06_group.svg +24 -0
- drawsvg-2.4.2/docs/img/06_imag.svg +7 -0
- drawsvg-2.4.2/docs/img/06_use.svg +14 -0
- drawsvg-2.4.2/docs/img/07_cart1.svg +9 -0
- drawsvg-2.4.2/docs/img/07_cart2.svg +11 -0
- drawsvg-2.4.2/docs/img/07_cart3.svg +9 -0
- drawsvg-2.4.2/docs/img/07_rota.svg +10 -0
- drawsvg-2.4.2/docs/img/07_rota2.svg +17 -0
- drawsvg-2.4.2/docs/img/07_scalcent.svg +12 -0
- drawsvg-2.4.2/docs/img/07_scale.svg +9 -0
- drawsvg-2.4.2/docs/img/07_scale2.svg +9 -0
- drawsvg-2.4.2/docs/img/07_skew.svg +23 -0
- drawsvg-2.4.2/docs/img/07_trans.svg +8 -0
- drawsvg-2.4.2/docs/img/example1.png +0 -0
- drawsvg-2.4.2/docs/index.md +1307 -0
- drawsvg-2.4.2/drawsvg/__init__.py +99 -0
- {drawSvg-1.2.2/drawSvg → drawsvg-2.4.2/drawsvg}/color.py +49 -43
- drawsvg-2.4.2/drawsvg/defs.py +116 -0
- drawsvg-2.4.2/drawsvg/drawing.py +445 -0
- drawsvg-2.4.2/drawsvg/elements.py +623 -0
- drawsvg-2.4.2/drawsvg/font_embed.py +48 -0
- drawSvg-1.2.2/drawSvg/animation.py → drawsvg-2.4.2/drawsvg/frame_animation.py +43 -17
- drawsvg-2.4.2/drawsvg/jupyter.py +41 -0
- drawsvg-2.4.2/drawsvg/native_animation/__init__.py +8 -0
- drawsvg-2.4.2/drawsvg/native_animation/playback_control_js.py +120 -0
- drawsvg-2.4.2/drawsvg/native_animation/playback_control_ui.py +87 -0
- drawsvg-2.4.2/drawsvg/native_animation/synced_animation.py +352 -0
- drawsvg-2.4.2/drawsvg/raster.py +94 -0
- drawsvg-2.4.2/drawsvg/types.py +379 -0
- drawsvg-2.4.2/drawsvg/url_encode.py +41 -0
- drawsvg-2.4.2/drawsvg/video.py +219 -0
- drawsvg-2.4.2/drawsvg/widgets/__init__.py +2 -0
- drawsvg-2.4.2/drawsvg/widgets/async_animation.py +154 -0
- drawsvg-2.4.2/drawsvg/widgets/drawing_javascript.py +155 -0
- {drawSvg-1.2.2/drawSvg → drawsvg-2.4.2/drawsvg}/widgets/drawing_widget.py +62 -13
- drawsvg-2.4.2/drawsvg.egg-info/PKG-INFO +543 -0
- drawsvg-2.4.2/drawsvg.egg-info/SOURCES.txt +114 -0
- drawsvg-2.4.2/drawsvg.egg-info/requires.txt +17 -0
- drawsvg-2.4.2/drawsvg.egg-info/top_level.txt +1 -0
- drawsvg-2.4.2/examples/animated-fix-github.svg +37 -0
- drawsvg-2.4.2/examples/animated.svg +18 -0
- drawsvg-2.4.2/examples/example1.png +0 -0
- drawsvg-2.4.2/examples/example1.svg +33 -0
- drawsvg-2.4.2/examples/example2.png +0 -0
- drawsvg-2.4.2/examples/example2.svg +22 -0
- drawsvg-2.4.2/examples/example3.png +0 -0
- drawsvg-2.4.2/examples/example3.svg +13 -0
- drawsvg-2.4.2/examples/example4.png +0 -0
- drawsvg-2.4.2/examples/example4.svg +13 -0
- drawsvg-2.4.2/examples/example5.gif +0 -0
- drawsvg-2.4.2/examples/example6.gif +0 -0
- drawsvg-2.4.2/examples/example6.png +0 -0
- drawsvg-2.4.2/examples/example7.gif +0 -0
- drawsvg-2.4.2/examples/example8.png +0 -0
- drawsvg-2.4.2/examples/example8.svg +15 -0
- drawsvg-2.4.2/examples/font.svg +15 -0
- drawsvg-2.4.2/examples/logo.html +210 -0
- drawsvg-2.4.2/examples/logo.ipynb +362 -0
- drawsvg-2.4.2/examples/logo.svg +204 -0
- drawsvg-2.4.2/examples/orbit-spritesheet.png +0 -0
- drawsvg-2.4.2/examples/playback-controls.html +169 -0
- drawsvg-2.4.2/examples/playback-controls.svg +163 -0
- drawsvg-2.4.2/mkdocs.yml +4 -0
- {drawSvg-1.2.2 → drawsvg-2.4.2}/setup.py +25 -9
- drawSvg-1.2.2/PKG-INFO +0 -259
- drawSvg-1.2.2/README.md +0 -243
- drawSvg-1.2.2/drawSvg/__init__.py +0 -72
- drawSvg-1.2.2/drawSvg/defs.py +0 -67
- drawSvg-1.2.2/drawSvg/drawing.py +0 -146
- drawSvg-1.2.2/drawSvg/elements.py +0 -355
- drawSvg-1.2.2/drawSvg/missing.py +0 -7
- drawSvg-1.2.2/drawSvg/raster.py +0 -43
- drawSvg-1.2.2/drawSvg/video.py +0 -54
- drawSvg-1.2.2/drawSvg/widgets/__init__.py +0 -1
- drawSvg-1.2.2/drawSvg/widgets/drawing_javascript.py +0 -91
- drawSvg-1.2.2/drawSvg.egg-info/PKG-INFO +0 -259
- drawSvg-1.2.2/drawSvg.egg-info/PKG-INFO_conflict-20190205-160748 +0 -183
- drawSvg-1.2.2/drawSvg.egg-info/SOURCES.txt +0 -20
- drawSvg-1.2.2/drawSvg.egg-info/requires.txt +0 -3
- drawSvg-1.2.2/drawSvg.egg-info/top_level.txt +0 -1
- {drawSvg-1.2.2/drawSvg.egg-info → drawsvg-2.4.2/drawsvg.egg-info}/dependency_links.txt +0 -0
- {drawSvg-1.2.2 → drawsvg-2.4.2}/setup.cfg +0 -0
drawsvg-2.4.2/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2023 Casey Duckering
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
drawsvg-2.4.2/PKG-INFO
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: drawsvg
|
|
3
|
+
Version: 2.4.2
|
|
4
|
+
Summary: A Python 3 library for programmatically generating SVG (vector) images and animations. Drawsvg can also render to PNG, MP4, and display your drawings in Jupyter notebook and Jupyter lab.
|
|
5
|
+
Home-page: https://github.com/cduck/drawsvg
|
|
6
|
+
Download-URL: https://github.com/cduck/drawsvg/archive/2.4.2.tar.gz
|
|
7
|
+
Author: Casey Duckering
|
|
8
|
+
Keywords: SVG,draw,graphics,iPython,Jupyter,widget,animation
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Framework :: IPython
|
|
12
|
+
Classifier: Framework :: Jupyter
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE.txt
|
|
15
|
+
Provides-Extra: raster
|
|
16
|
+
Requires-Dist: cairoSVG~=2.3; extra == "raster"
|
|
17
|
+
Requires-Dist: numpy<3,>=1.16; extra == "raster"
|
|
18
|
+
Requires-Dist: imageio~=2.5; extra == "raster"
|
|
19
|
+
Requires-Dist: imageio_ffmpeg~=0.4; extra == "raster"
|
|
20
|
+
Provides-Extra: color
|
|
21
|
+
Requires-Dist: pwkit~=1.0; extra == "color"
|
|
22
|
+
Requires-Dist: numpy<3,>=1.16; extra == "color"
|
|
23
|
+
Provides-Extra: all
|
|
24
|
+
Requires-Dist: cairoSVG~=2.3; extra == "all"
|
|
25
|
+
Requires-Dist: numpy<3,>=1.16; extra == "all"
|
|
26
|
+
Requires-Dist: imageio~=2.5; extra == "all"
|
|
27
|
+
Requires-Dist: imageio_ffmpeg~=0.4; extra == "all"
|
|
28
|
+
Requires-Dist: pwkit~=1.0; extra == "all"
|
|
29
|
+
Dynamic: author
|
|
30
|
+
Dynamic: classifier
|
|
31
|
+
Dynamic: description
|
|
32
|
+
Dynamic: description-content-type
|
|
33
|
+
Dynamic: download-url
|
|
34
|
+
Dynamic: home-page
|
|
35
|
+
Dynamic: keywords
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
Dynamic: provides-extra
|
|
38
|
+
Dynamic: summary
|
|
39
|
+
|
|
40
|
+
[](https://github.com/cduck/drawSvg/blob/master/examples/logo.svg)
|
|
41
|
+
|
|
42
|
+
A Python 3 library for programmatically generating SVG images and animations that can render and display your drawings in a Jupyter notebook or Jupyter lab.
|
|
43
|
+
|
|
44
|
+
Most common SVG tags are supported and others can easily be added by writing a small subclass of `DrawableBasicElement` or `DrawableParentElement`. [Nearly all SVG attributes](https://developer.mozilla.org/en-US/docs/Web/SVG) are supported via keyword args (e.g. Python keyword argument `fill_opacity=0.5` becomes SVG attribute `fill-opacity="0.5"`).
|
|
45
|
+
|
|
46
|
+
An interactive [Jupyter notebook](https://jupyter.org) widget, `drawsvg.widgets.DrawingWidget`, [is included](#interactive-widget) that can update drawings based on mouse events. The widget does not yet work in Jupyter lab.
|
|
47
|
+
|
|
48
|
+
[SVG quick reference docs](https://cduck.github.io/drawsvg/)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# Install
|
|
52
|
+
|
|
53
|
+
Drawsvg is available on PyPI:
|
|
54
|
+
```bash
|
|
55
|
+
$ python3 -m pip install "drawsvg~=2.0"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
To enable raster image support (PNG, MP4, and GIF), follow the [full-feature install instructions](#full-feature-install).
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
## Upgrading from version 1.x
|
|
62
|
+
|
|
63
|
+
Major breaking changes:
|
|
64
|
+
|
|
65
|
+
- camelCase method and argument names are now snake\_case and the package name is lowercase (except for arguments that correspond to camelCase SVG attributes).
|
|
66
|
+
- The default coordinate system y-axis now matches the SVG coordinate system (y increases down the screen, x increases right)
|
|
67
|
+
- How to fix `ModuleNotFoundError: No module named 'drawSvg'` (with a capital S)? Either pip install `"drawSvg~=1.9"` or update your code for drawsvg 2.x (for example, change `drawSvg` to `drawsvg` and `d.saveSvg` to `d.save_svg`).
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# Examples
|
|
71
|
+
|
|
72
|
+
### Basic drawing elements
|
|
73
|
+
```python
|
|
74
|
+
import drawsvg as draw
|
|
75
|
+
|
|
76
|
+
d = draw.Drawing(200, 100, origin='center')
|
|
77
|
+
|
|
78
|
+
# Draw an irregular polygon
|
|
79
|
+
d.append(draw.Lines(-80, 45,
|
|
80
|
+
70, 49,
|
|
81
|
+
95, -49,
|
|
82
|
+
-90, -40,
|
|
83
|
+
close=False,
|
|
84
|
+
fill='#eeee00',
|
|
85
|
+
stroke='black'))
|
|
86
|
+
|
|
87
|
+
# Draw a rectangle
|
|
88
|
+
r = draw.Rectangle(-80, -50, 40, 50, fill='#1248ff')
|
|
89
|
+
r.append_title("Our first rectangle") # Add a tooltip
|
|
90
|
+
d.append(r)
|
|
91
|
+
|
|
92
|
+
# Draw a circle
|
|
93
|
+
d.append(draw.Circle(-40, 10, 30,
|
|
94
|
+
fill='red', stroke_width=2, stroke='black'))
|
|
95
|
+
|
|
96
|
+
# Draw an arbitrary path (a triangle in this case)
|
|
97
|
+
p = draw.Path(stroke_width=2, stroke='lime', fill='black', fill_opacity=0.2)
|
|
98
|
+
p.M(-10, -20) # Start path at point (-10, -20)
|
|
99
|
+
p.C(30, 10, 30, -50, 70, -20) # Draw a curve to (70, -20)
|
|
100
|
+
d.append(p)
|
|
101
|
+
|
|
102
|
+
# Draw text
|
|
103
|
+
d.append(draw.Text('Basic text', 8, -10, -35, fill='blue')) # 8pt text at (-10, -35)
|
|
104
|
+
d.append(draw.Text('Path text', 8, path=p, text_anchor='start', line_height=1))
|
|
105
|
+
d.append(draw.Text(['Multi-line', 'text'], 8, path=p, text_anchor='end', center=True))
|
|
106
|
+
|
|
107
|
+
# Draw multiple circular arcs
|
|
108
|
+
d.append(draw.ArcLine(60, 20, 20, 60, 270,
|
|
109
|
+
stroke='red', stroke_width=5, fill='red', fill_opacity=0.2))
|
|
110
|
+
d.append(draw.Arc(60, 20, 20, 90, -60, cw=True,
|
|
111
|
+
stroke='green', stroke_width=3, fill='none'))
|
|
112
|
+
d.append(draw.Arc(60, 20, 20, -60, 90, cw=False,
|
|
113
|
+
stroke='blue', stroke_width=1, fill='black', fill_opacity=0.3))
|
|
114
|
+
|
|
115
|
+
# Draw arrows
|
|
116
|
+
arrow = draw.Marker(-0.1, -0.51, 0.9, 0.5, scale=4, orient='auto')
|
|
117
|
+
arrow.append(draw.Lines(-0.1, 0.5, -0.1, -0.5, 0.9, 0, fill='red', close=True))
|
|
118
|
+
p = draw.Path(stroke='red', stroke_width=2, fill='none',
|
|
119
|
+
marker_end=arrow) # Add an arrow to the end of a path
|
|
120
|
+
p.M(20, 40).L(20, 27).L(0, 20) # Chain multiple path commands
|
|
121
|
+
d.append(p)
|
|
122
|
+
d.append(draw.Line(30, 20, 0, 10,
|
|
123
|
+
stroke='red', stroke_width=2, fill='none',
|
|
124
|
+
marker_end=arrow)) # Add an arrow to the end of a line
|
|
125
|
+
|
|
126
|
+
d.set_pixel_scale(2) # Set number of pixels per geometry unit
|
|
127
|
+
#d.set_render_size(400, 200) # Alternative to set_pixel_scale
|
|
128
|
+
d.save_svg('example.svg')
|
|
129
|
+
d.save_png('example.png')
|
|
130
|
+
|
|
131
|
+
# Display in Jupyter notebook
|
|
132
|
+
#d.rasterize() # Display as PNG
|
|
133
|
+
d # Display as SVG
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/example1.svg)
|
|
137
|
+
|
|
138
|
+
### SVG-native animation with playback controls
|
|
139
|
+
```python
|
|
140
|
+
import drawsvg as draw
|
|
141
|
+
|
|
142
|
+
d = draw.Drawing(400, 200, origin='center',
|
|
143
|
+
animation_config=draw.types.SyncedAnimationConfig(
|
|
144
|
+
# Animation configuration
|
|
145
|
+
duration=8, # Seconds
|
|
146
|
+
show_playback_progress=True,
|
|
147
|
+
show_playback_controls=True))
|
|
148
|
+
d.append(draw.Rectangle(-200, -100, 400, 200, fill='#eee')) # Background
|
|
149
|
+
d.append(draw.Circle(0, 0, 40, fill='green')) # Center circle
|
|
150
|
+
|
|
151
|
+
# Animation
|
|
152
|
+
circle = draw.Circle(0, 0, 0, fill='gray') # Moving circle
|
|
153
|
+
circle.add_key_frame(0, cx=-100, cy=0, r=0)
|
|
154
|
+
circle.add_key_frame(2, cx=0, cy=-100, r=40)
|
|
155
|
+
circle.add_key_frame(4, cx=100, cy=0, r=0)
|
|
156
|
+
circle.add_key_frame(6, cx=0, cy=100, r=40)
|
|
157
|
+
circle.add_key_frame(8, cx=-100, cy=0, r=0)
|
|
158
|
+
d.append(circle)
|
|
159
|
+
r = draw.Rectangle(0, 0, 0, 0, fill='silver') # Moving square
|
|
160
|
+
r.add_key_frame(0, x=-100, y=0, width=0, height=0)
|
|
161
|
+
r.add_key_frame(2, x=0-20, y=-100-20, width=40, height=40)
|
|
162
|
+
r.add_key_frame(4, x=100, y=0, width=0, height=0)
|
|
163
|
+
r.add_key_frame(6, x=0-20, y=100-20, width=40, height=40)
|
|
164
|
+
r.add_key_frame(8, x=-100, y=0, width=0, height=0)
|
|
165
|
+
d.append(r)
|
|
166
|
+
|
|
167
|
+
# Changing text
|
|
168
|
+
draw.native_animation.animate_text_sequence(
|
|
169
|
+
d,
|
|
170
|
+
[0, 2, 4, 6],
|
|
171
|
+
['0', '1', '2', '3'],
|
|
172
|
+
30, 0, 1, fill='yellow', center=True)
|
|
173
|
+
|
|
174
|
+
# Save as a standalone animated SVG or HTML
|
|
175
|
+
d.save_svg('playback-controls.svg')
|
|
176
|
+
d.save_html('playback-controls.html')
|
|
177
|
+
|
|
178
|
+
# Display in Jupyter notebook
|
|
179
|
+
#d.display_image() # Display SVG as an image (will not be interactive)
|
|
180
|
+
#d.display_iframe() # Display as interactive SVG (alternative)
|
|
181
|
+
#d.as_gif('orbit.gif', fps=10) # Render as a GIF image, optionally save to file
|
|
182
|
+
#d.as_mp4('orbig.mp4', fps=60, verbose=True) # Render as an MP4 video, optionally save to file
|
|
183
|
+
#d.as_spritesheet('orbit-spritesheet.png', row_length=10, fps=3) # Render as a spritesheet
|
|
184
|
+
d.display_inline() # Display as interactive SVG
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/playback-controls.svg)
|
|
188
|
+
|
|
189
|
+
Note: GitHub blocks the playback controls.
|
|
190
|
+
Download the above SVG and open it in a web browser to try.
|
|
191
|
+
|
|
192
|
+
https://user-images.githubusercontent.com/2476062/221400434-1529d237-e9bf-4363-a143-0ece75cd349a.mp4
|
|
193
|
+
|
|
194
|
+
### Patterns and gradients
|
|
195
|
+
```python
|
|
196
|
+
import drawsvg as draw
|
|
197
|
+
|
|
198
|
+
d = draw.Drawing(1.5, 0.8, origin='center')
|
|
199
|
+
|
|
200
|
+
# Background pattern (not supported by Cairo, d.rasterize() will not show it)
|
|
201
|
+
pattern = draw.Pattern(width=0.13, height=0.23)
|
|
202
|
+
pattern.append(draw.Rectangle(0, 0, .1, .1, fill='yellow'))
|
|
203
|
+
pattern.append(draw.Rectangle(0, .1, .1, .1, fill='orange'))
|
|
204
|
+
d.draw(draw.Rectangle(-0.75, -0.5, 1.5, 1, fill=pattern, fill_opacity=0.4))
|
|
205
|
+
|
|
206
|
+
# Create gradient
|
|
207
|
+
gradient = draw.RadialGradient(0, 0.35, 0.7*10)
|
|
208
|
+
gradient.add_stop(0.5/0.7/10, 'green', 1)
|
|
209
|
+
gradient.add_stop(1/10, 'red', 0)
|
|
210
|
+
|
|
211
|
+
# Draw a shape to fill with the gradient
|
|
212
|
+
p = draw.Path(fill=gradient, stroke='black', stroke_width=0.002)
|
|
213
|
+
p.arc(0, 0.35, 0.7, -30, -120, cw=False)
|
|
214
|
+
p.arc(0, 0.35, 0.5, -120, -30, cw=True, include_l=True)
|
|
215
|
+
p.Z()
|
|
216
|
+
d.append(p)
|
|
217
|
+
|
|
218
|
+
# Draw another shape to fill with the same gradient
|
|
219
|
+
p = draw.Path(fill=gradient, stroke='red', stroke_width=0.002)
|
|
220
|
+
p.arc(0, 0.35, 0.75, -130, -160, cw=False)
|
|
221
|
+
p.arc(0, 0.35, 0, -160, -130, cw=True, include_l=True)
|
|
222
|
+
p.Z()
|
|
223
|
+
d.append(p)
|
|
224
|
+
|
|
225
|
+
# Another gradient
|
|
226
|
+
gradient2 = draw.LinearGradient(0.1, 0.35, 0.1+0.6, 0.35+0.2)
|
|
227
|
+
gradient2.add_stop(0, 'green', 1)
|
|
228
|
+
gradient2.add_stop(1, 'red', 0)
|
|
229
|
+
d.append(draw.Rectangle(0.1, 0.15, 0.6, 0.2,
|
|
230
|
+
stroke='black', stroke_width=0.002,
|
|
231
|
+
fill=gradient2))
|
|
232
|
+
|
|
233
|
+
# Display
|
|
234
|
+
d.set_render_size(w=600)
|
|
235
|
+
d
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/example2.svg)
|
|
239
|
+
|
|
240
|
+
### Duplicate geometry and clip paths
|
|
241
|
+
```python
|
|
242
|
+
import drawsvg as draw
|
|
243
|
+
|
|
244
|
+
d = draw.Drawing(1.4, 1.4, origin='center')
|
|
245
|
+
|
|
246
|
+
# Define clip path
|
|
247
|
+
clip = draw.ClipPath()
|
|
248
|
+
clip.append(draw.Rectangle(-.25, -.25, 1, 1))
|
|
249
|
+
|
|
250
|
+
# Draw a cropped circle
|
|
251
|
+
circle = draw.Circle(0, 0, 0.5,
|
|
252
|
+
stroke_width='0.01', stroke='black',
|
|
253
|
+
fill_opacity=0.3, clip_path=clip)
|
|
254
|
+
d.append(circle)
|
|
255
|
+
|
|
256
|
+
# Make a transparent copy, cropped again
|
|
257
|
+
g = draw.Group(opacity=0.5, clip_path=clip)
|
|
258
|
+
# Here, circle is not directly appended to the drawing.
|
|
259
|
+
# drawsvg recognizes that `Use` references `circle` and automatically adds
|
|
260
|
+
# `circle` to the <defs></defs> section of the SVG.
|
|
261
|
+
g.append(draw.Use(circle, 0.25, -0.1))
|
|
262
|
+
d.append(g)
|
|
263
|
+
|
|
264
|
+
# Display
|
|
265
|
+
d.set_render_size(400)
|
|
266
|
+
#d.rasterize() # Display as PNG
|
|
267
|
+
d # Display as SVG
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/example3.svg)
|
|
271
|
+
|
|
272
|
+
### Organizing and duplicating drawing elements
|
|
273
|
+
```python
|
|
274
|
+
import drawsvg as draw
|
|
275
|
+
|
|
276
|
+
d = draw.Drawing(300, 100)
|
|
277
|
+
d.set_pixel_scale(2)
|
|
278
|
+
|
|
279
|
+
# Use groups to contain other elements
|
|
280
|
+
# Children elements of groups inherit the coordinate system (transform)
|
|
281
|
+
# and attribute values
|
|
282
|
+
group = draw.Group(fill='orange', transform='rotate(-20)')
|
|
283
|
+
group.append(draw.Rectangle(0, 10, 20, 40)) # This rectangle will be orange
|
|
284
|
+
group.append(draw.Circle(30, 40, 10)) # This circle will also be orange
|
|
285
|
+
group.append(draw.Circle(50, 40, 10, fill='green')) # This circle will not
|
|
286
|
+
d.append(group)
|
|
287
|
+
|
|
288
|
+
# Use the Use element to make duplicates of elements
|
|
289
|
+
# Each duplicate can be placed at an offset (x, y) location and any additional
|
|
290
|
+
# attributes (like fill color) are inherited if the element didn't specify them.
|
|
291
|
+
d.append(draw.Use(group, 80, 0, stroke='black', stroke_width=1))
|
|
292
|
+
d.append(draw.Use(group, 80, 20, stroke='blue', stroke_width=2))
|
|
293
|
+
d.append(draw.Use(group, 80, 40, stroke='red', stroke_width=3))
|
|
294
|
+
|
|
295
|
+
d.display_inline()
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/example8.svg)
|
|
299
|
+
|
|
300
|
+
### Implementing other SVG tags
|
|
301
|
+
```python
|
|
302
|
+
import drawsvg as draw
|
|
303
|
+
|
|
304
|
+
# Subclass DrawingBasicElement if it cannot have child nodes
|
|
305
|
+
# Subclass DrawingParentElement otherwise
|
|
306
|
+
# Subclass DrawingDef if it must go between <def></def> tags in an SVG
|
|
307
|
+
class Hyperlink(draw.DrawingParentElement):
|
|
308
|
+
TAG_NAME = 'a'
|
|
309
|
+
def __init__(self, href, target=None, **kwargs):
|
|
310
|
+
# Other init logic...
|
|
311
|
+
# Keyword arguments to super().__init__() correspond to SVG node
|
|
312
|
+
# arguments: stroke_width=5 -> <a stroke-width="5" ...>...</a>
|
|
313
|
+
super().__init__(href=href, target=target, **kwargs)
|
|
314
|
+
|
|
315
|
+
d = draw.Drawing(1, 1.2, origin='center')
|
|
316
|
+
|
|
317
|
+
# Create hyperlink
|
|
318
|
+
hlink = Hyperlink('https://www.python.org', target='_blank',
|
|
319
|
+
transform='skewY(-30)')
|
|
320
|
+
# Add child elements
|
|
321
|
+
hlink.append(draw.Circle(0, 0, 0.5, fill='green'))
|
|
322
|
+
hlink.append(draw.Text('Hyperlink', 0.2, 0, 0, center=0.6, fill='white'))
|
|
323
|
+
|
|
324
|
+
# Draw and display
|
|
325
|
+
d.append(hlink)
|
|
326
|
+
d.set_render_size(200)
|
|
327
|
+
d
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/example4.svg)
|
|
331
|
+
|
|
332
|
+
### Animation with the SVG Animate Tag
|
|
333
|
+
```python
|
|
334
|
+
import drawsvg as draw
|
|
335
|
+
|
|
336
|
+
d = draw.Drawing(200, 200, origin='center')
|
|
337
|
+
|
|
338
|
+
# Animate the position and color of circle
|
|
339
|
+
c = draw.Circle(0, 0, 20, fill='red')
|
|
340
|
+
# See for supported attributes:
|
|
341
|
+
# https://developer.mozilla.org/en-US/docs/Web/SVG/Element/animate
|
|
342
|
+
c.append_anim(draw.Animate('cy', '6s', '-80;80;-80',
|
|
343
|
+
repeatCount='indefinite'))
|
|
344
|
+
c.append_anim(draw.Animate('cx', '6s', '0;80;0;-80;0',
|
|
345
|
+
repeatCount='indefinite'))
|
|
346
|
+
c.append_anim(draw.Animate('fill', '6s', 'red;green;blue;yellow',
|
|
347
|
+
calc_mode='discrete',
|
|
348
|
+
repeatCount='indefinite'))
|
|
349
|
+
d.append(c)
|
|
350
|
+
|
|
351
|
+
# Animate a black circle around an ellipse
|
|
352
|
+
ellipse = draw.Path()
|
|
353
|
+
ellipse.M(-90, 0)
|
|
354
|
+
ellipse.A(90, 40, 360, True, True, 90, 0) # Ellipse path
|
|
355
|
+
ellipse.A(90, 40, 360, True, True, -90, 0)
|
|
356
|
+
ellipse.Z()
|
|
357
|
+
c2 = draw.Circle(0, 0, 10)
|
|
358
|
+
# See for supported attributes:
|
|
359
|
+
# https://developer.mozilla.org/en-US/docs/Web/SVG/Element/animate_motion
|
|
360
|
+
c2.append_anim(draw.AnimateMotion(ellipse, '3s',
|
|
361
|
+
repeatCount='indefinite'))
|
|
362
|
+
# See for supported attributes:
|
|
363
|
+
# https://developer.mozilla.org/en-US/docs/Web/SVG/Element/animate_transform
|
|
364
|
+
c2.append_anim(draw.AnimateTransform('scale', '3s', '1,2;2,1;1,2;2,1;1,2',
|
|
365
|
+
repeatCount='indefinite'))
|
|
366
|
+
d.append(c2)
|
|
367
|
+
|
|
368
|
+
d.save_svg('animated.svg') # Save to file
|
|
369
|
+
d # Display in Jupyter notebook
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/animated.svg)
|
|
373
|
+
|
|
374
|
+
### Interactive Widget
|
|
375
|
+
```python
|
|
376
|
+
import drawsvg as draw
|
|
377
|
+
from drawsvg.widgets import DrawingWidget
|
|
378
|
+
import hyperbolic.poincare as hyper # python3 -m pip install hyperbolic
|
|
379
|
+
from hyperbolic import euclid
|
|
380
|
+
|
|
381
|
+
# Create drawing
|
|
382
|
+
d = draw.Drawing(2, 2, origin='center', context=draw.Context(invert_y=True))
|
|
383
|
+
d.set_render_size(500)
|
|
384
|
+
d.append(draw.Circle(0, 0, 1, fill='orange'))
|
|
385
|
+
group = draw.Group()
|
|
386
|
+
d.append(group)
|
|
387
|
+
|
|
388
|
+
# Update the drawing based on user input
|
|
389
|
+
click_list = []
|
|
390
|
+
def redraw(points):
|
|
391
|
+
group.children.clear()
|
|
392
|
+
for x1, y1 in points:
|
|
393
|
+
for x2, y2 in points:
|
|
394
|
+
if (x1, y1) == (x2, y2): continue
|
|
395
|
+
p1 = hyper.Point.from_euclid(x1, y1)
|
|
396
|
+
p2 = hyper.Point.from_euclid(x2, y2)
|
|
397
|
+
if p1.distance_to(p2) <= 2:
|
|
398
|
+
line = hyper.Line.from_points(*p1, *p2, segment=True)
|
|
399
|
+
group.draw(line, hwidth=0.2, fill='white')
|
|
400
|
+
for x, y in points:
|
|
401
|
+
p = hyper.Point.from_euclid(x, y)
|
|
402
|
+
group.draw(hyper.Circle.from_center_radius(p, 0.1),
|
|
403
|
+
fill='green')
|
|
404
|
+
redraw(click_list)
|
|
405
|
+
|
|
406
|
+
# Create interactive widget and register mouse events
|
|
407
|
+
widget = DrawingWidget(d)
|
|
408
|
+
@widget.mousedown
|
|
409
|
+
def mousedown(widget, x, y, info):
|
|
410
|
+
if (x**2 + y**2) ** 0.5 + 1e-5 < 1:
|
|
411
|
+
click_list.append((x, y))
|
|
412
|
+
redraw(click_list)
|
|
413
|
+
widget.refresh()
|
|
414
|
+
@widget.mousemove
|
|
415
|
+
def mousemove(widget, x, y, info):
|
|
416
|
+
if (x**2 + y**2) ** 0.5 + 1e-5 < 1:
|
|
417
|
+
redraw(click_list + [(x, y)])
|
|
418
|
+
widget.refresh()
|
|
419
|
+
widget
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+

|
|
423
|
+
|
|
424
|
+
Note: The above example currently only works in `jupyter notebook`, not `jupyter lab`.
|
|
425
|
+
|
|
426
|
+
### Frame-by-Frame Animation
|
|
427
|
+
```python
|
|
428
|
+
import drawsvg as draw
|
|
429
|
+
|
|
430
|
+
# Draw a frame of the animation
|
|
431
|
+
def draw_frame(t):
|
|
432
|
+
d = draw.Drawing(2, 6.05, origin=(-1, -5))
|
|
433
|
+
d.set_render_size(h=300)
|
|
434
|
+
d.append(draw.Rectangle(-2, -6, 4, 8, fill='white'))
|
|
435
|
+
d.append(draw.Rectangle(-1, 1, 2, 0.05, fill='brown'))
|
|
436
|
+
t = (t + 1) % 2 - 1
|
|
437
|
+
y = t**2 * 4 - 4
|
|
438
|
+
d.append(draw.Circle(0, y, 1, fill='lime'))
|
|
439
|
+
return d
|
|
440
|
+
|
|
441
|
+
with draw.frame_animate_jupyter(draw_frame, delay=0.05) as anim:
|
|
442
|
+
# Or:
|
|
443
|
+
#with draw.frame_animate_video('example6.gif', draw_frame, duration=0.05) as anim:
|
|
444
|
+
# Or:
|
|
445
|
+
#with draw.frame_animate_spritesheet('example6.png', draw_frame, row_length=10) as anim:
|
|
446
|
+
# Add each frame to the animation
|
|
447
|
+
for i in range(20):
|
|
448
|
+
anim.draw_frame(i/10)
|
|
449
|
+
for i in range(20):
|
|
450
|
+
anim.draw_frame(i/10)
|
|
451
|
+
for i in range(20):
|
|
452
|
+
anim.draw_frame(i/10)
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
GIF:
|
|
456
|
+
|
|
457
|
+

|
|
458
|
+
|
|
459
|
+
Spritesheet (usable in most 2D game engines):
|
|
460
|
+
|
|
461
|
+

|
|
462
|
+
|
|
463
|
+
### Asynchronous Frame-based Animation in Jupyter
|
|
464
|
+
```python
|
|
465
|
+
# Jupyter cell 1:
|
|
466
|
+
import drawsvg as draw
|
|
467
|
+
from drawsvg.widgets import AsyncAnimation
|
|
468
|
+
widget = AsyncAnimation(fps=10)
|
|
469
|
+
widget
|
|
470
|
+
# [Animation is displayed here (click to pause)]
|
|
471
|
+
|
|
472
|
+
# Jupyter cell 2:
|
|
473
|
+
global_variable = 'a'
|
|
474
|
+
@widget.set_draw_frame # Animation above is automatically updated
|
|
475
|
+
def draw_frame(secs=0):
|
|
476
|
+
# Draw something...
|
|
477
|
+
d = draw.Drawing(100, 40)
|
|
478
|
+
d.append(draw.Text(global_variable, 20, 0, 30))
|
|
479
|
+
d.append(draw.Text('{:0.1f}'.format(secs), 20, 30, 30))
|
|
480
|
+
return d
|
|
481
|
+
|
|
482
|
+
# Jupyter cell 3:
|
|
483
|
+
global_variable = 'b' # Animation above now displays 'b'
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+

|
|
487
|
+
|
|
488
|
+
Note: The above example currently only works in `jupyter notebook`, not `jupyter lab`.
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
### Embed custom fonts
|
|
492
|
+
```python
|
|
493
|
+
import drawsvg as draw
|
|
494
|
+
|
|
495
|
+
d = draw.Drawing(400, 100, origin='center')
|
|
496
|
+
d.embed_google_font('Permanent Marker', text=set('Text with custom font'))
|
|
497
|
+
|
|
498
|
+
d.append(draw.Text('Text with custom font', 35, 0, 0, center=True,
|
|
499
|
+
font_family='Permanent Marker', font_style='italic'))
|
|
500
|
+
|
|
501
|
+
d.save_svg('font.svg')
|
|
502
|
+
d # Custom fonts work in most browsers but not in rasterize(), save_png(), or save_video()
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
[](https://github.com/cduck/drawsvg/blob/master/examples/font.svg)
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
---
|
|
509
|
+
|
|
510
|
+
# Full-feature install
|
|
511
|
+
Drawsvg may be either be installed with no dependencies (only SVG and SVG-native animation will work):
|
|
512
|
+
```bash
|
|
513
|
+
$ python3 -m pip install "drawsvg~=2.0"
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
Or drawsvg may be installed with extra dependencies to support PNG, MP4, and GIF output:
|
|
517
|
+
```bash
|
|
518
|
+
$ python3 -m pip install "drawsvg[all]~=2.0"
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
An additional required package, [Cairo](https://www.cairographics.org/download/), cannot be installed with pip and must be installed separately. When Cairo is installed, drawsvg can output PNG and other image formats in addition to SVG. Install it with your preferred package manager. Examples:
|
|
522
|
+
|
|
523
|
+
**Ubuntu**
|
|
524
|
+
|
|
525
|
+
```bash
|
|
526
|
+
$ sudo apt install libcairo2
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
**macOS**
|
|
530
|
+
|
|
531
|
+
Using [homebrew](https://brew.sh/) (may require a Python version installed with `brew install python`):
|
|
532
|
+
|
|
533
|
+
```bash
|
|
534
|
+
$ brew install cairo
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
**Any platform**
|
|
538
|
+
|
|
539
|
+
Using [Anaconda](https://docs.conda.io/en/latest/miniconda.html) (may require Python and cairo installed in the same conda environment):
|
|
540
|
+
|
|
541
|
+
```bash
|
|
542
|
+
$ conda install -c anaconda cairo
|
|
543
|
+
```
|