ViewBox Mixin

class svgwrite.mixins.ViewBox

The ViewBox mixin provides the ability to specify that a given set of graphics stretch to fit a particular container element.

The value of the viewBox attribute is a list of four numbers min-x, min-y, width and height, separated by whitespace and/or a comma, which specify a rectangle in user space which should be mapped to the bounds of the viewport established by the given element, taking into account attribute preserveAspectRatio.

ViewBox.viewbox(minx=0, miny=0, width=0, height=0)

Specify a rectangle in user space (no units allowed) which should be mapped to the bounds of the viewport established by the given element.

Parameters:
  • minx (number) – left border of the viewBox
  • miny (number) – top border of the viewBox
  • width (number) – width of the viewBox
  • height (number) – height of the viewBox
ViewBox.stretch()

Stretch viewBox in x and y direction to fill viewport, does not preserve aspect ratio.

ViewBox.fit(horiz='center', vert='middle', scale='meet')

Set the preserveAspectRatio attribute.

Parameters:
  • horiz (string) – horizontal alignment 'left | center | right'
  • vert (string) – vertical alignment 'top | middle | bottom'
  • scale (string) – scale method 'meet | slice'
Scale methods Description
'meet' preserve aspect ration and zoom to limits of viewBox
'slice' preserve aspect ration and viewBox touch viewport on all bounds, viewBox will extend beyond the bounds of the viewport

Transform Mixin

class svgwrite.mixins.Transform

The Transform mixin operates on the transform attribute. The value of the transform attribute is a <transform-list>, which is defined as a list of transform definitions, which are applied in the order provided. The individual transform definitions are separated by whitespace and/or a comma. All coordinates are user space coordinates.

Transform.translate(tx, ty=None)

Specifies a translation by tx and ty. If ty is not provided, it is assumed to be zero.

Parameters:
  • tx (number) – user coordinate - no units allowed
  • ty (number) – user coordinate - no units allowed
Transform.rotate(angle, center=None)

Specifies a rotation by angle degrees about a given point. If optional parameter center are not supplied, the rotate is about the origin of the current user coordinate system.

Parameters:
  • angle (number) – rotate-angle in degrees
  • center (2-tuple) – rotate-center as user coordinate - no units allowed
Transform.skewX(angle)

Specifies a skew transformation along the x-axis.

Parameters:angle (number) – skew-angle in degrees, no units allowed
Transform.skewY(angle)

Specifies a skew transformation along the y-axis.

Parameters:angle (number) – skew-angle in degrees, no units allowed
Transform.scale(sx, sy=None)

Specifies a scale operation by sx and sy. If sy is not provided, it is assumed to be equal to sx.

Parameters:
  • sx (number) – scalar factor x-axis, no units allowed
  • sy (number) – scalar factor y-axis, no units allowed

Presentation Mixin

class svgwrite.mixins.Presentation

Helper methods to set presentation attributes.

Presentation.fill(color=None, rule=None, opacity=None)

Set SVG Properties fill, fill-rule and fill-opacity.

Presentation.stroke(color=None, width=None, opacity=None, linecap=None, linejoin=None, miterlimit=None)

Set SVG Properties stroke, stroke-width, stroke-opacity, stroke-linecap and stroke-miterlimit.

Presentation.dasharray(dasharray=None, offset=None)

Set SVG Properties stroke-dashoffset and stroke-dasharray.

Where dasharray specify the lengths of alternating dashes and gaps as <list> of <int> or <float> values or a <string> of comma and/or white space separated <lengths> or <percentages>. (e.g. as <list> dasharray=[1, 0.5] or as <string> dasharray=’1 0.5’)

MediaGroup Mixin

SVG Tiny 1.2

valid for SVG Elements: animation, audio, desc, image, metadata, title, video

class svgwrite.mixins.MediaGroup

Helper methods to set media group attributes.

MediaGroup.viewport_fill(color=None, opacity=None)

Set SVG Properties viewport-fill and viewport-fill-opacity.

Markers Mixin

class svgwrite.mixins.Markers

Helper methods to set marker attributes.

Markers.set_markers(markers)

Set markers for line elements (line, polygon, polyline, path) to values specified by markers.

  • if markers is a 3-tuple:
    • attribute ‘marker-start’ = markers[0]
    • attribute ‘marker-mid’ = markers[1]
    • attribute ‘marker-end’ = markers[2]
  • markers is a string or a Marker class:
    • attribute ‘marker’ = FuncIRI of markers

Clipping Mixin

class svgwrite.mixins.Clipping
Clipping.clip_rect(top='auto', right='auto', bottom='auto', left='auto')

Set SVG Property clip.