Skip to content

Commit 9a5d3a1

Browse files
committed
Random and mouse / keyboard interaction init
1 parent a961abc commit 9a5d3a1

27 files changed

Lines changed: 379 additions & 447 deletions

.DS_Store

0 Bytes
Binary file not shown.

_curriculum/.DS_Store

2 KB
Binary file not shown.

_curriculum/01-01_Lessons.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
title: "Code as Creative Medium" # Title and Heading 1
44
permalink: /codeAsCreativeMedium-lessons/ # Give your page a permalink
55
published: true
6-
7-
gallery: # Below is for including an image gallery
86
---
97

108
## Setting Up Your Environment

_curriculum/02-01_Intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,5 @@ This project will take approximately three to four 45-minute sessions:
137137
### Planning your Lost & Found Project
138138

139139
1. Students exchange written descriptions of their objects with one another.
140-
1. Then, each partner sketch out the lost object on paper.
140+
1. Then, each partner draw the object on paper by using ellipses and rectangles only.
141141
1. After 5 minutes of drawing, students take turns showing their drawing to the other for feedback. Feedback should be minimal and should not add too many complicated elements to the original drawing.

_curriculum/02-01_Lessons.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33
title: "Lost and Found" # Title and Heading 1
44
permalink: /lostAndFound-lessons/1 # Give your page a permalink
55
published: true
6-
7-
gallery: # Below is for including an image gallery
8-
- url: /assets/images/curriculum/Unit-1_Sample-2.gif
9-
image_path: /assets/images/curriculum/Unit-1_Sample-2.gif
10-
alt: "black circle following a mouse"
11-
title: "Digital Drawing Tool Step 1"
12-
- url: /assets/images/curriculum/Unit-1_Sample-3.gif
13-
image_path: /assets/images/curriculum/Unit-1_Sample-3.gif
14-
alt: "cursor drawing a black line made up of a series of translucent circles"
15-
title: "Digital Drawing Tool Step 2"
16-
- url: /assets/images/curriculum/Unit-1_Sample-4.gif
17-
image_path: /assets/images/curriculum/Unit-1_Sample-4.gif
18-
alt: "cursor drawing multiple black arches made up of translucent circles"
19-
title: "Digital Drawing Tool Step 3"
20-
- url: /assets/images/curriculum/Unit-1_Sample-5.gif
21-
image_path: /assets/images/curriculum/Unit-1_Sample-5.gif
22-
alt: "cursor creating a drawing of vertical blue lines and stamping red ellipses"
23-
title: "Digital Drawing Tool Step 4"
246
---
257

268
## Shape

_curriculum/02-02_Lessons.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33
title: "Lost and Found" # Title and Heading 1
44
permalink: /lostAndFound-lessons/2 # Give your page a permalink
55
published: true
6-
7-
gallery: # Below is for including an image gallery
8-
- url: /assets/images/curriculum/Unit-1_Sample-2.gif
9-
image_path: /assets/images/curriculum/Unit-1_Sample-2.gif
10-
alt: "black circle following a mouse"
11-
title: "Digital Drawing Tool Step 1"
12-
- url: /assets/images/curriculum/Unit-1_Sample-3.gif
13-
image_path: /assets/images/curriculum/Unit-1_Sample-3.gif
14-
alt: "cursor drawing a black line made up of a series of translucent circles"
15-
title: "Digital Drawing Tool Step 2"
16-
- url: /assets/images/curriculum/Unit-1_Sample-4.gif
17-
image_path: /assets/images/curriculum/Unit-1_Sample-4.gif
18-
alt: "cursor drawing multiple black arches made up of translucent circles"
19-
title: "Digital Drawing Tool Step 3"
20-
- url: /assets/images/curriculum/Unit-1_Sample-5.gif
21-
image_path: /assets/images/curriculum/Unit-1_Sample-5.gif
22-
alt: "cursor creating a drawing of vertical blue lines and stamping red ellipses"
23-
title: "Digital Drawing Tool Step 4"
246
---
257

268
## Color

_curriculum/02-03_Lessons.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33
title: "Lost and Found" # Title and Heading 1
44
permalink: /lostAndFound-lessons/3 # Give your page a permalink
55
published: true
6-
7-
gallery: # Below is for including an image gallery
8-
- url: /assets/images/curriculum/Unit-1_Sample-2.gif
9-
image_path: /assets/images/curriculum/Unit-1_Sample-2.gif
10-
alt: "black circle following a mouse"
11-
title: "Digital Drawing Tool Step 1"
12-
- url: /assets/images/curriculum/Unit-1_Sample-3.gif
13-
image_path: /assets/images/curriculum/Unit-1_Sample-3.gif
14-
alt: "cursor drawing a black line made up of a series of translucent circles"
15-
title: "Digital Drawing Tool Step 2"
16-
- url: /assets/images/curriculum/Unit-1_Sample-4.gif
17-
image_path: /assets/images/curriculum/Unit-1_Sample-4.gif
18-
alt: "cursor drawing multiple black arches made up of translucent circles"
19-
title: "Digital Drawing Tool Step 3"
20-
- url: /assets/images/curriculum/Unit-1_Sample-5.gif
21-
image_path: /assets/images/curriculum/Unit-1_Sample-5.gif
22-
alt: "cursor creating a drawing of vertical blue lines and stamping red ellipses"
23-
title: "Digital Drawing Tool Step 4"
246
---
257

268
## Composition

_curriculum/04-01-Lessons.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
---
2+
# This is the frontmatter
3+
title: "Mask Generator" # Title and Heading 1
4+
permalink: /maskGenerator-lessons/1 # Give your page a permalink
5+
published: true
6+
---
7+
## Variables
8+
9+
### Grade 6~8
10+
11+
#### What is a Variable?
12+
13+
A variable is a container used to store data. Picture a box that lets you store data.
14+
15+
But what do we mean by data? These data can be numbers:
16+
17+
![Variable box that stores numbers]({{ "/assets/images/curriculum/Unit-2_Sample-3.png" | relative_url }})
18+
19+
Or words:
20+
21+
![Variable box that stores numbers]({{ "/assets/images/curriculum/Unit-2_Sample-4.png" | relative_url }})
22+
23+
Or your favorite images and songs:
24+
25+
![Variable box that stores numbers]({{ "/assets/images/curriculum/Unit-2_Sample-5.png" | relative_url }})
26+
27+
We store data in these boxes so that they can be used as resources while a computer program is running.
28+
29+
#### Creating a Variable
30+
31+
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.
32+
33+
![Variable box labeled tomatoes]({{ "/assets/images/curriculum/Unit-2_Sample-1.png" | relative_url }})
34+
35+
If we want to indicate that there are 3 items inside the `tomatoes` box, we would write it out like this:
36+
37+
```js
38+
let tomatoes = 3; // assign 3 to the tomatoes variable
39+
40+
function setup() {
41+
createCanvas(400, 400);
42+
console.log(tomatoes); // load tomatoes variable
43+
}
44+
```
45+
46+
**Tip:** Conventionally, the first letter of a variable name is written in lower-case.
47+
{: .notice--success}
48+
49+
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.
50+
51+
> **🧠Teacher Trick:**
52+
> Think of yourself as creating a new world, declaring aloud **let** there be light!
53+
> By declaring with “let”, we’re proclaiming something (a variable) into existence in our world of code.
54+
55+
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.
56+
57+
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.
58+
59+
**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.
60+
{: .notice--success}
61+
62+
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:
63+
64+
```js
65+
// Lollipop stick variables
66+
let stickX = 200;
67+
let stickY = 240;
68+
let stickWidth = 10;
69+
let stickHeight = 100;
70+
// Lollipop candy variables
71+
let candyX = 200;
72+
let candyY = 200;
73+
let candyWidth = 60;
74+
let candyHeight = 60;
75+
76+
function setup() {
77+
createCanvas(400, 400);
78+
noStroke();
79+
rectMode(CENTER);
80+
}
81+
82+
function draw() {
83+
background(220);
84+
85+
//Lollipop stick
86+
fill(255);
87+
rect(stickX, stickY, stickWidth, stickHeight);
88+
89+
//Lollipop candy
90+
fill(180, 29, 159);
91+
ellipse(candyX, candyY, candyWidth, candyHeight);
92+
}
93+
```
94+
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!
95+
96+
**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`.
97+
{: .notice--success}
98+
99+
##### Applying Arithmetic to Variables
100+
101+
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:
102+
103+
```js
104+
let x = 200;
105+
let y = 200;
106+
let d = 80;
107+
108+
function setup() {
109+
createCanvas(400, 400);
110+
}
111+
112+
function draw() {
113+
background(220);
114+
ellipse(x - 100, y, d, d); // left
115+
ellipse(x, y, d, d); // middle
116+
ellipse(x + 100, y, d, d); // right
117+
}
118+
```
119+
120+
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.
121+
122+
123+
```js
124+
let offset = 40;
125+
let boxD = 40;
126+
127+
function setup() {
128+
createCanvas(400, 400); // w, h
129+
}
130+
131+
function draw() {
132+
background(220);
133+
rect(offset*0, offset*0, boxD, boxD); // x, y, w, h
134+
rect(offset*1, offset*1, boxD, boxD); // x, y, w, h
135+
rect(offset*2, offset*2, boxD, boxD); // x, y, w, h
136+
rect(offset*3, offset*3, boxD, boxD); // x, y, w, h
137+
rect(offset*4, offset*4, boxD, boxD); // x, y, w, h
138+
rect(offset*5, offset*5, boxD, boxD); // x, y, w, h
139+
rect(offset*6, offset*6, boxD, boxD); // x, y, w, h
140+
rect(offset*7, offset*7, boxD, boxD); // x, y, w, h
141+
rect(offset*8, offset*8, boxD, boxD); // x, y, w, h
142+
rect(offset*9, offset*9, boxD, boxD); // x, y, w, h
143+
}
144+
```
145+
146+
![Two diagonal lines]({{ "/assets/images/curriculum/Unit-2_Sample-8.png" | relative_url }})
147+
148+
#### System Variables
149+
150+
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.
151+
152+
##### mouseX and mouseY
153+
154+
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.
155+
156+
```js
157+
function draw() {
158+
background(220);
159+
ellipse(mouseX, mouseY, 20, 20); // x, y, w, h
160+
console.log(mouseX, mouseY); // displays mouseX and mouseY values in real time
161+
}
162+
```
163+
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:
164+
165+
```js
166+
function draw() {
167+
background(220);
168+
ellipse(200, 200, mouseX, mouseY); // x, y, w, h
169+
console.log(mouseX, mouseY); // displays mouseX and mouseY values in real time
170+
}
171+
```
172+
Or the color of the ellipse:
173+
174+
```js
175+
function draw() {
176+
background(220);
177+
fill(mouseX, 100, 100); // r, g, b
178+
ellipse(mouseX, mouseY, 20, 20); // x, y, w, h
179+
}
180+
```
181+
182+
**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.
183+
{: .notice--success}
184+
185+
##### width and height
186+
187+
`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:
188+
189+
```js
190+
function draw() {
191+
background(204);
192+
line(0, 0, width, height); // x1, y1, x2, y2
193+
line(width, 0, 0, height); // x1, y1, x2, y2
194+
}
195+
```
196+
197+
![Two diagonal lines]({{ "/assets/images/curriculum/Unit-2_Sample-6.png" | relative_url }})
198+
199+
Or if you'd like to lay out your shapes with equal spaces in between:
200+
201+
```js
202+
let dieR = 60;
203+
204+
function setup() {
205+
createCanvas(400, 400);
206+
noStroke();
207+
}
208+
209+
function draw() {
210+
background(227, 47, 2);
211+
fill(245, 237, 220);
212+
ellipse(width / 2, height / 2, dieR, dieR); // places the pip in the center
213+
ellipse(width / 4, height / 4, dieR, dieR); // places the pip in the upper left corner
214+
ellipse((width / 4) * 3, height / 4, dieR, dieR); // places the pip in the upper right corner
215+
ellipse(width / 4, (height / 4) * 3, dieR, dieR); // places the pip in the lower left corner
216+
ellipse((width / 4) * 3, (height / 4) * 3, dieR, dieR);
217+
// places the pip in the lower right corner
218+
}
219+
```
220+
![A red die]({{ "/assets/images/curriculum/Unit-2_Sample-7.png" | relative_url }})

_curriculum/04-02-Lessons.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
# This is the frontmatter
3+
title: "Mask Generator" # Title and Heading 1
4+
permalink: /maskGenerator-lessons/2 # Give your page a permalink
5+
published: true
6+
---
7+
## Randomness
8+
9+
### Grade 6~8
10+
11+
#### Empty Variables
12+
13+
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**.
14+
15+
```js
16+
let d; // create an empty variable
17+
18+
function setup() {
19+
createCanvas(400, 400);
20+
}
21+
22+
function draw() {
23+
background(220);
24+
d = 100; // assign 100 to d
25+
ellipse(200, 200, d, d);
26+
console.log(d); // print d
27+
}
28+
```
29+
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.
30+
31+
#### Creating a Random Number
32+
33+
[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:
34+
35+
```js
36+
let d; // create an empty variable
37+
38+
function setup() {
39+
createCanvas(400, 400);
40+
}
41+
42+
function draw() {
43+
background(220);
44+
d = random(80, 100); // generates a random number between 80 and 100
45+
ellipse(200, 200, d, d);
46+
console.log(d); // print d
47+
}
48+
```
49+
50+
Because `function draw()` runs 60 times per second, the code above would generate 60 new random numbers between 0 and 3 every second.
51+
52+
![Random circle]({{ "/assets/images/curriculum/Unit-2_Sample-9.gif" | relative_url }})
53+
54+
What if we can modify this sketch, so that only when you press your mouse button, a new random number gets generated?

0 commit comments

Comments
 (0)