What is P5? How is it distinguished from Processing?
P5.JS is a JavaScript library that starts with the original goal of Processing to make creative expression with code accessible to users with non-technical backgrounds such as artists, designers, and students. Unlike Processing, there is an added interactive element in which you are not limited to the confines of your canvas and can use built-in functions and libraries that allow you to use your entire browser (i.e. microphone and webcam access).
What does IDE stand for? Describe it’s components.
IDE is an “Integrated Development Environment”. This consists of a source code editor, build automation tools, and a debugger such as console.log.
How do you save a file in the P5 editor? What naming/saving convention might you use?
You can save your file by selecting the drop-down menu under “file” in the top left corner of your browser screen, or by typing command-s. Camelcase is a common naming/spelling convention in which the first letter of the word is lower-case and following words are capitalized with no spaces (ex: reallyCoolCode).
What is a library? How do you access and use a library with P5?
A library is a source of pre-written code which can be easily executed and allows for easier development. You can access and use the library by using methods to invoke the function (i.e. mic = new p5.AudioIn() activates microphone accessibility).
What do the triangle and circular shapes across the top of the P5 editor represent?
The triangle shape to the left means “play” or execute the code, and the square or “stop” ends the code and resets your canvas. Click the triangle to play your code again.
How do you add and name an additional or new tab in the P5 editor?
Click the > to the left of sketch.js (you’ll see it above the column with all the line numbers for your code) to expand the window. “Project-folder” should now appear to the left of >. Click > again, and select “add new file” or “add new folder” to create a new tab in the p5 editor.
Describe the coordinate system in P5.
The coordinate system in p5 starts from (0,0) in the bottom left corner of your canvas. Coordinates are set up using the convention of (x, y). Your x value is your width (left to right) and your y value is your height (down to up). Depending on how large your canvas is, your coordinate values may change.
What is the general syntax and structure for a line of code? Use code to demonstrate your response.
Identifiers/variables must be declared first before values are assigned to them. Example: let x = 5
What is the general syntax and structure for a block of code? Use code to demonstrate your response.
First variable, then value. If you are calling an argument, you must use the () to invoke the argument. Lines should be indented and curly brackets { } should be used within your block of code.
let x = 6
function
function addOne() {
if (x > 7) {
x++
}
}
Why are certain words in different colors in the P5 editor?What is a system or reserved word in P5? Give an example.
Depending on the context of the word within your code, P5 will recognize it as a certain type of code (i.e. variable, function, operator) and highlight it a certain color. Some special keywords such as “mouseIsPressed” are built-into p5 which means that the syntax cannot change for that word (ex: “let mouseIsPressed = x” would not be valid).
How does order matter in P5? Give an example demonstrated with code.
Order matters because certain code must be executed first before it can work later in your program.
/*calling the preLoad() function before loadImage() ensures that the image is ready for rendering in P5. this is placed before setup so that we can use a callback later*/
function preload() {
// Image source/credit: https://www.dgreetings.com/birthday/birthday-candles.html
img = loadImage(“assets/birthdaycandles1123.jpg”);
}
What is the difference between mouseIsPressed and mousePressed()? Use code to demonstrate your response.
mouseIsPressed is a boolean value, and mousePressed() is a method/function.
function reset() { //added a reset function
if (mouseIsPressed) {
continue;
}
}
function mousePressed() {
circX = circX + 10;
}
What called function must always be first in setup()?
The function createCanvas() must always be called first in setup. Without a canvas, we can’t draw() 😦
What is the difference between an inline comment and a block or multi-line comment? Use code to demonstrate your response.
An in-line comment starts with // and will only comment out one line. A multi-line comment begins with /* and ends with */ and can encompass multiple lines of code. Please see examples below.
//This is the line that sets the background color
/*EDIT- further revised with method functions that work! finally figured out how to properly use the .this keyword without breaking everything. Also added a reset function that activates when the spacebar or any key is pressed*/
Does whitespace matter in P5? Capitalization? Use code to demonstrate your response.
Yes and no. As long as your code is formatted correctly, whitespace isn’t the biggest deal. Capitalization can cause problems however, especially when you start working with constructor functions. Javascript will then start to look for that capitalized function name.
What is a variable? What is a data type? What is a value?
A variable, simply put, is a placeholder for a value. A data type is the definition for that value, i.e. strings (words), numbers, boolean values (true or false). You can store multiple pieces of data within an array (let myArray = [0, 2, 4, 6, 12, etc.]) or within objects the latter can thought of as a collection of properties (let color = { R: 200, G: 0, B: 100 } ) An object can be thought of as a collection of properties.
What is the difference between a system variable and one that you define? Use code to demonstrate your response.
A system variable or constant is a value that cannot be altered by the program during normal execution. For example, the variable “height” will never change as this measures the y value of your canvas. You can create and name new variables, however you must begin your line with the keyword “let”
let car = “Toyota” //we declared the variable car and assigned the string value “Toyota”
What is scope and how does it impact code? Use code to demonstrate your response.
JavaScript has functional and global scope. Each function creates a new scope. Scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function. Variables declared outside the function (global scope) can be accessed anywhere within your code.
//carName = “Toyota” is not accesible to code written here
function myCar() {
let carName = “Toyota”;
// code here CAN use carName (“Toyota”)
}
//carName is not accesible here because it is outside the functional scope
What does it mean to declare, initialize and use a variable? Use code to demonstrate your response.
Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the let keyword. You can then initialize and use that variable later in your code as follows
let circ = {
x: 0,
y: 400,
diam: 2,
r: 255,
g: 20,
b: 25
}
let color = {
r: 0,
g: 0,
b: 0,
}
function sunRise() {
circ.x = circ.x + speed;
circ.y = circ.y – speed;
circ.diam = circ.diam + 1;
circ.g = circ.g + 1;
color.g = color.g + 1;
color.b = color.b + 2;
console.log(“It’s time to wake up!”)
}
What happens when a variable is attempted to be accessed outside of its scope?
The error message “undefined” appears.
What happens when a variable is declared globally, but is not used?
JavaScript has a built-in garbage collection system that collects unused variables and frees memory. JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used.
What value does var nums; have?
This has a number value attached to it (i.e. 0, 5, etc.).
What are operators in P5? Use code to demonstrate your response using at least one operator.
JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more. The below operator ” || ” is a boolean logical operator which compares if either argument is true. This is called the “or” operator.
function mousePressed() {
if (mouseX >= width / 2 || mouseY >= height / 2)
if (mouseIsPressed) {
background(bgSwitch);
fill(0, 0, 255);
ellipse(width / 2, height / 2, diam, diam);
}
}
What is a boolean data type?
A boolean data type returns either of two values i.e. true or false. Boolean is used as a function to get the value of a variable, object, conditions, expressions etc. in terms of true or false.
List three examples of primitive data types.
true //boolean
false //boolean
char //stands for character, which can include integers(numbers).
Write a code example that increments a value by 5.
let x = 0
function incrementor() {
let x = x + 5
}
Describe the order of operations for: x = speed * 40;
The * stands for multiplication, so therefore the value of x is whatever value was assigned to speed within the global scope, then multiplied by 40. There are no other arithmetic operators.
Write a code example that decreases a value by one using shorthand.
let x = 5
function makeFour() {
x = x–
}
What does the logical operator ! do?
! stands for “not.” I.e. != not equal.
What is an if statement? When should it be used? Use code to describe its syntax.
An if statement is used to specify a block of JavaScript code to be executed if a condition is true.
function greeting() {
if (hour < 18) {
return “Good day”;
}
}
How many if statements can you use? What is an alternative to the if statement?
You can use as many if statements as you want, however it may be more beneficial to use “else if” or “else” statements to run different blocks of code for different arguments.
What is the difference between else and else if? Use code to demonstrate your response.
The “else if” statement specifies a new condition if the first condition is false. The “else” statement specifies a block of code to be executed if both the ‘if” and “else if” conditions are false.
if (age >= 21) {
greeting = “Welcome to the bar!”;
} else if (age = 20) {
greeting = “One more year til beer!”;
} else {
greeting = “No booze for you, kiddo.”;
}
What is the difference between code with several consecutive if statements and code with several else if statements?
With several if statements, only one of
What is a while loop? When should it be used? Use code to describe its syntax.
A while loop is a line of code that is executed while the condition or argument is true. When that argument is met, the code stops.
while (i > 10) {
i++;
}
What is a for loop? When should it be used? Use code to describe its syntax.
A “for loop” loops through a block of code a number of times. It has the following syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
for (i = 0; i < 5; i++) { text += “The number is ” + i + “
“;
}
Write code that uses a nested for loop to draw a grid of squares across the x and y axis of a canvas.
function draw() {
// For every i add 60. Spaces rectangles out.
for (let i = 0; i <= width; i += 60) {
// For every j add 60. Spaces rectangles out.
for (let j = 0; j <= height; j += 60) {
// Fill based on a random value and the mouse position
fill(mouseX, random(255), mouseY);
// Draw at i, j for each rectangle’s x and y position.
rect(i, j, 35, 35);
}
}
}
What is a function?
A function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes (calls) it.
What is the difference between a function or method built into P5 and one that you define?
Functions and methods built into P5 cannot be renamed, changed or assigned new variables. A user-defined function can be changed as necessary.
What does the keyword function mean?
The keyword function tells the computer to expect a function to be called, and to then search for the code assigned to that function to be executed.
What does the keyword return mean? Write code that uses the keyword return.
The return statement stops the execution of a function and returns a value from that function.
function heightFinder() {
return height;
}
Write code that uses a defined function (by the user) and call or use it.
function draw() {
noStroke();
background(color.r, color.g, color.b);
fill(circ.r, circ.g, circ.b);
ellipse(circ.x, circ.y, circ.diam, circ.diam);
sunRise();
skyChange();
}
function sunRise() {
circ.x = circ.x + speed;
circ.y = circ.y – speed;
circ.diam = circ.diam + 1;
circ.g = circ.g + 1;
color.g = color.g + 1;
color.b = color.b + 2;
console.log(“It’s time to wake up!”)
}
function skyChange() {
if (circ.x > width || circ.y < 0) {
background(0, 255, 255);
stroke(40);
textAlign(CENTER);
text(‘Good Morning!’, 200, 200);
textSize(50);
}
}
What is the distinction between function and method?
A function is a block of code designed to perform a particular task. A JavaScript method is a property containing a function definition. JavaScript methods are actions that can be performed on objects.
What is the distinction between argument and parameter?
A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method’s parameters. Parameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function.
What do the () in a function call or definition indicate?
The parentheses in a function call the parameter, or argument that is to be passed to the function.
What will happen if you call an undefined function?
You will receive an “undefined” error message.
What will happen if you define a function, but do not call or use it?
Nothing if you run the code, however JavaScript’s built-in garbage collection will take care of unused variables and functions to free up memory space.
What concept are functions useful for?
Functions can be re-used in your code as many times as needed. This adds to the flexibility, or modularity of code.
What is an object?
An object is a placeholder for a collection of properties assigned to a certain item. These properties can be values of any type, including numbers, text strings, arrays, or even other objects, and functions.
What data type is an object?
It is a data type like an array, which is also a placeholder for multiple values/properties.
What concept are objects, classes/constructors and arrays useful for?
Modularity! They are all reusable code.
What is the difference between an object and a class/constructor function? Use code to demonstrate your response.
let circ1 = {
x: 400,
y: 0,
size: 300,
display: function() {
ellipse(this.x, this.y, this.size, this.size)
}
}
function draw() {
background(bG.R, bG.G, bG.B);
stroke(s.R, s.G, s.B);
strokeWeight(s.sW);
fill(color.R, color.G, color.B);
circ1.display(); //no “new” keyword or capitalization, not a constructor
}
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 200; i++) {
stars[i] = new Star(); //constructor
}
}
function Star() { //yup definitely a constructor
this.x = random(0, width);
this.y = random(0, height);
this.display = function() {
stroke(random(255));
point(this.x, this.y);
}
}
What is the this keyword used for?
“This” is the object that “owns” the code.The value of “this,” when used in an object, is the object itself. In a constructor function, “this” does not have a value. It is a substitute for the new object. The value of “this”will become the new object when a new object is created.
What is dot syntax? Use code to demonstrate your response.
Dot syntax is used as a property accessor. For example, in console.log(“Hello World!), the dot between console and log accesses the console, and logs the string “Hello World” onto the console.
What is the keyword new used for?
The new keyword is used in javascript to create a object from a constructor function. The new keyword must be called before the constructor function for it to run.
What is a constructor? Where and when is it used? Use code to demonstrate your response.
A constructor function is used to create multiple objects from the same property. For example:
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 200; i++) {
stars[i] = new Star();
}
}
function Star() {
this.x = random(0, width);
this.y = random(0, height);
this.display = function() {
stroke(random(255));
point(this.x, this.y);
}
}
Organize original code to include the main program in one tab and a class or constructor in another. Use in-line comments to walkthrough code.
<script src = “circ1.js” type = “text/javascript”></script> //this allows access to the tab
function setup() {
createCanvas(400, 400);
}
let circ1 = { //this would be within the circ1.js tab
x: 400,
y: 0,
size: 300,
display: function() {
ellipse(this.x, this.y, this.size, this.size)
}
}
function draw() { //this would be within the main program window
background(bG.R, bG.G, bG.B);
stroke(s.R, s.G, s.B);
strokeWeight(s.sW);
fill(color.R, color.G, color.B);
circ1.display();
}
What is an array?
An array is a special type of object which is used to used to store multiple values in a single variable.
What notation is used to denote an array?
let array = [ ]
How do data types impact arrays?
Depending on which variable types are stored within an array, this can affect the boolean value for your array. For example, an array that contains numbers and strings will not pass true if your function or argument is searching for numeric values only.
What is an index?
An index number is the position of the variable or value within an array. The index number always starts at 0.
Write code that declares, initializes and accesses elements in an array. Use comments to walkthrough code.
let flavors = [“apple”, “strawberry”, “banana”, “grape”]
console.log(flavor[1]) //the flavor “strawberry” would print onto the console.
List two or three functions that can be called on an array. Describe how they manipulate the array.
You can use array.length and array.push. Array.length returns the length or number of items in an array, and array.push will add (or push) a new element to the end of the array, increasing the length of the array by 1.
Bonus: What is NPM? Give an example of a module that can be downloaded and installed from NPM.
NPM is the world’s largest software registry which can be used for open source software development. Here is an example of a module that can be downloaded.
Bonus: List one text editor that can be used in lieu of an IDE. How can a P5 project be coded and run usinga text editor?
Repl.it is an online text editor (and one that I’m actually familiar with, for once.) You can run projects created in Repl by saving/downloading the file and uploading it into P5, or by copying and pasting your code.
What does CLI stand for?
Command Line Interface
How can the current directory be identified using the command line?How can the contents of a directory (current) be viewed?
$pwd – print current directory, then hit enter
What command must be used to make a directory or folder using the CLI?
The “make directory” command
How can recent commands be viewed on the command line?
You can use the arrow keys. By pressing the ARROW UP key, you can step through the last commands you called (starting with the most recently used). ARROW DOWN will move forward in history towards the most recent call.
What shortcut keyboard combination can be used to automatically complete a path in the commands line?
The tab key will autocomplete for you.
What naming convention can be used to save an application or program?
From my understnding, camelCase is a viable option.
https://stackoverflow.com/questions/30840745/are-there-any-naming-conventions-for-command-line-arguments