Food&Code Food&Code Workshop

Brain-plosion


Home About

Course by for Food&Code

1) The big three of (our current) graphic vocabulary

If you’re asking yourself “what was there before there was anything?”, we have an answer. Before anything, there was the preload function, used to not just load, but preload. Who would have guessed. Loading before the setup what you’ll need later, like images.

Images are like rectangles. Once defined, you display them using the image function, feeding it positions and size.

Oupsy, while we’ll discuss variables a bit more in depth later, we’ll be already using them now. In the code below, img is a variable. A name, a container, that has some complexe values (which describe the image linked). What we’re doing here is a bit of a shortcut, we’ll see later why.

    function preload() {
        img = loadImage("http://foodandcode.github.io/assets/images/footer_back.png");
    }

    function setup() {
        //bla bla bla
        image(img,0,0,400,200);
    }

For that we use the function tint. You can feed it a color as you would with fill, stroke or background.

    tint(255,255,255,10); // Color, so plays like stroke and fill
    image(img,random(width)-100, random(height)-50, 200, 100);

2) Animation

Another possible brain. We’ll animate our lovely shapes. This animation needs to be described. Two main ways arise:

We’ll defined a movement using time, more precisely the function millis which gives us the number of milliseconds ellapsed. Let’s divide this because it can gets high pretty quickly, and feed that to the position of a rectangle.

    background(0,0,0);
    rect(millis()/1000, millis()/500, 50, 50);

Ok, this is boring. You’d better use that new power on something more fun.

      rect(width/2 + cos(millis()/100)*100, millis()/10, 50, 50);
      rect(width/2 + cos(millis()/100)*100, height/2 + sin(millis()/100)*100, 50, 50);
      fill(255,0,0,12);
      rect(width/2  + cos(millis()/100)*100 + random(10),
           height/2 + sin(millis()/100)*100 + random(10),
           50, 50); // Yep, you can do that. spaces or return to line are the same

Previously the evolution was based on time. This function would give us a value that would “change” over time, in a coherent and predictable manner: it would rise, and constantly, with time.

Similar evolution can be redone with more control. For that, we need something that we can control, and make vary. This vessel is called a variable. You already used a few: mouthX, height… Now, it’s time to use your own.

    var myVar; // Definition

// Where you define your variables defines where you can use it.
// For now, let's keep it here

    function preload() {
        //bla bla
        // Do not use an empty preload function...
    }

    function setup() {
        //bla bla
        myVar = width/2; // Initialisation
    }

    function draw() {
        //bla bla

        background(255,255,255);
        myVar = myVar + random(-5,5); // Update
        rect(myVar, 0, 10, 10); // Usage
    }

4) Press that button

Another couple of functions, keyPressed & keyTyped for classic keys and control keys. But among those function, a most important new keyword: if. The base of coputation. Thesis could be written on it, its importance, what it means, what it represents. It’s the atom of the computational world. What it does is test for something, and do one thing if it’s true (and optionaly another if it’s false). This is the basic of artificial reasoning. After the if, a condition is tested among parenthesis, and among curly brackets (as with functions), the block of code needed to be executed. If you want to do something when something is not valid, then use else as done below. You can test many things with conditions. Equality with ===, comparison with < and >… many more that we’ll discover another time!

Last: key and keycodes are the value of the key last types. Try it out a bit, it’ll make way more sense that way than reading a wall of text.

   function keyPressed() {
      if (keyCode === LEFT_ARROW) {
         background(255,random(255), 255);
            //keyCodes: https://p5js.org/reference/#/p5/keyCode 
      }
  }

   function keyTyped() {
      if (key === 'a') {
         background(255,0, 255);
      } else if (key === 'b') {
         background(255,255, 255);
      }

}