Skip to content

Changing defaults

Scott Pakin edited this page Mar 24, 2023 · 4 revisions

Transformations

A shortcut for applying the same transformation to multiple objects is to invoke

Function: transform(t)

This sets the default transformation to the string t. Objects specifying a transform parameter will prepend their transformations to the default (i.e., apply them afterwards).

Example:

transform('translate(100, 200) skewX(-15)')
for i in range(5):
    rect((i*100 + 10, 10), (i*100 + 40, 40))
    circle((i*100 + 75, 25), 15)

transform-command

The above first specifies that all future shapes be skewed to the right by 15° and moved 100 units to the right and 200 units down. It then draws a number of shapes, which will have those transformations applied to them. The following is an equivalent formulation using Transform objects:

Example:

tr = Transform()
tr.add_skewx(-15)
tr.add_translate(100, 200)
transform(tr)
for i in range(5):
    rect((i*100 + 10, 10), (i*100 + 40, 40))
    circle((i*100 + 75, 25), 15)

Styles

A shortcut for applying the same style to multiple objects is to invoke

Function: style(key=value, …)

This augments the default style with the given parameters. Use a value of None to remove the default value for key. Objects specifying key=value style parameters override any default value for the corresponding key. Here, too, a value of None cancels the effect of a previously assigned key.

The default style for most shapes is stroke='black', fill='none'. Text has an empty default, which SVG interprets as stroke='none', fill='black'.

Example:

style(stroke='red', stroke_width=2, stroke_dasharray='5,5')
cx, cy = canvas.width/2, canvas.height/2
for d in range(200, 20, -20):
    gray = 255 - int(d*255/200)
    shade = '#%02x%02x%02x' % (gray, gray, gray)
    rect((cx - d, cy - d), (cx + d, cy + d), fill=shade)

style

In the above, the stroke style is set once and inherited by all of the rectangles, which specify only a fill color.

Saving and restoring

The current default style and transformation can be saved with

Function: push_defaults()

and restored with

Function: pop_defaults()

Saving and restoring styles and transformations can be especially useful within user-defined functions. Without push_defaults and pop_defaults, the function propagates any changes to the default style and transformation to the caller, which may not be desirable; or it forgoes the use of the style and transform functions, leading to styles and/or transformations having to be repeated explicitly for each shape drawn. With push_defaults and pop_defaults, a user-defined function can take the following form:

def my_helper_function():
    push_defaults()
    style(…)
    # Draw lots of objects in the preceding style.pop_defaults()

push_defaults/pop_defaults pairs can be nested, as in the following example.

Example:

circle((100, 100), 25)   # Thin, black stroke, no fill

push_defaults()
style(fill='#d7eef4')    # Faint cyan fill
circle((200, 100), 25)

push_defaults()
style(stroke_width=5)    # Thick stroke
circle((300, 100), 25)

push_defaults()
style(stroke='#216778')  # Dark cyan stroke
circle((400, 100), 25)

pop_defaults()           # Undo dark cyan stroke
circle((500, 100), 25)

pop_defaults()           # Undo thick stroke
circle((600, 100), 25)

pop_defaults()           # Undo faint cyan fill
circle((700, 100), 25)

push-pop-style

Clone this wiki locally