Skip to content

Commit c45d7ce

Browse files
authored
Merge pull request #19 from xinemata/your-branch-name
Updates for Art + Code El Paso
2 parents 4618820 + 5d15776 commit c45d7ce

12 files changed

Lines changed: 835 additions & 492 deletions

.DS_Store

0 Bytes
Binary file not shown.

_curriculum/02-04_Lessons.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
# This is the frontmatter
3+
title: "Lost and Found" # Title and Heading 1
4+
permalink: /lostAndFound-lessons/4 # Give your page a permalink
5+
published: true
6+
---
7+
8+
## Variables
9+
10+
### Grade 6~8
11+
12+
#### **Recap**
13+
14+
So far, we've looked at 2-D Primitive Shapes, how to place them on the screen, how to layer them on those of each other, and how to color them in. These are nice "Still Lifes" we've made. But what if we want to make your image to be less "still" by enabling some animations or interactions with key presses and mouse clicks? First, we'll have to get comfortable with the concept of variables.
15+
16+
#### **What is a Variable?**
17+
18+
A variable is a container used to store data. Picture a box that lets you store data.
19+
20+
But what do we mean by data? These data can be numbers:
21+
22+
![Variable box that stores numbers]({{ "/assets/images/curriculum/Unit-2_Sample-3.png" | relative_url }})
23+
24+
Or words:
25+
26+
![Variable box that stores numbers]({{ "/assets/images/curriculum/Unit-2_Sample-4.png" | relative_url }})
27+
28+
Or your favorite images and songs:
29+
30+
![Variable box that stores numbers]({{ "/assets/images/curriculum/Unit-2_Sample-5.png" | relative_url }})
31+
32+
We store data in these boxes so that they can be used as resources while a computer program is running.
33+
34+
#### **Creating a Variable**
35+
36+
In order to create a variable, we need to give the variable a name. Think of this as the label you will be taping on the box to remind yourself what you've put inside the box.
37+
38+
![Variable box labeled tomatoes]({{ "/assets/images/curriculum/Unit-2_Sample-1.png" | relative_url }})
39+
40+
If we want to indicate that there are 3 items inside the `tomatoes` box, we would write it out like this:
41+
42+
```js
43+
let tomatoes = 3; // assign 3 to the tomatoes variable
44+
45+
function setup() {
46+
createCanvas(400, 400);
47+
console.log(tomatoes); // load tomatoes variable
48+
}
49+
```
50+
51+
**Tip:** Conventionally, the first letter of a variable name is written in lower-case.
52+
{: .notice--success}
53+
54+
There are some funny things here. First, we're using a new word called `let`. What is `let`? You can think of `let` like a magic spell that lets us create variable to store data.
55+
56+
> **🧠Teacher Trick:**
57+
> Think of yourself as creating a new world, declaring aloud **let** there be light!
58+
> By declaring with “let”, we’re proclaiming something (a variable) into existence in our world of code.
59+
60+
We're also using another JavaScript feature called `console.log()` to load the content of our `tomatoes` variable. `console.log()` helps us to keep track of what's inside a variable and debug code. We will be using it a lot to complete this project.
61+
62+
Also, `let tomatoes = 3;` is being written above `function setup()`! That's right. Think about the computer program like a factory assembly line, and you need to have all of your boxes lined up before loading them into the machines.
63+
64+
**Tip:** By default, you will be creating variables at the top of your program. Variables that are created outside of `function setup()` and `function draw()` are called global variables.
65+
{: .notice--success}
66+
67+
Let's practice using variables by remixing the [lollipop example](https://editor.p5js.org/xinemata/sketches/reojWdPOp). Create variables that would store all the numbers we used to draw the lollipop:
68+
69+
```js
70+
// Lollipop stick variables
71+
let stickX = 200;
72+
let stickY = 240;
73+
let stickWidth = 10;
74+
let stickHeight = 100;
75+
// Lollipop candy variables
76+
let candyX = 200;
77+
let candyY = 200;
78+
let candyWidth = 60;
79+
let candyHeight = 60;
80+
81+
function setup() {
82+
createCanvas(400, 400);
83+
noStroke();
84+
rectMode(CENTER);
85+
}
86+
87+
function draw() {
88+
background(220);
89+
90+
//Lollipop stick
91+
fill(255);
92+
rect(stickX, stickY, stickWidth, stickHeight);
93+
94+
//Lollipop candy
95+
fill(180, 29, 159);
96+
ellipse(candyX, candyY, candyWidth, candyHeight);
97+
}
98+
```
99+
100+
When you click the play button, you should see the exact same lollipop sketch appearing in front of you. But instead of seeing a bunch of random numbers on the screen, we now have a better idea of what each number was created for. Amazing!
101+
102+
**Tip:** When a variable name contains two words, there are two conventional ways to format the text. The first way, also known as camel case, capitalizes the second word, e.g. `candyWidth`. The second way, also known as snake case, uses an underscore symbol to connect the two words, e.g. `candy_x`.
103+
{: .notice--success}
104+
105+
##### **Using Math with Variables**
106+
107+
You can apply arithmetic operators such as +, -, \*, and / to variables! The following example shows how you can draw three circles with equal spacing in between:
108+
109+
```js
110+
let x = 200;
111+
let y = 200;
112+
let d = 80;
113+
114+
function setup() {
115+
createCanvas(400, 400);
116+
}
117+
118+
function draw() {
119+
background(220);
120+
ellipse(x - 100, y, d, d); // left
121+
ellipse(x, y, d, d); // middle
122+
ellipse(x + 100, y, d, d); // right
123+
}
124+
```
125+
126+
Revisiting the [multiple rect() example](https://editor.p5js.org/xinemata/sketches/gWHc2OalC), where we were supposed to draw a row of rectangles diagonally across the screen, we could apply arithmetic to achieve the effect.
127+
128+
```js
129+
let offset = 40;
130+
let boxD = 40;
131+
132+
function setup() {
133+
createCanvas(400, 400); // w, h
134+
}
135+
136+
function draw() {
137+
background(220);
138+
rect(offset * 0, offset * 0, boxD, boxD); // x, y, w, h
139+
rect(offset * 1, offset * 1, boxD, boxD); // x, y, w, h
140+
rect(offset * 2, offset * 2, boxD, boxD); // x, y, w, h
141+
rect(offset * 3, offset * 3, boxD, boxD); // x, y, w, h
142+
rect(offset * 4, offset * 4, boxD, boxD); // x, y, w, h
143+
rect(offset * 5, offset * 5, boxD, boxD); // x, y, w, h
144+
rect(offset * 6, offset * 6, boxD, boxD); // x, y, w, h
145+
rect(offset * 7, offset * 7, boxD, boxD); // x, y, w, h
146+
rect(offset * 8, offset * 8, boxD, boxD); // x, y, w, h
147+
rect(offset * 9, offset * 9, boxD, boxD); // x, y, w, h
148+
}
149+
```
150+
151+
![Two diagonal lines]({{ "/assets/images/curriculum/Unit-2_Sample-8.png" | relative_url }})

_curriculum/02-05_Lessons.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
# This is the frontmatter
3+
title: "Lost and Found" # Title and Heading 1
4+
permalink: /lostAndFound-lessons/5 # Give your page a permalink
5+
published: true
6+
---
7+
8+
## Randomness
9+
10+
### Grade 6~8
11+
12+
#### Empty Variables
13+
14+
So far, we've been naming variables and storing data in them all in one go. But there's actually a way to create a variable, name it, and not store data inside until later. This type of variable is called an **empty variable**.
15+
16+
```js
17+
let d; // create an empty variable
18+
19+
function setup() {
20+
createCanvas(400, 400);
21+
}
22+
23+
function draw() {
24+
background(220);
25+
d = 100; // assign 100 to d
26+
ellipse(200, 200, d, d);
27+
console.log(d); // print d
28+
}
29+
```
30+
31+
Why would we need to wait to store data inside of an **empty variable**? One possible reason is because we're generating a random number, and that number won't get created until `function draw()` runs.
32+
33+
#### Creating a Random Number
34+
35+
[random()](https://p5js.org/reference/p5/random/) is a [p5.js](https://p5js.org/) feature that generates a random number between a minimum value and a maximum value:
36+
37+
```js
38+
let d; // create an empty variable
39+
40+
function setup() {
41+
createCanvas(400, 400);
42+
}
43+
44+
function draw() {
45+
background(220);
46+
d = random(80, 100); // generates a random number between 80 and 100
47+
ellipse(200, 200, d, d);
48+
console.log(d); // print d
49+
}
50+
```
51+
52+
Because `function draw()` runs 60 times per second, the code above would generate 60 new random numbers between 0 and 3 every second.
53+
54+
![Random circle]({{ "/assets/images/curriculum/Unit-2_Sample-9.gif" | relative_url }})
55+
56+
Let's keep playing with this idea. Below is an example of a circle that will generate a different circle size and a different stroke weight.
57+
58+
```js
59+
let circleSize;
60+
let strokeWidth;
61+
62+
function setup() {
63+
createCanvas(400, 300);
64+
background(0);
65+
circleSize = random(5, 250);
66+
strokeWidth = random(4, 28);
67+
}
68+
69+
function draw() {
70+
strokeWeight(strokeWidth);
71+
stroke(255, 109, 162);
72+
fill(247, 213, 17);
73+
ellipse(200, 150, circleSize);
74+
}
75+
```
76+
77+
![Pink and Yellow Circle]({{ "/assets/images/curriculum/pink_yellow_circle.png" | relative_url }})
78+
79+
What would happen if you put the `circleSize` and `strokeWidth` variables in the `draw()` function?

_curriculum/02-06_Lessons.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
# This is the frontmatter
3+
title: "Lost and Found" # Title and Heading 1
4+
permalink: /lostAndFound-lessons/6 # Give your page a permalink
5+
published: true
6+
---
7+
8+
#### System Variables
9+
10+
The [p5.js contributors](https://github.com/processing/p5.js?tab=readme-ov-file#contributors) have created a couple of variables that are available for us to load and use. These system variables were created to bring joy and ease to the coding process. We will spend the rest of Art + Code to try them out.
11+
12+
##### mouseX and mouseY
13+
14+
The `mouseX` and `mouseY` variables load the X and Y positions of your cursor in real time, allowing you to interact with your drawing with your mouse.
15+
16+
```js
17+
function draw() {
18+
background(220);
19+
ellipse(mouseX, mouseY, 20, 20); // x, y, w, h
20+
console.log(mouseX, mouseY); // displays mouseX and mouseY values in real time
21+
}
22+
```
23+
24+
Aside from using `mouseX` and `mouseY` to control the X and Y positions of an ellipse, we can also use those real time inputs to control the size of the ellipse:
25+
26+
```js
27+
function draw() {
28+
background(220);
29+
ellipse(200, 200, mouseX, mouseY); // x, y, w, h
30+
console.log(mouseX, mouseY); // displays mouseX and mouseY values in real time
31+
}
32+
```
33+
34+
Or the color of the ellipse:
35+
36+
```js
37+
function draw() {
38+
background(220);
39+
fill(mouseX, 100, 100); // r, g, b
40+
ellipse(mouseX, mouseY, 20, 20); // x, y, w, h
41+
}
42+
```
43+
44+
**Tip:** Similar to animation or a flip book, the modern browser has a framerate and refreshes 60 times per second! This illusion of real time can be interrupted when your browser slows down or crashes.
45+
{: .notice--success}
46+
47+
##### width and height
48+
49+
`width` and `height` variables loads the values of your canvas width and height, making it incredibly convenient to draw a shape in relation to the canvas. For example, if you'd like to draw two diagonal [line()](https://p5js.org/reference/p5/line/) across the canvas:
50+
51+
```js
52+
function draw() {
53+
background(204);
54+
line(0, 0, width, height); // x1, y1, x2, y2
55+
line(width, 0, 0, height); // x1, y1, x2, y2
56+
}
57+
```
58+
59+
![Two diagonal lines]({{ "/assets/images/curriculum/Unit-2_Sample-6.png" | relative_url }})
60+
61+
Or if you'd like to lay out your shapes with equal spaces in between:
62+
63+
```js
64+
let dieR = 60;
65+
66+
function setup() {
67+
createCanvas(400, 400);
68+
noStroke();
69+
}
70+
71+
function draw() {
72+
background(227, 47, 2);
73+
fill(245, 237, 220);
74+
ellipse(width / 2, height / 2, dieR, dieR); // places the pip in the center
75+
ellipse(width / 4, height / 4, dieR, dieR); // places the pip in the upper left corner
76+
ellipse((width / 4) * 3, height / 4, dieR, dieR); // places the pip in the upper right corner
77+
ellipse(width / 4, (height / 4) * 3, dieR, dieR); // places the pip in the lower left corner
78+
ellipse((width / 4) * 3, (height / 4) * 3, dieR, dieR);
79+
// places the pip in the lower right corner
80+
}
81+
```
82+
83+
![A red die]({{ "/assets/images/curriculum/Unit-2_Sample-7.png" | relative_url }})
84+
85+
#### Mouse Interaction
86+
87+
This section introduces [mousePressed()](https://p5js.org/reference/p5/mousePressed/). With this feature, you can use the mouse button to generate a new random number everytime the mouse is pressed:
88+
89+
```js
90+
let d;
91+
92+
function setup() {
93+
createCanvas(400, 400);
94+
d = random(80, 100); // randomly pick a number between 80 and 100
95+
}
96+
97+
function draw() {
98+
background(220);
99+
ellipse(200, 200, d, d);
100+
}
101+
102+
function mousePressed() {
103+
d = random(80, 100); // randomly pick a number between 80 and 100
104+
console.log(d); // print d
105+
}
106+
```
107+
108+
![Random circle]({{ "/assets/images/curriculum/Unit-2_Sample-10.gif" | relative_url }})
109+
110+
Let's get more practice by applying the same idea to the [lollipop example](https://editor.p5js.org/xinemata/sketches/reojWdPOp). Update the sketch so that everytime mouse is pressed, the candy will get assigned a new random color.
111+
112+
```js
113+
function draw() {
114+
background(220);
115+
116+
//Lollipop stick
117+
fill(255);
118+
rect(stickX, stickY, stickWidth, stickHeight);
119+
120+
//Lollipop candy
121+
fill(candyR, candyG, candyB);
122+
ellipse(candyX, candyY, candyWidth, candyHeight);
123+
}
124+
125+
function mousePressed() {
126+
candyR = random(0, 255); // randomly pick a number between 0 and 255
127+
candyG = random(0, 255); // randomly pick a number between 0 and 255
128+
candyB = random(0, 255); // randomly pick a number between 0 and 255
129+
console.log(candyR, candyG, candyB); // print candyR, candyG, candyB
130+
}
131+
```
132+
133+
![Random circle]({{ "/assets/images/curriculum/Unit-2_Sample-11.gif" | relative_url }})
134+
135+
**Code Snippet:** [random lollipop example](https://editor.p5js.org/xinemata/sketches/IG9-aEdGT).
136+
{: .notice--info}
137+
138+
**Tip:** Code inside of `function mousePressed()` runs once after the mouse button gets pressed.
139+
{: .notice--success}
140+
141+
#### Keyboard Interaction
142+
143+
[keyPressed()](https://p5js.org/reference/p5/keyPressed/) works in a very similar way. With this feature, you can run a block of code after any key on the keyboard gets pressed.
144+
145+
```js
146+
let d;
147+
148+
function setup() {
149+
createCanvas(400, 400);
150+
d = random(80, 100); // randomly pick a number between 80 and 100
151+
}
152+
153+
function draw() {
154+
background(220);
155+
ellipse(200, 200, d, d);
156+
}
157+
158+
function keyPressed() {
159+
d = random(80, 100); // randomly pick a number between 80 and 100
160+
console.log(d); // print d
161+
}
162+
```
163+
164+
Now that you've learned the basics of mouse and keyboard interactions, incorporate this feature into your next sketch to make it more interactive.

0 commit comments

Comments
 (0)