-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (128 loc) · 3.02 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// TO DO
// Remove rain drops and splats proportionally to the frame rate
class Particle {
constructor(x, y, vx, vy) {
this.pos = createVector(x, y);
this.vel = createVector(vx, vy);
this.acc = createVector(0, -0.1);
this.radius = 2;
}
draw() {
noStroke();
fill(255);
ellipse(this.pos.x, height - this.pos.y, this.radius);
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
}
}
class RainSplat {
constructor(rainDrop) {
this.splat = [];
const total = 12;//rainDrop.length / 3;
for (let i = 0; i < total; i++) {
const x = rainDrop.pos.x;
const y = 0;//rainDrop.pos.y;
const vx = random(-2, 2);
const vy = random(0, -1 * rainDrop.vel.y/5);
this.splat.push(new Particle(x, y, vx, vy));
}
}
draw() {
this.splat.forEach((particle) => {
particle.draw();
});
}
update() {
this.splat.forEach((particle) => {
particle.update();
});
}
isVisible() {
for (let i = 0; i < this.splat.length; i++) {
let particle = this.splat[i];
if (particle.pos.y + particle.radius > 0) return true;
}
return false;
}
}
class RainDrop {
constructor() {
this.reset();
this.acc = createVector(0, -0.1);
this.length = 30;
this.width = 3;
}
draw() {
noStroke();
fill(255);
rect(this.pos.x, height - this.pos.y, this.width, this.length);
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
}
hasCollided() {
return this.pos.y - this.length <= 0
}
reset() {
this.pos = createVector(random(width), random(height + 100, height + 1000));
this.vel = createVector(0, random(-8, -3));
}
}
class Rain {
constructor(totalDrops) {
this.rainDrops = [];
for (let i = 0; i < totalDrops; i++) {
this.rainDrops.push(new RainDrop());
}
this.rainSplats = [];
}
draw() {
this.rainDrops.forEach((rainDrop) => {
rainDrop.draw();
});
this.rainSplats.forEach((rainSplat) => {
rainSplat.draw();
});
}
update() {
this.rainDrops.forEach((rainDrop) => {
rainDrop.update();
});
this.rainSplats.forEach((rainSplat) => {
rainSplat.update();
});
}
resolveCollisions() {
this.rainDrops.forEach((rainDrop) => {
if (rainDrop.hasCollided()) {
this.rainSplats.push(new RainSplat(rainDrop));
rainDrop.reset();
}
});
}
cullSplats() {
for (let i = this.rainSplats.length - 1; i >= 0; i--) {
let rainSplat = this.rainSplats[i];
if (rainSplat.isVisible()) continue;
this.rainSplats.splice(i, 1);
}
}
}
let rain;
function setup() {
createCanvas(windowWidth, windowHeight);
rain = new Rain(80);
}
function draw() {
background(40);
rain.draw();
rain.update();
rain.resolveCollisions();
rain.cullSplats();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}