Section outline

      • circle
        • A special case of ellipse
      • rect
      • line
      • text
      • …

      You’ll find them, and others, in the reference documentation.

      Math

      The usual +, -, * and /

      circle(width/2, mouseY - 100, mouseX * 0.3);

      Iterating over a collection of items?

      For example the Celestial Emporium of Benevolent Knowledge (Borges 1942)

      animals = ["belonging to the emperor", "embalmed", "tame", "suckling pigs"];
      
      animals.forEach(animal => {
          console.log(animal);
      });

      We will want to move on to do something else than console.log the them.

      animals = ["belonging to the emperor", "embalmed", "tame", "suckling pigs"];
      
      animals.forEach(animal => {
          draw(animal);
          animate(animal);
          interact_with(animal);
          ...
      });

      For instance drawing them on the canvas.

      animals = ["belonging to the emperor", "embalmed", "tame", "suckling pigs"];
      
      function setup() {
        createCanvas(400, 400);
      }
      
      function draw() {
        background(220);
        
        animals.forEach(animal => {
            text(animal, mouseX, mouseY);
        });
      }

      Array of concentric circles

      Same idea, iterating over a collection of numbers circle sizes rather than peculiar animals (strings).

      https://editor.p5js.org/mace/sketches/gYF243mRu

      sizes = [30, 50, 60, 70, 30, 10, 5]; // I made them up!
      
      function setup() {
        createCanvas(400, 400);
      }
      
      function draw() {
        background(200);
        noFill();
        sizes.forEach(size => {
          circle(width/2, height/2, size * 5)
        })
      }

      Note the drawing order

      https://editor.p5js.org/mace/sketches/NnXYHnynN

      sizes = [30, 50, 60, 70, 30, 10, 5];
      
      function setup() {
        createCanvas(400, 400);
      }
      
      function draw() {
        background(220);
        angle = height/mouseY * 2;
        // noStroke();
        sizes.forEach((size, index) => { // NB. the second argument index
          ellipse(width/2,               // center horizontally along x
                  100 + (1/index) * 200, // lay them vertically along y
                  size*5,                // stretch horizontally
                  size*0.3*angle)        // squeeze vertically
        })
      }

      A composition of lines

      Still the same pattern: define an array of data, then iterate for each of them. This time drawing lines.

      https://editor.p5js.org/mace/sketches/y-TuBhQxU

      points = [[10, 40],
                [50, 60],
                [30, 150],
                [340, 250],
                [180, 100]
               ];
      
      function setup() {
        createCanvas(400, 200);
      }
      
      function draw() {
        background("white");
        strokeWeight(6);
        points.forEach(point => {
                      line(point[0], 0, point[1], height)
        });
      }

      You can study examples from Benedikt Groß, Hartmut Bohnacker, Julia Laub, Claudius Lazzeroni's book Generative Design: Visualize, Program, and Create with JavaScript in p5.js.

      Exploration of shapes and math

      Make 3 little programs, this time with feeling.

      • Explore use of math to place your shapes
      • Explore reacting to mouseX and mouseY
      • Explore iterating over a collection of items

      Try to read (and modify!) the code of one of the programs in Generative Design.

      [!note] Next time make sure to bring pen and paper!