-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubbles.html
197 lines (155 loc) · 5.39 KB
/
bubbles.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bubbles</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/easings.js" charset="utf-8"></script>
</head>
<body style="background-color: #24282e;">
<canvas id="canvas" width="300" height="300"></canvas>
<script type="text/javascript">
var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
canvas.width = w;
canvas.height = h;
// Setup interactions
var maxRadiusMultiplier = 5;
var mouseRadius = 30;
var circleAmount = 80;
var minCircleSize = 5;
var maxCircleSize = 30;
var animationTimespan = 500;
////////////////////////////////////////////////////
function randomIntFromInterval(min, max) {
return Math.random() * (max - min + 1) + min;
// return Math.floor(Math.random()*(max-min+1)+min);
}
var mouse = {
x: undefined,
y: undefined,
r: mouseRadius
};
var colorArray = [
'#703030',
'#2F343B',
'#7E827A',
'#E3CDA4',
'#C77966'
];
var circleArray = [];
window.addEventListener('mousemove', function(event) {
mouse.x = event.x;
mouse.y = event.y;
});
window.addEventListener('mouseout', function(event) {
mouse.x = undefined;
mouse.y = undefined;
console.log("Mouse out of bounds.")
});
window.addEventListener('resize', function(event) {
w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
canvas.width = w;
canvas.height = h;
init();
});
function getEasing(startTime) {
var easeOverTime = animationTimespan;
var diff = new Date().getTime() - startTime;
diff = Math.min(easeOverTime, diff);
return diff / easeOverTime;
}
function Circle(x, y, dx, dy, radius) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.radius = radius;
this.originalRadius = radius;
this.maxRadius = radius * maxRadiusMultiplier;
this.color = colorArray[Math.floor(Math.random() * colorArray.length)];
// console.log(this);
this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
c.fillStyle = this.color;
c.fill();
}
this.update = function() {
if (this.x + this.radius > w || this.x - this.radius < 0) {
this.dx = -this.dx;
}
if (this.y + this.radius > h || this.y - this.radius < 0) {
this.dy = -this.dy;
}
this.x += this.dx;
this.y += this.dy;
// interaction growing
if (mouse.x - this.x < mouse.r && mouse.x - this.x > -mouse.r &&
mouse.y - this.y < mouse.r && mouse.y - this.y > -mouse.r) {
delete this.shrinkTime;
if (this.radius < this.maxRadius) {
if (this.growTime === undefined) {
this.growTime = new Date().getTime();
}
var diff = getEasing(this.growTime);
var easedDiff = EasingFunctions.easeOutQuad(diff);
// console.log("Growing: " + diff + " - " + easedDiff);
var newRadius = Math.min(this.maxRadius, this.maxRadius * easedDiff);
this.radius = (newRadius < this.originalRadius ? this.originalRadius : newRadius);
// this.radius = Math.min( this.maxRadius, this.maxRadius * easedDiff );
// console.log("Growing radius: " + this.radius);
} else {
this.radius = this.maxRadius;
delete this.growTime;
}
// interaction shrinking
} else {
delete this.growTime;
if (this.radius > this.originalRadius) {
if (this.shrinkTime === undefined) {
this.shrinkTime = new Date().getTime();
}
var diff = getEasing(this.shrinkTime);
var easedDiff = EasingFunctions.easeInQuad(diff);
// console.log("Shrinking: " + diff + " - " + easedDiff);
this.radius = Math.max(this.originalRadius, this.radius - easedDiff);
// console.log("Shrinking radius: " + this.radius);
} else {
this.radius = this.originalRadius;
delete this.shrinkTime;
}
}
this.draw();
}
}
function init() {
// Empty the array (resize event)
circleArray = [];
// Generate circles
for (var i = 0; i < circleAmount; i++) {
var radius = randomIntFromInterval(minCircleSize, maxCircleSize);
var x = randomIntFromInterval(radius * 2, w - radius * 2);
var dx = randomIntFromInterval(-1, 1);
var y = randomIntFromInterval(radius * 2, h - radius * 2);
var dy = randomIntFromInterval(-1, 1);
// .. and push them to an array
circleArray.push(new Circle(x, y, dx, dy, radius));
}
}
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, w, h);
for (var i = 0; i < circleArray.length; i++) {
circleArray[i].update();
}
}
init();
animate();
</script>
</body>
</html>