-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbles.js
261 lines (206 loc) · 6.86 KB
/
bubbles.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var canvas = document.querySelector('canvas');
canvas.width = canvas.parentElement.clientWidth - 20;
canvas.height = canvas.parentElement.clientHeight - 20;
var ctx = canvas.getContext('2d');
var count = canvas.height;
var bubbles = [];
var bubbleCount = 30;
var bubbleSpeed = 1;
var popLines = 6;
var popDistance = 40;
var mouseOffset = {
x: 0,
y: 0
}
// --------------
// Animation Loop
// --------------
function animate() {
// ------------
// Clear Canvas
// ------------
ctx.clearRect(0, 0, canvas.width, canvas.height);
// ------------
// Draw Bubbles
// ------------
ctx.beginPath();
for (var i = 0; i < bubbles.length; i++) {
// first num = distance between waves
// second num = wave height
// third num = move the center of the wave away from the edge
bubbles[i].position.x = Math.sin(bubbles[i].count/bubbles[i].distanceBetweenWaves) * 50 + bubbles[i].xOff;
bubbles[i].position.y = bubbles[i].count;
bubbles[i].render();
if(bubbles[i].count < 0 - bubbles[i].radius) {
bubbles[i].count = canvas.height + bubbles[i].yOff;
} else {
bubbles[i].count -= bubbleSpeed;
}
}
// ---------------
// On Bubble Hover
// ---------------
for (var i = 0; i < bubbles.length; i++) {
if(mouseOffset.x > bubbles[i].position.x - bubbles[i].radius && mouseOffset.x < bubbles[i].position.x + bubbles[i].radius) {
if(mouseOffset.y > bubbles[i].position.y - bubbles[i].radius && mouseOffset.y < bubbles[i].position.y + bubbles[i].radius) {
for (var a = 0; a < bubbles[i].lines.length; a++) {
popDistance = bubbles[i].radius * 0.5;
bubbles[i].lines[a].popping = true;
bubbles[i].popping = true;
}
}
}
}
window.requestAnimationFrame(animate);
}
window.requestAnimationFrame(animate);
// ------------------
// Bubble Constructor
// ------------------
var createBubble = function() {
this.position = {x: 0, y: 0};
this.radius = 8 + Math.random() * 6;
this.xOff = Math.random() * canvas.width - this.radius;
this.yOff = Math.random() * canvas.height;
this.distanceBetweenWaves = 50 + Math.random() * 40;
this.count = canvas.height + this.yOff;
this.color = 'rgb(250,250,250)';
this.lines = [];
this.popping = false;
this.maxRotation = 85;
this.rotation = Math.floor(Math.random() * (this.maxRotation - (this.maxRotation * -1))) + (this.maxRotation * -1);
this.rotationDirection = 'forward';
// Populate Lines
for (var i = 0; i < popLines; i++) {
var tempLine = new createLine();
tempLine.bubble = this;
tempLine.index = i;
this.lines.push(tempLine);
}
this.resetPosition = function() {
this.position = {x: 0, y: 0};
this.radius = 8 + Math.random() * 6;
this.xOff = Math.random() * canvas.width - this.radius;
this.yOff = Math.random() * canvas.height;
this.distanceBetweenWaves = 50 + Math.random() * 40;
this.count = canvas.height + this.yOff;
this.popping = false;
}
// Render the circles
this.render = function() {
if(this.rotationDirection === 'forward') {
if(this.rotation < this.maxRotation) {
this.rotation++;
} else {
this.rotationDirection = 'backward';
}
} else {
if(this.rotation > this.maxRotation * -1) {
this.rotation--;
} else {
this.rotationDirection = 'forward';
}
}
ctx.save();
ctx.translate(this.position.x, this.position.y);
ctx.rotate(this.rotation*Math.PI/180);
if(!this.popping) {
ctx.beginPath();
ctx.strokeStyle = 'rgb(250,250,250)';
ctx.lineWidth = 1;
ctx.arc(0, 0, this.radius - 3, 0, Math.PI*1.5, true);
ctx.stroke();
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, Math.PI*2, false);
ctx.stroke();
}
ctx.restore();
// Draw the lines
for (var a = 0; a < this.lines.length; a++) {
if(this.lines[a].popping) {
if(this.lines[a].lineLength < popDistance && !this.lines[a].inversePop) {
this.lines[a].popDistance += 0.06;
} else {
if(this.lines[a].popDistance >= 0) {
this.lines[a].inversePop = true;
this.lines[a].popDistanceReturn += 1;
this.lines[a].popDistance -= 0.03;
} else {
this.lines[a].resetValues();
this.resetPosition();
}
}
this.lines[a].updateValues();
this.lines[a].render();
}
}
}
}
// ----------------
// Populate Bubbles
// ----------------
for (var i = 0; i < bubbleCount; i++) {
var tempBubble = new createBubble();
bubbles.push(tempBubble);
}
// ----------------
// Line Constructor
// ----------------
function createLine() {
this.lineLength = 0;
this.popDistance = 0;
this.popDistanceReturn = 0;
this.inversePop = false; // When the lines reach full length they need to shrink into the end position
this.popping = false;
this.resetValues = function() {
this.lineLength = 0;
this.popDistance = 0;
this.popDistanceReturn = 0;
this.inversePop = false;
this.popping = false;
this.updateValues();
}
this.updateValues = function() {
this.x = this.bubble.position.x + (this.bubble.radius + this.popDistanceReturn) * Math.cos(2 * Math.PI * this.index / this.bubble.lines.length);
this.y = this.bubble.position.y + (this.bubble.radius + this.popDistanceReturn) * Math.sin(2 * Math.PI * this.index / this.bubble.lines.length);
this.lineLength = this.bubble.radius * this.popDistance;
this.endX = this.lineLength;
this.endY = this.lineLength;
}
this.render = function() {
this.updateValues();
ctx.beginPath();
ctx.strokeStyle = 'rgb(250,250,250)';
ctx.lineWidth = 2;
ctx.moveTo(this.x, this.y);
if(this.x < this.bubble.position.x) {
this.endX = this.lineLength * -1;
}
if(this.y < this.bubble.position.y) {
this.endY = this.lineLength * -1;
}
if(this.y === this.bubble.position.y) {
this.endY = 0;
}
if(this.x === this.bubble.position.x) {
this.endX = 0;
}
ctx.lineTo(this.x + this.endX, this.y + this.endY);
ctx.stroke();
};
}
// ---------------
// Event Listeners
// ---------------
canvas.addEventListener('mousemove', mouseMove);
function mouseMove(e) {
mouseOffset.x = e.offsetX;
mouseOffset.y = e.offsetY;
}
window.addEventListener('resize', function() {
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight;
});
// ---------------
// Event Listeners
// ---------------