-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
67 lines (54 loc) · 1.49 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
let leaves = [];
function setup() {
createCanvas(windowWidth, windowHeight); // set canvas to fullscreen
createLeaves();
}
function draw() {
drawSky();
drawGrass();
drawLeaves();
}
// MOUSE INTERACTIONS
function mousePressed() {
removeClickedLeaf();
}
function removeClickedLeaf() {
// check each leaves position for mouse click
// if found, remove leaf and end for loop
// reference: https://www.youtube.com/watch?v=TaN5At5RWH8
for (let i = 0; i < leaves.length; i++) {
let mouseInsideLeaf = leaves[i].isInside(mouseX, mouseY);
if (mouseInsideLeaf) {
leaves.splice(i, 1);
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
break;
}
}
}
// HELPER FUNCTIONS
function createLeaves() {
// Fill leaves array
for (let i = 0; i < LEAF_COUNT; i++) {
// create leaf at random x position
let leaf = new Leaf(random(width), LEAF_SIZE, i);
leaves.push(leaf); // add to leaves array
}
}
function drawSky() {
background(0, 200, 255); // sky blue
}
function drawGrass() {
push(); // isolate styling to grass
fill(10, 80, 50); // grass color
let grassHeight = height * GRASS_HEIGHT; // calculate height of grass
rect(0, height - grassHeight, width, grassHeight);
pop();
}
function drawLeaves() {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
// update and display leaves
leaves.forEach((leaf) => {
leaf.update();
leaf.show();
});
}