Week 5 - Functions, Arguments, Fonts

Today

  • check-in
  • functions
  • functions with arguments!
  • review in-progress work
  • save
  • text
  • working time?

Check-in and Questions

Review drawing programs

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 p5js 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

So far, our 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.

We create a temporary variable name that will hold any passed in value. Then we use that variable name in our function somewhere. 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.

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

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
  3. Add a link to the font in your index.html file inside the <head> section.
  4. Use textFont to change to your font.
  5. Change size with textSize(18); and display text with text("my words",x,y);

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.

HW: Finish Drawing Tool

Part B due next week

Software like Adobe Photoshop appears at first to have infinite possibilities. In actuality it is constrained and specific. You can only do what the software allows you to do.

As long as artists have been making art, they have been inventing, designing, and altering their tools.

Becoming a programmer is often equated in our culture to developing magical powers or unlocking new abilities. Let’s embody this idea with the challenge to create our own art-making tools. For this assignment, consider what we have learned this semester, as well as the history of artist’s software tools.

In the p5js web editor, create your own custom art-making tool. Consider color, brushes, images, buttons, text.

  • save reference page

Part B - due October 7, 23:59

Final Requirements

  1. A catchy title
  2. A short description
  3. Screenshots of the tool/software
  4. Documentation of how to use the software
    • optional: short video screencast of you demonstrating your software
  5. Ability to save with your software
  6. 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.

Rubric

Concept and design:
  • Is the software concept clearly defined?
  • Is the concept novel and specific?
  • Are there clear visual indicators for functionality? (i.e. pictures or text on buttons indicate their functionality)
  • Does the workflow of the software make sense? (i.e. pressing a button leads to correct outcome when painting)
  • Are there visual indicators to show program state? (i.e. a color display showing current color, or a background color to show which button is depressed)
Programming:
  • Is the program code organized and logical?
  • Does the software function correctly?
  • Are the variable and function names useful and clear?
  • Does the program use comments throughout to make the logic clear?
  • Is the program broken down into separate useful logical functions that are called from draw?
Documentation:

To be complete, it must have:

  • A catchy title
  • A 1 paragraph description
  • Documentation of how to use the software consisting of text describing each button/option as well as
    • Screenshots of the tool/software in use demonstrating various functionality
    • optional: short video screencast of you demonstrating your software, with voiceover or text overlay
  • Ability to save with your software

EXTRA CREDIT:

  • A novel and proper use of looping in the program!

Concept:

Remember, to be considered complete, the code works, all buttons and other interactivity work. And the program documentation contains title, description, screenshots, walkthrough documentation of screenshots and text or ivideo, a save function, example artwork created with your program.

References

  • a list of experimental web-based drawing tools