p5js week 09

(photo from https://www.flickr.com/photos/carowallis1/2260652819)

Phew! It's been lots of toast over the past eight lessons! We'd better take a lesson to catch up and review what we've learned.

We'll review in two parts: general JavaScript and then p5js. You should also review how to put your work on the wiki and make sure you've added all of your work there. That lets you share it with your teachers and classmates, and eventually with the public, too!

Be sure to ask your teacher if you need help with any of the items listed in the review below. OK, let's review!

(photo from https://pixahive.com/photo/morning-breakfast-2/)

You can open the console in the browser by pressing F12, selecting “developer's tools” from the menu, or right-clicking to inspect and then choosing the console tab.

We can store values in variables; then we can use them later. When we do that, we use the “let” keyword.

let aNumber = 10;

Making functions is super important! The basic pattern is like this:

// function functionName (arguments) {
//    stuff you want to do;
//    let aValueYouCalculated = the result of some computation above...
//    return aValueYouCalculated // to get out the value you wanted to compute
// }

// which looks like this real function:

function addTwoThenTimes100 (number) {
  let preNumber = number + 2;
  let finalNumber = preNumber * 100;
  return finalNumber

We can call a function by using its name and then giving values for the arguments. We can use variables we declared before as the arguments. That works like this:

addTwoThenTimes100 (aNumber)

The arguments are the input to the function, and the return statement is the output.

We also learned another way to write functions called arrow functions. That pattern looks like:

// let functionName = argument => stuff you want to do

// which looks like this real function:

let addTwoThenTimes100_2 = number => (number + 2) * 100

addTwoThenTimes100_2 (aNumber)

When we wrap up some calculations inside of a function, that's called encapsulation. When we change a value inside and make it an argument so that we can use the function in more situations, we're doing what's called generalization. That's like this:

function addTwoThenTimesX (number, x) {
  let preNumber = number + 2;
  let finalNumber = preNumber * x;
  return finalNumber

addTwoThenTimesX (aNumber, 500)

We can use many kinds of values in our functions, including numbers, strings (“hi!”), booleans (true or false).

Be careful about what order you do stuff; that's called thinking carefully about your procedure.

We can group data together, also called structuring data, into arrays or objects.

Arrays can tell us how long they are when we use the length method. There are also cool methods like forEach, map, and filter (check lessons 6, 7, and 8) that let us avoid using for-loops. We also learned about adding things to arrays using push and unshift, as well as removing things from arrays using pop.

However, if we want to use a for-loop, we can. Remember the basic pattern of a for-loop:

// for (counter variable; stopping condition; increment) {
//  what you want to do on each loop
// }

// you can use it like this inside a function...

function addCounterToBaseNumber (baseNumber, n) {
  let finalNumber = baseNumber;
  for (let i = 0; i < n; i++) {
    finalNumber = finalNumber + i
  return finalNumber

We made some useful functions, including randomRange, pick, and buildArray.

Often when we use objects, we make a class that makes it easier to make objects. We can make objects using the constructor from the class. To use the constructor, we start with the “new” keyword.

(photo from https://www.flickr.com/photos/30478819@N08/50322224721)

You've learned about the setup and draw functions.

You know how to use some basic shapes like rect and ellipse; these use x and y coordinates for their positions.

We made a Point class to make positions and changing them easier.

You've learned about fill and stroke, and you know how to use a color picker to find the numbers for colors that you want to use. We learned about RGB color values and HSL color values (go back and review lesson 2).

You know how to draw text in p5js, including adding text that tells you where the mouse is.

We also learned how to make not only triangles but other shapes using our own randomShape function. That required using beginShape and endShape.

We learned how to move the shapes and rotate them using transform and rotate, and we also learned how to get back to the original rotation and size using resetMatrix.

(photo from https://www.flickr.com/photos/92800334@N02/49013804871)

For a couple of weeks, you should have been working on your third drawing.

  1. draw an animal, plant, or a complicated shape from your imagination
  2. there should be many different versions of it, like different sizes and different colors
  3. make a class to draw it
  4. use buildArray to fill an array of objects from your class; try to make 100 or more!
  5. use filter to change your drawing in some way

Can you make some progress with this and add it to your wiki page? Be sure to add your other drawings, too. Ask the teachers for help if you need it. They'd be happy to help!

  • p5js-week-09.txt
  • Last modified: 5 days ago
  • by renick