---
description: Below walks through the code updating it in ways we did on Thursday.
---

# Week 1 Processing Exercise

The steps below walk you through the Processing activity we did during Week 1. All code should allow you to copy and paste.

## Step 1: Create a Canvas

Remember:&#x20;

* setup() runs once on the first frame, draw() runs once per frame until it's stopped
* Think of blocks of code like packages:
  * Any opening parenthesis, curly bracket, and quotation mark need a closing one
  * Close the blocks within before the containing blocks
* End lines of code with a semicolon

Use the size(width, height) function within the { } of setup()

```java
void setup() { 
  size(600, 600);
}

void draw() {
  
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFlqFFxRTnbDAVqMs4O%2Fimage.png?alt=media\&token=9e1ff195-a88a-4a37-a4a0-a1477ecb3cc5)

When no background color is provided in the code, this is the gray it will default to.

## Step 2: Create a Shape

Use the 2D Primitives section in Processing.org's Reference to see what's available and what information a shape needs: [https://processing.org/reference/](https://processing.org/reference/)

Here, I'll use circle(x-position, y-position, diameter)

```java
void setup() { 
  size(600, 600);
}

void draw() {
  circle(300, 300, 50);
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFlr1V4maeCUer3-7E_%2Fimage.png?alt=media\&token=c5cb6e3e-640f-4a18-8288-45c1038ee0a3)

The default color for the stroke is black (0,0,0) and the default color for fill is white (255, 255, 255).

## Step 3: Add Color

Color is handled in Processing by default as red, green, and blue values ranging from 0 (no color) to 255 (most saturated color). All colors are a combination of these values. If you only put in one value, Processing assumes you want all three values to be the **same number**, making the color **a shade of gray**.

{% hint style="info" %}
There is a fourth value we did not cover called "alpha," which is also the color's opacity. It ranges from 0-1 with 0 making it **invisible/transparent** and 1 being completely visible.
{% endhint %}

To change the color of a shape, it needs to be stated before drawing the shape. It's like telling the artist, "Here is your paint. Now, paint this..."

Use stroke(red, green, blue) and fill(red, green, blue) to set the color for the stroke and fill.

{% hint style="info" %}
Use noStroke() or noFill() without any values to have the stroke or fill removed.
{% endhint %}

Here, I'll use a black background, a cyan fill, and a dark gray stroke.

```java
void setup() { 
  size(600, 600);
}

void draw() {
  background(0);
  stroke(115);
  fill(0, 255, 255);
  circle(300, 300, 50);
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFlxIt1SXsgv5M5dY9D%2Fimage.png?alt=media\&token=9e0d3767-ee91-4560-86ef-768f2e6d9216)

## Step 4: Using Mouse Coordinates

In Processing, there are built-in variables called "mouseX" and "mouseY" that provide the value of the x-position and the y-position of the cursor while it is within the canvas. This value is updated every frame when put in the draw() block.

Let's use mouseX and mouseY instead of coordinates.

```java
void setup() { 
  size(600, 600);
}

void draw() {
  background(0);
  stroke(115);
  fill(0, 255, 255);
  circle(mouseX, mouseY, 50);
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFlyDKrICE0CaWFaAyL%2Fstep4.gif?alt=media\&token=e723e058-11df-44ec-86d7-b00b593f72f9)

For each frame, the program is drawing a black background, then a cyan circle with a gray outline/stroke.

## Step 5: Draw the Background Only Once

Let's move the background color to the setup() block so it is only drawn once and the circle is drawn repeatedly.

```java
void setup() { 
  size(600, 600);
  background(0);
}

void draw() {
  stroke(115);
  fill(0, 255, 255);
  circle(mouseX, mouseY, 50);
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFlz9qEtICE_wAj_d7Y%2Fstep5.gif?alt=media\&token=3f9d24df-5e07-46af-baf6-2c0a4e416fe6)

## Step 6: Remove the Stroke

Removing the stroke will make the shape appear to blend/blur together.

Let's use noStroke() to remove the outline.

```java
void setup() { 
  size(600, 600);
  background(0);
}

void draw() {
  noStroke();
  fill(0, 255, 255);
  circle(mouseX, mouseY, 50);
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFlzu6OwwgmFPws8YWp%2FStep6.gif?alt=media\&token=a138ff99-1329-4b0d-9dbe-7cfd2a37aa26)

## Step 7: Add Some Randomness to Size

**random(min, max)** is a common function that takes a minimum number and a maximum number and provides a "random" number between them.

Let's use it first on the size of the circle, making the diameter somewhere between 1 and 75 pixels.

```java
void setup() { 
  size(600, 600);
  background(0);
}

void draw() {
  noStroke();
  fill(0, 255, 255);
  circle(mouseX, mouseY, random(1, 75));
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFm0CBvBhH3_B39_Fj6%2FStep7.gif?alt=media\&token=671aa93b-dc3d-404b-8235-5f201deda280)

## Step 8: Add Randomness to the Red Values

Let's add a random value between 0 and 255 to the red values of the fill color:

```java
void setup() { 
  size(600, 600);
  background(0);
}

void draw() {
  noStroke();
  fill(random(0, 255), 255, 255);
  circle(mouseX, mouseY, random(1, 75));
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFm1N5kS4SuBqvOCOKQ%2FStep8.gif?alt=media\&token=7f42c36c-190a-4562-82b9-ad7fbe8295d4)

Since both green and blue values are at their maximum, increases in the red value bring the color closer to white (255, 255, 255).

## Step 9: Add More Randomness

Let's make all the color values random!

```java
void setup() { 
  size(600, 600);
  background(0);
}

void draw() {
  noStroke();
  fill(random(0, 255), random(0, 255), random(0, 255));
  circle(mouseX, mouseY, random(1, 75));
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFm2Ga2KrElMPNMg0I7%2FStep9.gif?alt=media\&token=7370f1be-4a5c-45f5-84a6-126b8ae9d5ed)

## Step 10: Using a Variable

This is jumping ahead, but let's make the speed in which we move the mouse on the X-axis to determine the size of the circle.

What's a quick way of determining speed? Speed really is the amount of time it takes for an object to change. In this case, we want to change the size of the circle based on how much change happened between two frames.&#x20;

To record/hold a value to use later, we use **variables**. Variables are similar to reusable labels or nicknames that stand for specified information.

mouseX is updated at the beginning of each frame, so we can't use it by itself. Here, we will "declare" a variable called lastXPosition to hold the information of one frame's mouseX information to use in the next frame.&#x20;

"float" is used here to tell Processing the type of information it will have. We'll cover this next week.

The single equals sign = is a way of "assigning" or "setting" the value of a variable. So lastXPosition = 0 means "I want lastXPosition to be 0."

{% hint style="info" %}
It is good practice to have a starting or "initial" value to avoid errors.
{% endhint %}

Put variables that will be used for the project before and outside of the setup() and draw() blocks. We want these to be accessed outside of those restrictions.

To declare a variable, use this syntax:\
&#xNAN;_&#x74;ype_ nameOfVariable = _initialValue_;

```java
float lastXPosition = 0;
```

Once you create a variable, you can update it within the draw() block:

```java
lastXPosition = mouseX;
```

{% hint style="info" %}
You do not need the type of variable when you are updating it.
{% endhint %}

{% hint style="info" %}
Updating variables to hold values that change like mouseX are best put at the end.
{% endhint %}

To determine the speed, we will subtract the last frame's mouseX from the current one.

That looks like this:

```java
mouseX - lastXPosition
```

However, this could give negative values. Lets use a built-in math function for "absolute power" or abs(value) to make the value positive.

That looks like this:

```java
abs(mouseX - lastXPosition)
```

Now that we have what we need to get a value for the diameter, let's use it in the circle code:

```java
float lastXPosition = 0;

void setup() { 
  size(600, 600);
  background(0);
}

void draw() {
  noStroke();
  fill(random(0, 255), random(0, 255), random(0, 255));
  circle(mouseX, mouseY, abs(mouseX - lastXPosition));
  lastXPosition = mouseX;
}
```

Looks like:

![](https://4077345522-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MFloEXqUun2rk6R28rU%2F-MFloI7y1aGu4D5qtDyg%2F-MFm7XKLKDAzrAI4qzN6%2FStep10.gif?alt=media\&token=220bc492-4ebf-4b56-9255-4b7d9520c9d0)

