-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravballs.html
More file actions
261 lines (223 loc) · 6.62 KB
/
gravballs.html
File metadata and controls
261 lines (223 loc) · 6.62 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Swirling Balls</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
#keys{
position: absolute;
bottom: 0;
right:0;
padding: 5px 20px;
background-color: white;
color: black;
font-family: "Courier New", Courier, monospace;
font-size: 14px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="keys" style="display:none">
<p><b>F</b> fullscreen</p>
<p><b>Q</b> <b>W</b> Size</p>
<p><b>Z</b> <b>X</b> Num balls</p>
</div>
<script>
const keysEl = document.getElementById('keys');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var numBalls = 100; // Initial number of balls
var maxRadius = 50; // Maximum radius of a ball
var balls = [];
// Physics parameters
var G = 0.001; // Gravitational constant
var minDist = maxRadius * 4; // Minimum distance for repulsion
var minDistSquared = minDist * minDist; // Square of the minimum distance
var maxDist = maxRadius * 8; // Maximum distance for attraction
var maxDistSquared = maxDist * maxDist; // Square of the maximum distance
var maxVelocity = 5; // Maximum allowed velocity
var maxVelocitySquared = maxVelocity * maxVelocity; // Square of the maximum velocity
var edgeRepulsionFactor = 0.5; // Strength of the edge repulsion
var lineWidth = 2;
const white = 'hsl(0,0%,100%)';
const black = 'hsl(0,0%,0%)';
var foreColor = white;
var backColor = black;
function Ball(x, y, radius, dx, dy) {
this.x = x;
this.y = y;
this.radius = radius;
this.linewidth = maxRadius * 0.1;//radius * 0.16667;
this.dx = dx;
this.dy = dy;
this.mass = Math.PI * radius * radius; // Mass proportional to area
//this.color = `hsl(${Math.random() * 360}, 100%, 50%)`; // Random color
this.color = foreColor;
this.circledraw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
};
this.draw = function() {
ctx.strokeStyle = this.color;
ctx.lineWidth = this.linewidth;
// Draw three concentric circles
for (let i = 1; i <= 3; i++) {
let r = this.radius * (i / 3); // Radius of the current circle
ctx.beginPath();
ctx.arc(this.x, this.y, r, 0, Math.PI * 2, false);
ctx.stroke();
ctx.closePath();
}
};
this.update = function() {
// Calculate gravitational forces from other balls
for (let i = 0; i < balls.length; i++) {
if (balls[i] === this) continue; // Skip self-interaction
let dx = balls[i].x - this.x;
let dy = balls[i].y - this.y;
let distanceSquared = dx * dx + dy * dy;
if (distanceSquared < maxDistSquared && distanceSquared > minDistSquared) {
// Attraction (inverse square law)
let distance = Math.sqrt(distanceSquared);
let force = G * this.mass * balls[i].mass / distanceSquared;
let angle = Math.atan2(dy, dx);
this.dx += force * Math.cos(angle) / this.mass;
this.dy += force * Math.sin(angle) / this.mass;
} else if (distanceSquared < minDistSquared) {
// Repulsion (inverse square law)
let distance = Math.sqrt(distanceSquared);
let force = -G * this.mass * balls[i].mass / distanceSquared;
let angle = Math.atan2(dy, dx);
this.dx += force * Math.cos(angle) / this.mass;
this.dy += force * Math.sin(angle) / this.mass;
}
}
// Limit velocity
let velocitySquared = this.dx * this.dx + this.dy * this.dy;
if (velocitySquared > maxVelocitySquared) {
let scale = maxVelocity / Math.sqrt(velocitySquared);
this.dx *= scale;
this.dy *= scale;
}
// Edge repulsion
let edgeDistanceX = 2 * this.radius;
let edgeDistanceY = 2 * this.radius;
if (this.x < edgeDistanceX) {
this.dx += edgeRepulsionFactor * (edgeDistanceX - this.x) / edgeDistanceX;
} else if (this.x > canvas.width - edgeDistanceX) {
this.dx -= edgeRepulsionFactor * (this.x - (canvas.width - edgeDistanceX)) / edgeDistanceX;
}
if (this.y < edgeDistanceY) {
this.dy += edgeRepulsionFactor * (edgeDistanceY - this.y) / edgeDistanceY;
} else if (this.y > canvas.height - edgeDistanceY) {
this.dy -= edgeRepulsionFactor * (this.y - (canvas.height - edgeDistanceY)) / edgeDistanceY;
}
// Update position based on velocity
this.x += this.dx;
this.y += this.dy;
this.draw();
};
}
function generateBalls() {
balls = []; // Clear existing balls
for (let i = 0; i < numBalls; i++) {
let radius = Math.random() * maxRadius + (maxRadius * 0.5); // Radius between 10 and maxRadius
let x = Math.random() * (canvas.width - radius * 2) + radius;
let y = Math.random() * (canvas.height - radius * 2) + radius;
let dx = (Math.random() - 0.5) * 2; // Small initial random velocity
let dy = (Math.random() - 0.5) * 2;
balls.push(new Ball(x, y, radius, dx, dy));
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = backColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < balls.length; i++) {
balls[i].update();
}
requestAnimationFrame(animate);
}
// Event Listeners
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
generateBalls(); // Regenerate balls on resize
});
document.addEventListener('keydown', (e) => {
if (e.key === 'z') {
lessBalls();
}
if (e.key === 'x') {
moreBalls();
}
if (e.key === 'q') {
smaller();
}
if (e.key === 'w') {
bigger();
}
if (e.key === "k") {
toggleKeys();
}
if (e.key === "f") {
toggleFullScreen();
}
});
function toggleKeys(){
keysEl.style.display = keysEl.style.display === "none" ? "block" : "none";
}
// Control Functions
function lessBalls() {
changeNumBalls(-1);
}
function moreBalls() {
changeNumBalls(1);
}
function changeNumBalls(change) {
numBalls += change;
if (numBalls < 1) {
numBalls = 1;
}
generateBalls();
}
function smaller() {
changeMaxSize(-5);
}
function bigger() {
changeMaxSize(5);
}
function changeMaxSize(change) {
maxRadius += change;
if (maxRadius < 10) {
maxRadius = 10;
}
generateBalls();
}
function toggleFullScreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
} else if (document.exitFullscreen) {
document.exitFullscreen();
}
}
// Initialize
generateBalls();
animate();
</script>
</body>
</html>