create_shape()#

The create_shape() function is used to define a new shape.

Examples#

example picture for create_shape()
 1def setup():
 2    global s  # the Py5Shape object
 3    # creating the Py5Shape as a square. the
 4    # numeric arguments are similar to rect().
 5    s = py5.create_shape(py5.RECT, 0, 0, 50, 50)
 6    s.set_fill("#0000FF")
 7    s.set_stroke(False)
 8
 9
10def draw():
11    py5.shape(s, 25, 25)
example picture for create_shape()
 1def setup():
 2    global s  # the Py5Shape object
 3    # creating a custom Py5Shape as a square, by
 4    # specifying a series of vertices.
 5    s = py5.create_shape()
 6    s.begin_shape()
 7    s.fill(0, 0, 255)
 8    s.no_stroke()
 9    s.vertex(0, 0)
10    s.vertex(0, 50)
11    s.vertex(50, 50)
12    s.vertex(50, 0)
13    s.end_shape(py5.CLOSE)
14
15
16def draw():
17    py5.shape(s, 25, 25)
example picture for create_shape()
 1def setup():
 2    global s
 3    py5.size(100, 100, py5.P2D)
 4    s = py5.create_shape()
 5    s.begin_shape(py5.TRIANGLE_STRIP)
 6    s.vertex(30, 75)
 7    s.vertex(40, 20)
 8    s.vertex(50, 75)
 9    s.vertex(60, 20)
10    s.vertex(70, 75)
11    s.vertex(80, 20)
12    s.vertex(90, 75)
13    s.end_shape()
14
15
16def draw():
17    py5.shape(s, 0, 0)
example picture for create_shape()
 1def setup():
 2    # create the shape group
 3    global alien
 4    alien = py5.create_shape(py5.GROUP)
 5
 6    # make two shapes
 7    py5.ellipse_mode(py5.CORNER)
 8    head = py5.create_shape(py5.ELLIPSE, -25, 0, 50, 50)
 9    head.set_fill("#FFFFFF")
10    body = py5.create_shape(py5.RECT, -25, 45, 50, 40)
11    body.set_fill("#000000")
12
13    # add the two "child" shapes to the parent group
14    alien.add_child(body)
15    alien.add_child(head)
16
17
18def draw():
19    py5.background(204)
20    py5.translate(50, 15)
21    py5.shape(alien)  # draw the group

Description#

The create_shape() function is used to define a new shape. Once created, this shape can be drawn with the shape() function. The basic way to use the function defines new primitive shapes. One of the following parameters are used as the first parameter: ELLIPSE, RECT, ARC, TRIANGLE, SPHERE, BOX, QUAD, or LINE. The parameters for each of these different shapes are the same as their corresponding functions: ellipse(), rect(), arc(), triangle(), sphere(), box(), quad(), and line(). The first example clarifies how this works.

Custom, unique shapes can be made by using create_shape() without a parameter. After the shape is started, the drawing attributes and geometry can be set directly to the shape within the begin_shape() and end_shape() methods. See the second example for specifics, and the reference for begin_shape() for all of its options.

The create_shape() function can also be used to make a complex shape made of other shapes. This is called a “group” and it’s created by using the parameter GROUP as the first parameter. See the fourth example to see how it works.

After using create_shape(), stroke and fill color can be set by calling methods like Py5Shape.set_fill() and Py5Shape.set_stroke(), as seen in the examples. The complete list of methods and fields for the Py5Shape class are in the py5 documentation.

Underlying Processing method: createShape

Syntax#

create_shape() -> Py5Shape
create_shape(kind: int, /, *p: float) -> Py5Shape
create_shape(type: int, /) -> Py5Shape

Parameters#

  • kind: int - either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE

  • p: float - parameters that match the kind of shape

  • type: int - either GROUP, PATH, or GEOMETRY

Updated on March 22, 2022 21:53:01pm UTC