1 of 44

P5.js

Introduction to p5.js

2 of 44

Things to do, read and watch:

  1. Create a p5 account
  2. Watch the Hello P5 video
  3. Do this p5 tutorial
  4. Check out the p5 reference

3 of 44

Objectives

  • Using code, draw a robot in p5 that you’ll post online
  • Gain comfort with text-based programming using the p5 development environment
  • Review and apply CS concepts to make computer graphics.
  • Create working examples for your lesson development.

4 of 44

Quick Reflection

  • What did you see in the video?
  • What things do you think you can make with p5?
  • How is it similar or different to Scratch? Do you know what Scratch is?

5 of 44

Unplugged Activity

gged activity

Instructions: Link to Worksheet

1. Draw a robot using basic shapes

Steps:

  1. Use only three basic shapes: Triangles, Ellipses, Rectangles

B. Combine up to 5 shapes total. Repeat shapes if needed.

C. Place the shapes on the coordinate system on the worksheet following the guidelines below

6 of 44

Unplugged Activity

Ellipses:

  1. In the grid system, pick a center of an ellipse first.
  2. Decide a width and height for your ellipse.
  3. Complete your ellipse on the grid system

7 of 44

Unplugged Activity

Rectangles:

  1. Place rectangles based on their upper left corner.
  2. Decide a width and height for your rectangle.
  3. Complete your rectangle on the grid system.

8 of 44

Unplugged Activity

Triangles:

  1. Place the points of a triangle carefully on the coordinate system.

9 of 44

Example

gged activity

10 of 44

2. Deconstruct your drawing

On the second page of the worksheet, list each shape used in your drawing. In the instructions section, provide the information for each shape as if you were instructing someone to draw your robot including x-coordinate, y-coordinate, width, and height (when applicable.)

gged activity

Unplugged Activity

11 of 44

3. Translate your instructions into code using the following syntax.

Ellipse: ellipse(x,y,width,height)

Rectangle: rect(x,y,width,height)

Triangle: triangle(x1,y1,x2,y2,x3,y3)

gged activity

Unplugged Activity

12 of 44

Link to Worksheet

13 of 44

Reflection

  • What was challenging?
  • Why was it challenging?
  • What was easy?
  • How would do this activity with your kids?
  • What would you add to your robot?

14 of 44

Overview

  • The p5.js web editor
  • Drawing shapes in p5.js
  • Use variables in drawings
  • Apply color to p5.js drawings

15 of 44

P5.js editor

What you’ll need for this Learning Activity

16 of 44

p5.js extends the Javascript programming language. Javascript is the standard language of the web. It allows web designers to make web pages interactive as opposed to static documents..

The p5 web editor allows for p5 sketches to be created, edited and saved in the browser.

What is it?

17 of 44

Play and Stop

Execute and stop

Your code

Main Navigation Menu

Editing Window

Write and edit

code

Console

Displays error

And print

messages

Preview

Displays code that is

run

Preferences

Change text size, theme

Color, toggle autosave

How does it look?

18 of 44

What can we do with it?

19 of 44

Project: Use code to draw the robot that you made on graph paper in the unplugged activity in the p5 editor.

20 of 44

Concepts

  • Functions
  • Parameters
  • P5.js coordinate system
  • Comments
  • Shape functions
  • P5.js reference
  • Color
  • Variables

21 of 44

We’ll cover all of these concepts today by breaking them down so that they’re easy to understand. We’ll begin with functions. Functions are lines of code that perform specific tasks.

Functions

22 of 44

  • Create a Canvas

This line of code is a function that creates a p5.js canvas that is 600 pixels wide and 240 pixels high. The canvas is an element on webpage on which we will draw our graphics.

23 of 44

  • Create a Canvas

We will add lines of code to add graphics to that canvas.

First, play with the size of the canvas here

24 of 44

  • Add a Background Color

This function gives our canvas a background color between 0 (black) and 255 (white). background(value)

Change the shade of the canvas here.

25 of 44

Functions are lines of code that perform specific tasks. P5 has a function to draw a line, a function to draw a rectangle, and many many more. When we write one of these lines of code we are “calling a function.”

Two functions are automatically added

to your sketch when you open the editor.

Runs the bracketed code once when the program begins

Runs the bracketed code repeatedly in a loop

Functions

26 of 44

Read the following slides on parameters and turn and talk:

  1. What are the parameters in createCanvas?
  2. What is the syntax of parameters?
  3. Why might parameters be a powerful concept for you and your students to learn and use?

Parameters

27 of 44

Parameters are the values inside of the parentheses following the name of the function. These parameters will be different based on the information that the function needs. The createCanvas function needs two parameters from the programmer; the width and the height of the canvas. The background function needed the color value which in this case was a shade of gray between 0 and 255.

Parameters

28 of 44

  • In order to use the functions in the p5.js library, we will need to know their names, and the parameters that they need to be drawn on the screen.
  • Take a look at the p5.js reference. You’ll see a list of functions that perform different tasks. If you click on the function, you’ll see the parameter that the function needs.
  • Check out these functions:
    • ellipse()
    • rect()
    • fill()
  • What can you make with those functions?

Parameters

29 of 44

P5 makes drawing on the canvas easy by providing functions for us to draw shapes, but we need to be able to tell the program where to draw those shapes. To do that, we need to understand the coordinate system that we’ll be drawing in.

P5.js Coordinate System

30 of 44

  • Point and Line Functions

Play with the point and line functions here.

Notice what each parameter controls.

point- The function name to draw a point is “point”. The parameters are (x,y) which are the x and y coordinates. In the p5 coordinate system, the top left corner is (0,0)

31 of 44

  • Point and Line Functions

line- The function name to draw a line is “line.” Since a line is just a connection between two points, the parameters to draw a line are (x1,y1,x2,y2) which are the x and y coordinates of the two points that we want to connect with a line. The first x and y are the starting point and and the second are the end point.

32 of 44

Read the following slide on comments and add your answers to today’s padlet

  1. What type of information do you put in comments?
  2. Why is commenting important to programming?
  3. How could you use comments to plan your project?
  4. How could you use it to help you program?
  5. How could you use it to help your kids program?

Comments

33 of 44

Lines with two slashes (//) before them will be read as comments in p5.js and will be ignored by the interpreter. The interpreter is the program that executes the instructions written in a programming language.

Programmers add comments to their code as notes to themselves or others who might be reading it. These comments will be ignored so they will not affect how the program works.

Comments are also useful when your program does not behave as expected. You can turn off or “comment out” lines of code one by one to find out which line is causing the problem. Different languages can use different characters to create comments.

Commenting code is excellent practice and should be constantly encouraged.

Comments

34 of 44

  • Basic Shapes : Draw a Rectangle

The rect function takes four parameters: x, y, width and height. For the rect function to work, it needs to know where to put the rectangle (the x and y locations) and what size the rectangle should be (width and height).

Activity: Draw another rectangle to the right of the rectangle here.

35 of 44

function setup( ){ this function runs once

createCanvas(600,120);

}

function draw( ){ this function runs in a loop

background(204);

rect(40,40,400,60); this shape is drawn first

ellipse(335,50,50,50); this shape is drawn second

}

Play with the order of the shapes here

Drawing Order

36 of 44

The program is executed from the top down so the last shape drawn will be on top

Drawing Order

37 of 44

  • Fill and Stroke
  • noStroke() - gets rid of the line around the shape
  • noFill() - gets rid of the shape color
  • strokeWeight() - determines the thickness of the line around the shape
  • fill() - determines the color of the shape

Play with the strokeWeight and color here

38 of 44

Today’s Project:

Use code to draw on p5 the robot that you drew on graph paper.

39 of 44

  • RGB

One way to add color to our drawings is to use RGB values. We will need to pass three parameters to the fill or stroke function: one for red, one for green, and one for blue.

fill(red, green, blue)

stroke(red, green, blue)

Find an RGB color picker here

Play with the color values here

40 of 44

  • Hex Color Codes

If you remember hex colors from CSS, they can also be used in p5 by writing the hex code in quotes.

fill(‘#fffff’)

41 of 44

  • Display the mouse location on the canvas

We are going to learn more about these built in variables a bit later, but for now, we’ll add the following after our background in the draw function:

  • text(mouseX + ‘,’ + mouseY, 20,20);

This will display our mouse location on the canvas to make it easier to draw more complex shapes.

42 of 44

Extra Project: Add color to your robot. Use RGB values, vary the stroke colors and stroke weights.

43 of 44

Reflection:

  1. What was the easiest part of this activity?
  2. What was the hardest or most challenging part of this activity?
  3. What concepts did we use in the activity?
  4. What practices did we use?
  5. What are your concerns when it’s time to teach this activity?
  6. How would you change it for your students?

44 of 44

Resources