# Bootstrap

Under Construction

Programmers use the term bootstrapping to describe a sequence of events that start simple and become more complex over time; the end goal is often to get a system “up and running.” This tutorial aims to get you up and running with p5.js.

The button below opens a code editor in a new tab. Set up your free account and let’s get to it!

## First Sketch

When you open the p5.js Web Editor, you’ll find a ready-made computer program waiting for you. Programs are called sketches in p5.js lingo.

function setup() {
createCanvas(400, 400);
}

function draw() {
background(220);
}


In the p5.js Web Editor, click the button, then click the button. What happened?

Let’s unpack this sketch a little. Functions bundle related information and instructions together under a common name. Think of them as a recipe, or procedure, your computer must follow from top to bottom. The sketch defines two functions, setup and draw, that are essential ingredients. They in turn use, or call, the functions createCanvas and background. Altogether you painted a $400\times400$ canvas light gray. Neat!

— Schoolhouse Rock!

In mathematics, functions like $f(x) = x + 1$ take an input and produce some sort of output. Most functions you’ll encounter operate on numbers as their input, sometimes several at once! $f$ simply adds one to whatever number $x$ you provide.

Functions in computer science are more flexible. They can take arguments as input, or not, and return an output, or not. You could define $f$ in JavaScript like so.

function f(x) {
return x + 1;
}


Back to the original sketch, it looks like setup and draw don’t take any arguments. createCanvas and background are called with two and one arguments, respectively. We don’t use their output directly.

Edit the sketch so that background is called with $100$ as an argument. Try a few numbers between $0$ and $255$.

## Functions Review

What is a function in computer science? How do they compare to functions in mathematics?

Pull out a notebook and write down your answer in language an $8$-year-old could understand. Better yet, explain functions to a child. If you’re struggling to find the words then you may have identified gaps in your knowledge. Nice work—you know what you need to learn! Review the notes above and visit the p5.js website for more extensive reference. Try explaining again and repeat as needed. In sum,

1. Try explaining in simple language
2. Identify gaps in your knowledge
3. Review
4. Refine and simplify

## Hello, Point

Painting a canvas one solid color works if you’re Ellsworth Kelly, but we’ll tack in a different direction.

The sketch below includes a note, or comment. Comments begin with two forward slashes // and continue to the end of the line. Think of comments as notes to your future self. Days, weeks, or months from now, you will have no idea what you were thinking at this moment.

function setup() {
createCanvas(400, 400);
}

function draw() {
background(220);
point(200, 200); // wait, what's a point?
}


Draw a point in the middle of your canvas by calling the function point(200, 200).

Protip: The semicolon at the end of each function call is like punctuation at the end of a sentence; it’s (very) easy to forget them.

The result might be a little… confusing. Did anything happen?

If you look closely, you’ll see a single, solitary point drawn in the middle of your canvas. Hey little fella!

The canvas you created is $400\times400$ picture elements, or pixels, wide and high. Imagine your screen divided into squares like a checkerboard; each square is a pixel shining with a particular color. The point you drew is $1\times1$ pixels, so it’s like you used a fine tip pen.

function setup() {
createCanvas(400, 400);
}

function draw() {
background(220);
strokeWeight(8);  // try different weights
point(200, 200);
}


Adjust the thickness, or weight, of your pen with the strokeWeight function.

The point at $(200, 200)$ now has a weight of $8$ pixels. Much better.

## $\lambda$ Graph Paper

Why do we need to call createCanvas and point with two numbers as arguments?

You may have seen a Cartesian coordinate system before. Cartesian coordinates in two dimensions define location by an ordered pair of numbers $(x, y)$. The first number is the horizontal x-coordinate, or abscissa, and the second number is the vertical y-coordinate, or ordinate.

Imagine a blank piece of paper divided into squares by a bunch of lines that cross, or intersect, at right angles. If you’ve used graph paper then you’ve got the idea.

The point you drew at $(200, 200)$ wound up at the center of your $400\times400$ canvas. Coordinates are usually given relative to the point $(0, 0)$, called the origin. In computer graphics, the origin is located at the top-left corner.

Draw another point at $(50, 100)$. Where did it show up?

## Coordinates Review

What is a coordinate system? How does p5.js lay out its coordinate system?

Remember, your goal is to explain coordinates so simply that a third-grader could understand.

## Grayscale

What color did you paint your canvas? It’s gray, sure, but that description isn’t precise enough for your computer to follow.

Hopefully you played around with background earlier and saw that changing the argument changed the color of your canvas. At the extremes, $0$ paints your canvas black and $255$ paints it white. The range of numbers from $0\text{--}255$ covers $256$ distinct values of gray, called a grayscale.

Your computer stores color in its memory as a sequence of binary digits, or bits. Think of bits as tiny switches with a value of either $0$ (off) or $1$ (on). Combining eight bits, or one byte, gives us $2^{8} = 256$ possible shades of gray.

What happens if you call background with three arguments? For example, background(0, 120, 240).