begin_shape()#

Using the begin_shape() and end_shape() functions allow creating more complex forms.

Examples#

example picture for begin_shape()
1def setup():
2    py5.begin_shape()
3    py5.vertex(30, 20)
4    py5.vertex(85, 20)
5    py5.vertex(85, 75)
6    py5.vertex(30, 75)
7    py5.end_shape(py5.CLOSE)
example picture for begin_shape()
1def setup():
2    py5.begin_shape(py5.POINTS)
3    py5.vertex(30, 20)
4    py5.vertex(85, 20)
5    py5.vertex(85, 75)
6    py5.vertex(30, 75)
7    py5.end_shape()
example picture for begin_shape()
1def setup():
2    py5.begin_shape(py5.LINES)
3    py5.vertex(30, 20)
4    py5.vertex(85, 20)
5    py5.vertex(85, 75)
6    py5.vertex(30, 75)
7    py5.end_shape()
example picture for begin_shape()
1def setup():
2    py5.no_fill()
3    py5.begin_shape()
4    py5.vertex(30, 20)
5    py5.vertex(85, 20)
6    py5.vertex(85, 75)
7    py5.vertex(30, 75)
8    py5.end_shape()
example picture for begin_shape()
1def setup():
2    py5.no_fill()
3    py5.begin_shape()
4    py5.vertex(30, 20)
5    py5.vertex(85, 20)
6    py5.vertex(85, 75)
7    py5.vertex(30, 75)
8    py5.end_shape(py5.CLOSE)
example picture for begin_shape()
1def setup():
2    py5.begin_shape(py5.TRIANGLES)
3    py5.vertex(30, 75)
4    py5.vertex(40, 20)
5    py5.vertex(50, 75)
6    py5.vertex(60, 20)
7    py5.vertex(70, 75)
8    py5.vertex(80, 20)
9    py5.end_shape()
example picture for begin_shape()
 1def setup():
 2    py5.begin_shape(py5.TRIANGLE_STRIP)
 3    py5.vertex(30, 75)
 4    py5.vertex(40, 20)
 5    py5.vertex(50, 75)
 6    py5.vertex(60, 20)
 7    py5.vertex(70, 75)
 8    py5.vertex(80, 20)
 9    py5.vertex(90, 75)
10    py5.end_shape()
example picture for begin_shape()
1def setup():
2    py5.begin_shape(py5.TRIANGLE_FAN)
3    py5.vertex(57.5, 50)
4    py5.vertex(57.5, 15)
5    py5.vertex(92, 50)
6    py5.vertex(57.5, 85)
7    py5.vertex(22, 50)
8    py5.vertex(57.5, 15)
9    py5.end_shape()
example picture for begin_shape()
 1def setup():
 2    py5.begin_shape(py5.QUADS)
 3    py5.vertex(30, 20)
 4    py5.vertex(30, 75)
 5    py5.vertex(50, 75)
 6    py5.vertex(50, 20)
 7    py5.vertex(65, 20)
 8    py5.vertex(65, 75)
 9    py5.vertex(85, 75)
10    py5.vertex(85, 20)
11    py5.end_shape()
example picture for begin_shape()
 1def setup():
 2    py5.begin_shape(py5.QUAD_STRIP)
 3    py5.vertex(30, 20)
 4    py5.vertex(30, 75)
 5    py5.vertex(50, 20)
 6    py5.vertex(50, 75)
 7    py5.vertex(65, 20)
 8    py5.vertex(65, 75)
 9    py5.vertex(85, 20)
10    py5.vertex(85, 75)
11    py5.end_shape()
example picture for begin_shape()
1def setup():
2    py5.begin_shape()
3    py5.vertex(20, 20)
4    py5.vertex(40, 20)
5    py5.vertex(40, 40)
6    py5.vertex(60, 40)
7    py5.vertex(60, 60)
8    py5.vertex(20, 60)
9    py5.end_shape(py5.CLOSE)
example picture for begin_shape()
 1def setup():
 2    py5.translate(25, 50)
 3    py5.stroke_weight(4)
 4    py5.stroke("#F00")
 5    with py5.begin_closed_shape():
 6        py5.vertex(-20, -40)
 7        py5.vertex(20, -40)
 8        py5.vertex(20, 40)
 9        py5.vertex(-20, 40)
10
11    py5.translate(50, 0)
12    py5.stroke("#00F")
13    with py5.begin_shape():
14        py5.vertex(-20, -40)
15        py5.vertex(20, -40)
16        py5.vertex(20, 40)
17        py5.vertex(-20, 40)

Description#

Using the begin_shape() and end_shape() functions allow creating more complex forms. begin_shape() begins recording vertices for a shape and end_shape() stops recording. The value of the kind parameter tells it which types of shapes to create from the provided vertices. With no mode specified, the shape can be any irregular polygon. The parameters available for begin_shape() are POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, and QUAD_STRIP. After calling the begin_shape() function, a series of vertex() commands must follow. To stop drawing the shape, call end_shape(). The vertex() function with two parameters specifies a position in 2D and the vertex() function with three parameters specifies a position in 3D. Each shape will be outlined with the current stroke color and filled with the fill color.

Transformations such as translate(), rotate(), and scale() do not work within begin_shape(). It is also not possible to use other shapes, such as ellipse() or rect() within begin_shape().

The P2D and P3D renderers allow stroke() and fill() to be altered on a per-vertex basis, but the default renderer does not. Settings such as stroke_weight(), stroke_cap(), and stroke_join() cannot be changed while inside a begin_shape() & end_shape() block with any renderer.

This method can be used as a context manager to ensure that end_shape() always gets called, as shown in the last example. Use begin_closed_shape() to create a context manager that will pass the CLOSE parameter to end_shape(), closing the shape.

Underlying Processing method: beginShape

Syntax#

begin_shape() -> None
begin_shape(kind: int, /) -> None

Parameters#

  • kind: int - Either POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, or QUAD_STRIP

Updated on November 12, 2021 11:30:58am UTC