Py5Graphics.push()

The push() function saves the current drawing style settings and transformations, while Py5Graphics.pop() restores these settings.

Examples

example picture for push()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def setup():
    py5.size(100, 100, py5.P2D)

    g = py5.create_graphics(60, 60, py5.P2D)
    with g.begin_draw():
        g.translate(30, 30)
        with g.push():
            g.stroke("#F00")
            g.scale(2)
            with g.begin_closed_shape():
                g.vertex(-10, -10)
                g.vertex(10, -10)
                g.vertex(10, 10)
                g.vertex(-10, 10)
        with g.begin_closed_shape():
            g.vertex(-10, -10)
            g.vertex(10, -10)
            g.vertex(10, 10)
            g.vertex(-10, 10)

    py5.image(g, 0, 0)
    py5.image(g, 25, 25)

Description

The push() function saves the current drawing style settings and transformations, while Py5Graphics.pop() restores these settings. Note that these functions are always used together. They allow you to change the style and transformation settings and later return to what you had. When a new state is started with push(), it builds on the current style and transform information.

push() stores information related to the current transformation state and style settings controlled by the following functions: Py5Graphics.rotate(), Py5Graphics.translate(), Py5Graphics.scale(), Py5Graphics.fill(), Py5Graphics.stroke(), Py5Graphics.tint(), Py5Graphics.stroke_weight(), Py5Graphics.stroke_cap(), Py5Graphics.stroke_join(), Py5Graphics.image_mode(), Py5Graphics.rect_mode(), Py5Graphics.ellipse_mode(), Py5Graphics.color_mode(), Py5Graphics.text_align(), Py5Graphics.text_font(), Py5Graphics.text_mode(), Py5Graphics.text_size(), Py5Graphics.text_leading().

The push() and Py5Graphics.pop() functions can be used in place of Py5Graphics.push_matrix(), Py5Graphics.pop_matrix(), push_styles(), and pop_styles(). The difference is that push() and Py5Graphics.pop() control both the transformations (rotate, scale, translate) and the drawing styles at the same time.

This method can be used as a context manager to ensure that Py5Graphics.pop() always gets called, as shown in the example.

This method is the same as push() but linked to a Py5Graphics object. To see example code for how it can be used, see push().

Underlying Java method: PGraphics.push

Syntax

push() -> None

Updated on September 24, 2021 10:56:40am UTC