Other instance outside shape

Group

Group is an element that hold other elements inside it (can include inner group too). No need to fill any basic argument, but you can always add extras like SVG tags:

draw.Group(**kwargs)

The grouped elements (pie and small_pie) get rotated and its stroke become blue

d = draw.Drawing(400, 300)

degree = 45
pole_x = 100
pole_y = 150
group1 = draw.Group(transform=f"rotate({degree} {pole_x} {pole_y})", stroke="blue")
pie = draw.Pie(cx=100, cy=150, r=100, startDeg=0, endDeg=90, fill= "None")
group1.append(pie)
small_pie = draw.Pie(cx=100, cy=150, r=80, startDeg=0, endDeg=90, fill= "None")
group1.append(small_pie)

pie2 = draw.Pie(cx=300, cy=150, r=100, startDeg=0, endDeg=90, fill= "None", stroke="pink")

d.append(group1)
d.append(pie2)
d

svg

Gradient

Linear

gradient = draw.LinearGradient(x1, y1, x2, y2, gradientUnits='userSpaceOnUse', **kwargs)
gradient.addStop(offset, color, opacity=None)

(cx,cy) point to its center, r to its radius offset is in between 0 and 1

d = draw.Drawing(400, 300)

gradient = draw.LinearGradient(*[0,0],*[1,0], gradientUnits='objectBoundingBox')
gradient.addStop(0, 'pink', 1)
gradient.addStop(1, 'cyan', 0)
bg = draw.Rectangle(
    x=0,y=0,
    width="100%",height="100%",
    fill=gradient)
d.append(bg)
d

svg

Radial

gradient = draw.RadialGradient(cx, cy, r, **kwargs)
gradient.addStop(offset, color, opacity=None)

(cx,cy) point to its center, r to its radius offset is in between 0 and 1

d = draw.Drawing(400, 300, idPrefix='pic')

gradient = draw.RadialGradient(200,150,100)
gradient.addStop(0, 'green', 1)
gradient.addStop(1, 'red', 1)


bg = draw.Rectangle(x=0,y=0,width="100%",height="100%",fill=gradient)
d.append(bg)

d

svg

Clip

Clip is an element that hold other elements inside it (such as path, rect, etc). No argument:

clip_name = draw.ClipPath()

To add shape as Clip, use .append() method.
To apply Clip, fill clip_path argument with clip_name

d = draw.Drawing(200, 100, idPrefix='clippy')

clip = draw.ClipPath()
clip.append(draw.Rectangle(0,0,100,100))

# Draw a cropped circle
c = draw.Circle(100,100,100,
                fill="cyan", clip_path=clip,
                id='circle')

d.append(c)
d

svg

Mask

A drawing where the gray value and transparency are used to control the transparency of another shape (White = Full opacity, Black = Total transparency). No Argument:

mask_name = draw.Mask()

To add shape as Clip, use .append() method.
To apply Clip, fill mask argument with mask_name

d = draw.Drawing(200, 100, idPrefix='masky')

gradient = draw.LinearGradient(*[0,0],*[1,0], gradientUnits='objectBoundingBox')
gradient.addStop(0, 'white')
gradient.addStop(1, 'black')

mask = draw.Mask()
box = draw.Rectangle(
    x=30,y=0,
    width=100,height=100,
    fill=gradient)
mask.append(box)

#initial shape
rect = draw.Rectangle(
        x=0,y=0,
        width=200,height=100,
        fill="cyan",)
d.append(rect)

#After Mask
rect = draw.Rectangle(
        x=0,y=0,
        width=200,height=100,
        fill="pink", mask=mask)
d.append(rect)
d

svg

Implementing other SVG Tags

Not all SVG tags are available within this library, so it's convenient if we can make up new specific implementation of some tags. Basically, every tag implementations are a class that based on draw.DrawingParentElement. Here is the template to make new implementation:

class Hyperlink(draw.DrawingParentElement):
    TAG_NAME = 'a'
    def __init__(self, href, target=None, **kwargs):
        # Other init logic...
        # Keyword arguments to super().__init__() correspond to SVG node
        # arguments: stroke_width=5 -> stroke-width="5"
        super().__init__(href=href, target=target, **kwargs)
d = draw.Drawing(200, 100, idPrefix='hyperlink')
hlink = Hyperlink('https://www.python.org')

hlink.append(draw.Circle(*[100,50],50, fill='green'))

hlink.append(draw.Text('Hyperlink',20, *[100,50], text_anchor="middle", fill='white'))

# Draw and display
d.append(hlink)
d.setRenderSize(200)
d

svg