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
Copy file name to clipboardExpand all lines: _curriculum/02-03_Lessons.md
+141-4Lines changed: 141 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,11 +112,11 @@ While the leaves are symmetrical on the X-axis, they are asymmetrical on the Y-a
112
112
113
113
<iframewidth="560"height="315"src="https://www.youtube.com/embed/maTfm84mLbo?si=Orx71C5hl7owLq78"title="YouTube video player"frameborder="0"allow="accelerometer; autoplay; clipbo a ard-write; encrypted-media; gyroscope; picture-in-picture; web-share"referrerpolicy="strict-origin-when-cross-origin"allowfullscreen></iframe>
114
114
115
-
#### B. Translate
115
+
#### B. Grouped Drawing
116
116
117
117
As your p5.js journey unfold, you might find yourself wanting to move or resize a group of shapes all at the same time. This is achievable through features listed under the [Transform](https://p5js.org/reference/#transform) category.
118
118
119
-
To begin, let's draw a cloud using the [translate()](https://p5js.org/reference/p5/translate/) feature.
119
+
To begin, let's draw a cloud using the [translate()](https://p5js.org/reference/p5/translate/) feature:
Great work! Before moving on to draw another shape, make sure that you finish grouping your cloud drawing by sandwitching the code we've written so far in between [push()](https://p5js.org/reference/p5/push/) and [pop()](https://p5js.org/reference/p5/pop/). Doing this will make sure that any new shapes you draw won't get affected by the cloud's custom [translate()](https://p5js.org/reference/p5/translate/) values.
170
+
Great work! Before moving on to draw another shape, make sure that you sort your cloud drawing into a group using [push()](https://p5js.org/reference/p5/push/) and [pop()](https://p5js.org/reference/p5/pop/).
From this point on, you can draw a new shape, or copy and paste the cloud drawing into a new position on the canvas by manipulating the [translate()](https://p5js.org/reference/p5/translate/) values:
176
+
177
+
```js
178
+
functionsetup() {
179
+
createCanvas(400, 400);
180
+
rectMode(CENTER);
181
+
noStroke();
182
+
}
183
+
184
+
functiondraw() {
185
+
background(122, 122, 190);
186
+
fill(255);
187
+
188
+
// Cloud 1
189
+
190
+
// Begin tranformation
191
+
push();
192
+
193
+
// Shifts canvas origin to a new location
194
+
translate(60, 60); // x, y
195
+
196
+
// The rectangle refers to the new canvas origin to determine X, Y positions
197
+
rect(0, 0, 100, 50);
198
+
199
+
ellipse(-80, 0, 70, 70); // offset ellipse 80 pixel to the left
200
+
ellipse(80, 0, 70, 70); // offset ellipse 80 pixel to the right
201
+
202
+
ellipse(-40, -40, 70, 70); // offset ellipse 40 pixel to the left, and 40 pixel to the top
203
+
ellipse(40, 40, 70, 70); // offset ellipse 40 pixel to the right, and 40 pixel to the bottom
204
+
ellipse(-40, 40, 70, 70); // offset ellipse 40 pixel to the left, and 40 pixel to the bottom
205
+
ellipse(40, -40, 70, 70); // offset ellipse 40 pixel to the right, and 40 pixel to the top
206
+
207
+
ellipse(0, -50, 70, 70); // offset ellipse 50 pixel to the top
208
+
ellipse(0, 50, 70, 70); // offset ellipse 50 pixel to the bottom
209
+
210
+
// End transformation
211
+
pop();
212
+
213
+
// Cloud 2
214
+
215
+
// Begin tranformation
216
+
push();
217
+
218
+
// Shifts canvas origin to a new location
219
+
translate(200, 200); // x, y
220
+
221
+
// The rectangle refers to the new canvas origin to determine X, Y positions
222
+
rect(0, 0, 100, 50);
223
+
224
+
ellipse(-80, 0, 70, 70); // offset ellipse 80 pixel to the left
225
+
ellipse(80, 0, 70, 70); // offset ellipse 80 pixel to the right
226
+
227
+
ellipse(-40, -40, 70, 70); // offset ellipse 40 pixel to the left, and 40 pixel to the top
228
+
ellipse(40, 40, 70, 70); // offset ellipse 40 pixel to the right, and 40 pixel to the bottom
229
+
ellipse(-40, 40, 70, 70); // offset ellipse 40 pixel to the left, and 40 pixel to the bottom
230
+
ellipse(40, -40, 70, 70); // offset ellipse 40 pixel to the right, and 40 pixel to the top
231
+
232
+
ellipse(0, -50, 70, 70); // offset ellipse 50 pixel to the top
233
+
ellipse(0, 50, 70, 70); // offset ellipse 50 pixel to the bottom
234
+
235
+
// End transformation
236
+
pop();
237
+
238
+
// Cloud 3
239
+
240
+
// Begin tranformation
241
+
push();
242
+
243
+
// Shifts canvas origin to a new location
244
+
translate(350, 350); // x, y
245
+
246
+
// The rectangle refers to the new canvas origin to determine X, Y positions
247
+
rect(0, 0, 100, 50);
248
+
249
+
ellipse(-80, 0, 70, 70); // offset ellipse 80 pixel to the left
250
+
ellipse(80, 0, 70, 70); // offset ellipse 80 pixel to the right
251
+
252
+
ellipse(-40, -40, 70, 70); // offset ellipse 40 pixel to the left, and 40 pixel to the top
253
+
ellipse(40, 40, 70, 70); // offset ellipse 40 pixel to the right, and 40 pixel to the bottom
254
+
ellipse(-40, 40, 70, 70); // offset ellipse 40 pixel to the left, and 40 pixel to the bottom
255
+
ellipse(40, -40, 70, 70); // offset ellipse 40 pixel to the right, and 40 pixel to the top
256
+
257
+
ellipse(0, -50, 70, 70); // offset ellipse 50 pixel to the top
258
+
ellipse(0, 50, 70, 70); // offset ellipse 50 pixel to the bottom
Combining [translate()](https://p5js.org/reference/p5/translate/), [scale()](https://p5js.org/reference/p5/scale/), [push()](https://p5js.org/reference/p5/push/) and [pop()](https://p5js.org/reference/p5/pop/) opens up enormous potentials for creating illusion of depth.
304
+
305
+
Play around with the [multiple clouds example](https://editor.p5js.org/xinemata/sketches/Uox1dH1Mc) by resizing each cloud. Are you able to use [scale()](https://p5js.org/reference/p5/scale/) to make a cloud appear closer or further away?
0 commit comments