Programmatically generate SVG (vector) images, animations, and interactive Jupyter widgets

Fix #31, add example, update readme

Changed files
+36 -14
drawsvg
examples
+10 -6
README.md
···
-
[![drawsvg logo](https://raw.githubusercontent.com/cduck/drawsvg/v2/examples/logo.svg)](https://github.com/cduck/drawSvg/blob/v2/examples/logo.ipynb)
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.
···
d.display_inline() # Display as interactive SVG
```
-
[![Example animated image](https://raw.githubusercontent.com/cduck/drawsvg/master/examples/playback-controls.svg)](https://github.com/cduck/drawsvg/blob/master/examples/playback-controls.svg)
Note: GitHub blocks the playback controls.
Download the above SVG and open it in a web browser to try.
-
### Gradients
```python
import drawsvg as draw
d = draw.Drawing(1.5, 0.8, origin='center')
-
d.draw(draw.Rectangle(-0.75, -0.5, 1.5, 1, fill='#ddd'))
# Create gradient
gradient = draw.RadialGradient(0, 0.35, 0.7*10)
···
d
```
-
[![Example output image](https://raw.githubusercontent.com/cduck/drawsvg/master/examples/example2.png)](https://github.com/cduck/drawsvg/blob/master/examples/example2.svg)
-
### Duplicate geometry, clip paths
```python
import drawsvg as draw
···
+
[![drawsvg logo](https://raw.githubusercontent.com/cduck/drawsvg/v2/examples/logo.svg?sanitize=true)](https://github.com/cduck/drawSvg/blob/v2/examples/logo.ipynb)
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.
···
d.display_inline() # Display as interactive SVG
```
+
[![Example animated image](https://raw.githubusercontent.com/cduck/drawsvg/master/examples/playback-controls.svg?sanitize=true)](https://github.com/cduck/drawsvg/blob/master/examples/playback-controls.svg)
Note: GitHub blocks the playback controls.
Download the above SVG and open it in a web browser to try.
+
### Patterns and gradients
```python
import drawsvg as draw
d = draw.Drawing(1.5, 0.8, origin='center')
+
# Background pattern (not supported by Cairo, rasterize will not show it)
+
pattern = draw.Pattern(width=0.13, height=0.23)
+
pattern.append(draw.Rectangle(0, 0, .1, .1, fill='yellow'))
+
pattern.append(draw.Rectangle(0, .1, .1, .1, fill='orange'))
+
d.draw(draw.Rectangle(-0.75, -0.5, 1.5, 1, fill=pattern, fill_opacity=0.4))
# Create gradient
gradient = draw.RadialGradient(0, 0.35, 0.7*10)
···
d
```
+
[![Example output image](https://raw.githubusercontent.com/cduck/drawsvg/master/examples/example2.svg?sanitize=true)](https://github.com/cduck/drawsvg/blob/master/examples/example2.svg)
+
### Duplicate geometry and clip paths
```python
import drawsvg as draw
+15 -1
drawsvg/defs.py
···
class RadialGradient(DrawingDef):
'''
-
A radial gradient to use as a fill or other color
Has <stop> nodes as children, added with `.add_stop()`.
'''
···
'''A control point for a radial or linear gradient.'''
TAG_NAME = 'stop'
has_content = False
class ClipPath(DrawingDef):
'''
···
class RadialGradient(DrawingDef):
'''
+
A radial gradient to use as a fill or other color.
Has <stop> nodes as children, added with `.add_stop()`.
'''
···
'''A control point for a radial or linear gradient.'''
TAG_NAME = 'stop'
has_content = False
+
+
class Pattern(DrawingDef):
+
'''
+
A repeating pattern of other drawing elements to use as a fill or other
+
color.
+
+
Width and height specify the repetition period. Append regular drawing
+
elements to create the pattern.
+
'''
+
TAG_NAME = 'pattern'
+
def __init__(self, width, height, x=None, y=None,
+
patternUnits='userSpaceOnUse', **kwargs):
+
super().__init__(width=width, height=height, x=x, y=y,
+
patternUnits=patternUnits, **kwargs)
class ClipPath(DrawingDef):
'''
examples/example2.png

This is a binary file and will not be displayed.

+11 -7
examples/example2.svg
···
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
-
width="600" height="320.0" viewBox="-0.75 -0.4 1.5 0.8">
<defs>
-
<radialGradient cx="0" cy="0.35" r="7.0" gradientUnits="userSpaceOnUse" id="d0">
<stop offset="0.07142857142857142" stop-color="green" stop-opacity="1" />
<stop offset="0.1" stop-color="red" stop-opacity="0" />
</radialGradient>
-
<linearGradient x1="0.1" y1="0.35" x2="0.7" y2="0.14999999999999997" gradientUnits="userSpaceOnUse" id="d1">
<stop offset="0" stop-color="green" stop-opacity="1" />
<stop offset="1" stop-color="red" stop-opacity="0" />
</linearGradient>
</defs>
-
<rect x="-0.75" y="-0.5" width="1.5" height="1" fill="#ddd" />
-
<path d="M0.6062177826491071,5.551115123125783e-17 A0.7,0.7,0,0,0,-0.3499999999999998,-0.2562177826491071 L-0.2499999999999999,-0.08301270189221943 A0.5,0.5,0,0,1,0.43301270189221935,0.1 Z" fill="url(#d0)" stroke="black" stroke-width="0.002" />
-
<path d="M-0.48209070726490444,-0.22453333233923367 A0.75,0.75,0,0,0,-0.7047694655894312,0.09348489250574832 L0.0,0.35 A0,0,0,0,1,0.0,0.35 Z" fill="url(#d0)" stroke="red" stroke-width="0.002" />
-
<rect x="0.1" y="0.14999999999999997" width="0.6" height="0.2" stroke="black" stroke-width="0.002" fill="url(#d1)" />
</svg>
···
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
+
width="600" height="320.0" viewBox="-0.75 -0.4 1.5 0.8">
<defs>
+
<pattern width="0.13" height="0.23" patternUnits="userSpaceOnUse" id="d0">
+
<rect x="0" y="0" width="0.1" height="0.1" fill="yellow" />
+
<rect x="0" y="0.1" width="0.1" height="0.1" fill="orange" />
+
</pattern>
+
<radialGradient cx="0" cy="0.35" r="7.0" gradientUnits="userSpaceOnUse" id="d1">
<stop offset="0.07142857142857142" stop-color="green" stop-opacity="1" />
<stop offset="0.1" stop-color="red" stop-opacity="0" />
</radialGradient>
+
<linearGradient x1="0.1" y1="0.35" x2="0.7" y2="0.55" gradientUnits="userSpaceOnUse" id="d2">
<stop offset="0" stop-color="green" stop-opacity="1" />
<stop offset="1" stop-color="red" stop-opacity="0" />
</linearGradient>
</defs>
+
<rect x="-0.75" y="-0.5" width="1.5" height="1" fill="url(#d0)" fill-opacity="0.4" />
+
<path d="M0.6062177826491071,5.551115123125783e-17 A0.7,0.7,0,0,0,-0.3499999999999998,-0.2562177826491071 L-0.2499999999999999,-0.08301270189221943 A0.5,0.5,0,0,1,0.43301270189221935,0.1 Z" fill="url(#d1)" stroke="black" stroke-width="0.002" />
+
<path d="M-0.48209070726490444,-0.22453333233923367 A0.75,0.75,0,0,0,-0.7047694655894312,0.09348489250574832 L0.0,0.35 A0,0,0,0,1,0.0,0.35 Z" fill="url(#d1)" stroke="red" stroke-width="0.002" />
+
<rect x="0.1" y="0.15" width="0.6" height="0.2" stroke="black" stroke-width="0.002" fill="url(#d2)" />
</svg>