-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
214 lines (203 loc) · 7.38 KB
/
index.html
File metadata and controls
214 lines (203 loc) · 7.38 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
body, html {
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
const canvas = window.canvas;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const padding = 10;
let gameover = false;
class CirclePacker {
constructor(ctx) {
this.ctx = ctx;
this.circles = [];
this.lastPointAdded = null;
this.gameover = false;
this.setSpawnInterval();
this.spawner = -1;
}
setSpawnInterval(intv) {
this.spawnInterval = intv || 21;
}
pack() {
// spawn circles
this.setupSpawner(this.spawnInterval);
// tick, tack...
function update() {
if (!this.gameover) {
requestAnimationFrame(update.bind(this));
packer.tick();
}
}
requestAnimationFrame(update.bind(this));
}
setupSpawner(interval) {
this.spawner = setInterval(() => {
if (!this.gameover) {
this.addCircle(new Circle(this.getRandomCoords()));
} else {
clearInterval(spawner);
}
}, interval);
}
addCircle(circle) {
this.circles.push(circle);
this.lastPointAdded = Date.now();
}
addCircleAt(coords) {
this.addCircle(new Circle(coords));
}
getRandomCoords() {
let x = Math.random() * this.ctx.canvas.width;
let y = Math.random() * this.ctx.canvas.height;
let c = 0; // to count the attempts
while(!this.areCoordsFree(x, y)) {
x = Math.random() * this.ctx.canvas.width;
y = Math.random() * this.ctx.canvas.height;
c++;
}
if (c > 2000) {
this.gameover = true;
console.info('took more than 2000 tries to find an empty spot for a new circle. Restarting...');
this.restart();
}
return new Point(x, y);
}
areCoordsFree(x, y) {
let res = true;
this.circles.forEach(circle => {
if (res === true && circle.distance(new Point(x, y)) < circle.radius + padding) {
res = false;
}
});
return res;
}
tick() {
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.circles.forEach(circle => {
this.drawCircle(circle);
if (circle.growing && this.mayCircleGrow(circle)) {
circle.grow();
} else {
circle.growing = false;
}
});
}
mayCircleGrow(circle) {
let can = true;
if (circle.origin.x - circle.radius < padding || circle.origin.y - circle.radius < padding ||
circle.origin.x + circle.radius > canvas.width - padding || circle.origin.y + circle.radius > canvas.height - padding) {
return false;
}
this.circles.forEach(other => {
if (can === true && circle !== other) {
if (circle.distance(other.origin) <= circle.radius + other.radius + 5) {
can = false;
}
}
});
return can;
}
drawCircle(circle) {
this.ctx.fillStyle = circle.color;
this.ctx.beginPath();
this.ctx.arc(circle.origin.x, circle.origin.y, circle.radius, 0, 2 * Math.PI);
this.ctx.fill();
}
reset() {
this.circles = [];
this.gameover = false;
clearInterval(this.spawner);
}
restart() {
this.reset();
this.pack();
}
clearAt(coords) {
let circles = this.findCirclesAt(coords);
if (circles.length > 0) {
this.removeCircle(circles[0]);
}
return circles.length > 0;
}
findCirclesAt(coords) {
return this.circles.filter(circle => circle.distance(coords) < circle.radius);
}
removeCircle(circle) {
this.circles = this.circles.filter(other => circle !== other);
}
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class Circle {
constructor(point, color) {
this.origin = point;
this.radius = 1;
this.color = color || this.getColor();
this.growing = true;
}
getColor() {
let r = Math.min(Math.floor(255 * (this.origin.x / canvas.width)) + 40, 255);
let g = Math.max(Math.floor(255 * (this.origin.x / canvas.width)) - 20, 16);
let b = Math.floor(255 * (this.origin.x / canvas.width));
return '#' + r.toString(16) + g.toString(16) + '44';
}
grow() {
this.radius++;
}
distance(point) {
return Math.sqrt(Math.pow(point.x - this.origin.x, 2) + Math.pow(point.y - this.origin.y, 2))
}
}
let packer = new CirclePacker(canvas.getContext('2d'));
packer.pack();
let dragging = false;
let removing = false;
canvas.addEventListener('mousedown', e => {
dragging = true;
let p = new Point(e.clientX, e.clientY);
if (!packer.clearAt(p)) {
packer.addCircleAt(p);
} else {
removing = true;
}
});
canvas.addEventListener('mousemove', e => {
if (dragging && removing) {
let p = new Point(e.clientX, e.clientY);
packer.clearAt(p);
}
});
canvas.addEventListener('mouseup', e => {
dragging = false;
removing = false;
});
console.info('click in whitespace to place a new bubble\nclick (and drag) a bubble to remove bubbles');
console.info('use reset(interval) to specify the time (in ms) before a new bubble appears (default is 21).\nusing reset() clears the entire canvas!');
window.reset = function(interval) {
packer.setSpawnInterval(interval);
packer.restart();
}
</script>
</body>
</html>