Week 5 - Functions, Arguments, Fonts

Today

  • custom functions
  • functions with arguments
  • review drawing software
  • save
  • text

Artists working with code

Camille Utterback

Geoffrey Drake-Drockman, Factum Arte, Janet Echelman, Miral Kotb

Modularity with Custom Functions

So far we’ve been using the builtin functions preload(), setup() and draw(). We’ve also covered event functions like mousePressed() and keyPressed(). Our programs are starting to get longer. And we’re starting to use if statements to create more complex program flows.

With increased functionality, our programs could become harder to organize. If our draw() loop is one giant block of code, it’s quite difficult to debug. Almost all programming languages add an ability to make a modular unit block of packaged code. This can sometimes be called a subroutine. In p5.js and most modern languages, we call a named block of code a function.

example

//defines the createPlanet function

function createPlanet(){
  fill(random(255),random(255),random(255));
  ellipse(random(width),random(height),20,20);
}

//below this is the place that actually calls
//(runs) our createPlanet function block

createPlanet();

Why do we create our own custom functions?

  • anytime you want to repeat a certain block of code, it helps to only have to write the entire thing out once. You can then call that function anywhere you want it to run.
  • creating functions makes our code modular. We can easily fit function calls (running a function) within if/else statements for example.
  • It helps us with debugging. We can more easily turn on and off specific functions when testing.

Functions with Arguments

All functions have always had a pair of twin parenthesis () after their name. This is where we can pass in variables to be used by a function.

When we use a built-in function like drawing a rectangle, the function expects to receive 4 pieces of data: x-position, y-position, width, height.

When we can a custom function we can create a temporary variable that will hold any passed in value. Then we use that variable in our function. When it’s called in the function, it will substitute in the passed in value. This might become clearer with some examples:

Example

function helloPerson(name){
  print("Hello "+name);
}

helloPerson('Rebecca');//prints 'Hello Rebecca'
helloPerson('Jerome'); //prints 'Hello Jerome'

Note that the quotes are necessary around the names passed in above. Without the quotes it assumes Rebecca and Jerome are variable names and will spit out an error in the console saying that Rebecca and Jerome are undefined.

function helloPerson(name){
  print("Hello "+name);
}

helloPerson(Jerome);//error: Jerome is not defined 

In-class practice with Functions

Create a face function that is called when you click a button. Choose a random color background, add eyes and a mouth. Each face generated should be slightly different. Pass in parameters for the x, y position, color, and anything else you’d like to add.

Example code for Don’t Worry Be Happy face function

Code Time: Refactoring Code: Pong

Go back and “refactor” your bouncing ball and Pong code. To refactor means to reorganize your code. Break your code into manageable functions that you can “call” from your draw() function.

Lee’s starter code

Save

Warning before we start! Don’t crash your browser. Don’t put save or SaveCanvas in your draw loop!

Save the current canvas as an image. The output depends on the Browser you are using for whether it automatically downloads to a folder or prompts a pop-up dialog.

save and saveCanvas

example:

saveCanvas('myCanvas', 'png')

example:

function keyTyped() {
  if (key === 's') {
    save('photo', 'png');
  }
}

Fonts

Fonts are a type of media and loaded similar to loading images with a key difference.

Calling loadFont() inside preload() guarantees that the load operation will have completed before setup() and draw() are called.

Steps:

  1. Find a font! You can use a system font that everyone has installed, like Arial or Tahoma, or find a new one that will be loaded from the web.
  2. For example, there are fonts at Google Fonts or at DaFont.
  3. Upload the font in your sketch. Pay attention to the filename, which you can rename to make it simpler to type.
  4. Create a variable to hold your font.
  5. Load the font into the variable inside the preload.
  6. Use textFont to change to your font.
  7. Change size with textSize(18); and display text with text("my words",x,y);
let f; //don't use font as a variable name
function preload(){
  f = loadFont("my-font-name.ttf");
}
function setup(){
  createCanvas(400,400);
  textFont(f);
  textSize(24);
  textAlign(CENTER,CENTER);
  text("Hello!",width/2,height/2);
}

Note that fill and stroke affect your fonts!

You can specify a width and height parameter to display text inside a box.

More info in the reference.

Homework

Read

Chapter 9 - Functions

Watch

Function basics

Function parameters and arguments

Functions and returns (new)

Code

Drawing Program

Part B - due October 3
Final Requirements

  • A catchy title
  • A short description
  • Screenshots of the tool/software
  • Documentation of how to use the software
    • optional: short video screencast of you demonstrating your software
  • Ability to save with your software
  • Create a folder of representative ‘good’ images that you’d want to demonstrate. There should be at least 3 works (saved screenshots) made with your tool.