You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -66,20 +67,20 @@ <h2> Introduction to p5.js </h2>
66
67
<li><strong> draw() </strong> which is a function executed every frame (by default, about 60 times a second) </li>
67
68
</ul>
68
69
69
-
We create the canvas (arguments being the width and length) in <strong> setup() </strong> since we only need to do this once, and draw background (which you can call with RGB colour arguments as below) is called every frame in <strong> draw()</strong>!
70
+
We create the canvas (arguments being the width and length) in <strong> setup() </strong> since we only need to do this once, and <strong>background()</strong> (which you can call with RGB colour arguments as below) is called every frame in <strong> draw()</strong>!
70
71
71
72
<pre>
72
73
<codeclass="language-js">
73
74
function draw() {
74
-
redV = 128
75
-
greenV = 12
76
-
blueV = 128
75
+
let redV = 128; // if you're new to js, use "let" to declare variables
76
+
let greenV = 12;
77
+
let blueV = 128;
77
78
background(redV, greenV, blueV); // my favourite colour; purple!
78
79
}
79
80
</code>
80
81
</pre>
81
82
82
-
This difference matters when it comes to sketches with changing elements. See the difference in the following sketch, where we draw a circle at the mouse's position every frame.
83
+
This difference matters when it comes to sketches with changing elements; see the difference in the following sketches, where we draw a circle at the mouse's position every frame. One draws a background once, and the other draws a background every frame.
<emph> Background is called every frame and essentially clears the screen by drawing the background over everything in the previous frame. </emph>
126
+
127
+
128
+
<h3> Task: Multicolour </h3>
123
129
130
+
We can use the <strong>stroke()</strong> and <strong>fill()</strong> functions to change the line colour and fill colour of the circles (and any shape we draw) like below:
131
+
132
+
<pre>
133
+
<codeclass="language-js">
134
+
stroke(255, 0, 0); // outlines are now red!
135
+
fill(0, 255, 0); // areas are now green!
136
+
</code>
137
+
</pre>
138
+
139
+
<p><strong>Part 1:</strong> Modify the <emph>Circles 2</emph> sketch to have colours of your choice. </p>
140
+
141
+
<p><strong>Part 2:</strong> Modify the <emph>Circles 1</emph> sketch to have constantly changing colour like so: </p>
142
+
143
+
<divid="CirclesRainbowContainer"></div>
144
+
145
+
<strong><emph>Hint:</emph></strong> The range of colours is 0-255 (anything >255 will be interpreted as 255), so you will have to use modulus (<strong>%</strong>) or if statements to ensure RGB values don't go outside that range!
var variables are function scoped - if it's defined in a function, then it can be used in the function (even if it was defined many blocks deep)
13
+
Also hoisted, with undefined
14
+
*/
15
+
16
+
functiondoSomething(){
17
+
vara=8;
18
+
console.log("Inside function: "+a);// 8
19
+
}
20
+
21
+
doSomething();
22
+
23
+
console.log("After function: "+a);// 6 - note that this is *different* because vars are function scoped; the variable 'a' in the function is different from the global 'a'
24
+
25
+
functionchangeA(){
26
+
a=13;
27
+
console.log("Inside changing function: "+a);// 13
28
+
}
29
+
changeA();
30
+
31
+
console.log("Outside changing function: "+a);// 13 - expected since function changed the global var 'a'
32
+
33
+
/*
34
+
Okay so obviously this has issues because "var" allows you to redeclare a global variable
35
+
"let" is a variable that is block-scoped and ALSO not able to be redeclared
36
+
37
+
you CAN "redeclare" a let variable in another scope though -> they're treated as different variables, so there's no actual redeclaration
0 commit comments