Name

createShape()

Description

The createShape() 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 above clarifies how this works.

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

The createShape() 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 above to see how it works.

After using createShape(), stroke and fill color can be set by calling methods like setFill() and setStroke(), as seen in the examples above. The complete list of methods and fields for the PShape class are in the Processing Javadoc.

Examples

  • PShape square;  // The PShape object
    
    void setup() {
      size(100, 100);
      // Creating the PShape as a square. The
      // numeric arguments are similar to rect().
      square = createShape(RECT, 0, 0, 50, 50);
      square.setFill(color(0, 0, 255));
      square.setStroke(false);
    }
    
    void draw() {
      shape(square, 25, 25);
    }
    
  • PShape s;  // The PShape object
    
    void setup() {
      size(100, 100);
      // Creating a custom PShape as a square, by
      // specifying a series of vertices.
      s = createShape();
      s.beginShape();
      s.fill(0, 0, 255);
      s.noStroke();
      s.vertex(0, 0);
      s.vertex(0, 50);
      s.vertex(50, 50);
      s.vertex(50, 0);
      s.endShape(CLOSE);
    }
    
    void draw() {
      shape(s, 25, 25);
    }
    
  • PShape s;
    
    void setup() {
      size(100, 100, P2D);
      s = createShape();
      s.beginShape(TRIANGLE_STRIP);
      s.vertex(30, 75);
      s.vertex(40, 20);
      s.vertex(50, 75);
      s.vertex(60, 20);
      s.vertex(70, 75);
      s.vertex(80, 20);
      s.vertex(90, 75);
      s.endShape();
    }
    
    void draw() {
      shape(s, 0, 0);
    }
    
  • PShape alien, head, body;
    
    void setup() {
      size(100, 100);
    
      // Create the shape group
      alien = createShape(GROUP);
    
      // Make two shapes
      ellipseMode(CORNER);
      head = createShape(ELLIPSE, -25, 0, 50, 50);
      head.setFill(color(255));
      body = createShape(RECT, -25, 45, 50, 40);
      body.setFill(color(0));
    
      // Add the two "child" shapes to the parent group
      alien.addChild(body);
      alien.addChild(head);
    }
    
    void draw() {
      background(204);
      translate(50, 15);
      shape(alien); // Draw the group
    }
    

Syntax

  • createShape()
  • createShape(type)
  • createShape(kind, p)

Parameters

  • kind(int)either POINT, LINE, TRIANGLE, QUAD, RECT, ELLIPSE, ARC, BOX, SPHERE
  • p(float[])parameters that match the kind of shape

Return

  • PShape
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.