Programmatically generate SVG (vector) images, animations, and interactive Jupyter widgets
1 2from .elements import DrawingElement, DrawingParentElement 3 4 5class DrawingDef(DrawingParentElement): 6 ''' Parent class of SVG nodes that must be direct children of <defs> ''' 7 @property 8 def id(self): 9 return self.args.get('id', None) 10 @id.setter 11 def id(self, newId): 12 self.args['id'] = newId 13 def getSvgDefs(self): 14 return (self,) 15 def writeSvgDefs(self, idGen, isDuplicate, outputFile): 16 DrawingElement.writeSvgDefs(idGen, isDuplicate, outputFile) 17 18class DrawingDefSub(DrawingParentElement): 19 ''' Parent class of SVG nodes that are meant to be descendants of a Def ''' 20 pass 21 22class RadialGradient(DrawingDef): 23 ''' A radial gradient to use as a fill or other color 24 25 Has <stop> nodes as children. ''' 26 TAG_NAME = 'radialGradient' 27 def __init__(self, cx, cy, r, gradientUnits='userSpaceOnUse', fy=None, **kwargs): 28 yShift = 0 29 if gradientUnits != 'userSpaceOnUse': 30 yShift = 1 31 try: cy = yShift - cy 32 except TypeError: pass 33 try: fy = yShift - fy 34 except TypeError: pass 35 super().__init__(cx=cx, cy=cy, r=r, gradientUnits=gradientUnits, 36 fy=fy, **kwargs) 37 def addStop(self, offset, color, opacity=None, **kwargs): 38 stop = GradientStop(offset=offset, stop_color=color, 39 stop_opacity=opacity, **kwargs) 40 self.append(stop) 41 42class GradientStop(DrawingDefSub): 43 ''' A control point for a radial or linear gradient ''' 44 TAG_NAME = 'stop' 45 hasContent = False 46