Programmatically generate SVG (vector) images, animations, and interactive Jupyter widgets
1''' 2A library for creating SVG files or just drawings that can be displayed in 3Jupyter notebooks 4 5Example: 6``` 7 import drawsvg as draw 8 9 d = draw.Drawing(200, 100, origin='center') 10 11 # Draw an irregular polygon 12 d.append(draw.Lines(-80, 45, 13 70, 49, 14 95, -49, 15 -90, -40, 16 close=False, 17 fill='#eeee00', 18 stroke='black')) 19 20 # Draw a rectangle 21 r = draw.Rectangle(-80, -50, 40, 50, fill='#1248ff') 22 r.append_title("Our first rectangle") # Add a tooltip 23 d.append(r) 24 25 # Draw a circle 26 d.append(draw.Circle(-40, 10, 30, 27 fill='red', stroke_width=2, stroke='black')) 28 29 # Draw an arbitrary path (a triangle in this case) 30 p = draw.Path(stroke_width=2, stroke='lime', fill='black', fill_opacity=0.2) 31 p.M(-10, -20) # Start path at point (-10, -20) 32 p.C(30, 10, 30, -50, 70, -20) # Draw a curve to (70, -20) 33 d.append(p) 34 35 # Draw text 36 d.append(draw.Text('Basic text', 8, -10, -35, fill='blue')) # 8pt text at (-10, -35) 37 d.append(draw.Text('Path text', 8, path=p, text_anchor='start', line_height=1)) 38 d.append(draw.Text(['Multi-line', 'text'], 8, path=p, text_anchor='end', center=True)) 39 40 # Draw multiple circular arcs 41 d.append(draw.ArcLine(60, 20, 20, 60, 270, 42 stroke='red', stroke_width=5, fill='red', fill_opacity=0.2)) 43 d.append(draw.Arc(60, 20, 20, 60, 270, cw=False, 44 stroke='green', stroke_width=3, fill='none')) 45 d.append(draw.Arc(60, 20, 20, 270, 60, cw=True, 46 stroke='blue', stroke_width=1, fill='black', fill_opacity=0.3)) 47 48 # Draw arrows 49 arrow = draw.Marker(-0.1, -0.51, 0.9, 0.5, scale=4, orient='auto') 50 arrow.append(draw.Lines(-0.1, 0.5, -0.1, -0.5, 0.9, 0, fill='red', close=True)) 51 p = draw.Path(stroke='red', stroke_width=2, fill='none', 52 marker_end=arrow) # Add an arrow to the end of a path 53 p.M(20, 40).L(20, 27).L(0, 20) # Chain multiple path commands 54 d.append(p) 55 d.append(draw.Line(30, 20, 0, 10, 56 stroke='red', stroke_width=2, fill='none', 57 marker_end=arrow)) # Add an arrow to the end of a line 58 59 d.set_pixel_scale(2) # Set number of pixels per geometry unit 60 #d.set_render_size(400, 200) # Alternative to set_pixel_scale 61 d.save_svg('example.svg') 62 d.save_png('example.png') 63 64 # Display in Jupyter notebook 65 d.rasterize() # Display as PNG 66 d # Display as SVG 67``` 68''' 69 70from .defs import * 71from .raster import Raster 72from .drawing import Drawing 73from .types import ( 74 Context, 75 DrawingElement, 76 DrawingBasicElement, 77 DrawingParentElement, 78) 79from .elements import * 80from .video import ( 81 render_svg_frames, 82 save_video, 83) 84from .frame_animation import ( 85 FrameAnimation, 86 frame_animate_video, 87 frame_animate_jupyter, 88 frame_animate_spritesheet, 89) 90from .native_animation import ( 91 SyncedAnimationConfig, 92 animate_element_sequence, 93 animate_text_sequence, 94) 95from .url_encode import ( 96 bytes_as_data_uri, 97 svg_as_data_uri, 98 svg_as_utf8_data_uri, 99)