-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
264 lines (222 loc) · 7.68 KB
/
index.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
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
262
263
264
<!DOCTYPE html>
<html>
<head>
<title>Experience with physics</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Madimi+One&display=swap"
rel="stylesheet"
/>
<style>
html,
body {
margin: 0;
width: 100%;
height: 100%;
background-color: #9e9e9e;
}
body,
button {
font-family: "Madimi One", sans-serif;
font-weight: 400;
font-style: normal;
}
canvas {
background-color: #fff;
border-radius: 10px;
}
.add-circle-button {
background-color: #3f51b5;
padding: 1rem 2rem;
text-transform: uppercase;
font-size: 1.25rem;
border-radius: 10px;
border: 0 none;
color: #fff;
cursor: pointer;
}
.add-circle-button:hover {
transform: scale(1.05);
}
.add-circle-button:active {
transform: scale(1);
}
.container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
height: 100vh;
}
.title {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div>
<h1 class="title">Experience with Physics</h1>
<canvas id="canvas" width="800" height="600"></canvas>
<div>
<span id="totalDisplay"></span>
<span>/</span>
<span id="fpsDisplay"></span>
</div>
</div>
<button class="add-circle-button" onclick="addCircle()">
Add Circles
</button>
</div>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const fpsDisplay = document.getElementById("fpsDisplay");
const totalDisplay = document.getElementById("totalDisplay");
const balls = [];
let lastFrameTime = performance.now();
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomColor() {
const letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function addCircle() {
const radius = getRandomNumber(5, 25);
const newBall = {
color: getRandomColor(),
x: getRandomNumber(20, canvas.width),
y: getRandomNumber(20, canvas.height),
radius,
velocityX: getRandomNumber(-5, 5),
velocityY: getRandomNumber(-5, 5),
gravity: 0.5 * (radius * 0.1),
isDragging: false,
dragStartX: 0,
dragStartY: 0,
};
balls.push(newBall);
}
function drawBall(ball) {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.fill();
ctx.closePath();
}
function checkCollision(ball1, ball2) {
const dx = ball1.x - ball2.x;
const dy = ball1.y - ball2.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < ball1.radius + ball2.radius) {
return true;
}
return false;
}
function update() {
let currentTime = performance.now();
const deltaTime = currentTime - lastFrameTime;
const fps = 1000 / deltaTime;
ctx.clearRect(0, 0, canvas.width, canvas.height);
balls.forEach(function (ball, index) {
drawBall(ball);
// Applies gravity to the ball's Y speed
if (!ball.isDragging) {
ball.velocityY += ball.gravity;
}
// Update ball position
ball.x += ball.velocityX;
ball.y += ball.velocityY;
// Checks if the ball hits the ground
if (ball.y + ball.radius >= canvas.height) {
// Inverts Y speed to simulate bounce
ball.velocityY *= -0.8;
// Fix ball position to prevent it from leaving the ground
ball.y = canvas.height - ball.radius;
}
// Checks if the ball has reached the side edges of the canvas
if (ball.x + ball.radius >= canvas.width) {
// // Inverts the X speed to simulate rebound
ball.velocityX *= -0.8;
// Correct the position of the ball to prevent it from leaving the canvas
ball.x = canvas.width - ball.radius;
} else if (ball.x - ball.radius <= 0) {
// Inverts the X speed to simulate rebound
ball.velocityX *= -0.8;
// Correct the position of the ball to prevent it from leaving the canvas
ball.x = ball.radius;
}
// Checks if the ball has reached the top of the canvas
if (ball.y - ball.radius <= 0) {
// Inverts the Y speed to simulate rebound
ball.velocityY *= -0.8;
// Correct the position of the ball to prevent it from leaving the canvas
ball.y = ball.radius;
}
// Check collision with other balls
balls.forEach(function (otherBall, otherIndex) {
if (index !== otherIndex) {
if (checkCollision(ball, otherBall)) {
// The balls collided, reverse speeds
const tempVelocityX = ball.velocityX;
const tempVelocityY = ball.velocityY;
ball.velocityX = otherBall.velocityX;
ball.velocityY = otherBall.velocityY;
otherBall.velocityX = tempVelocityX;
otherBall.velocityY = tempVelocityY;
}
}
});
canvas.addEventListener("mousedown", function (event) {
// Checks if the click occurred inside the ball
const distance = Math.sqrt(
Math.pow(event.offsetX - ball.x, 2) +
Math.pow(event.offsetY - ball.y, 2)
);
if (distance <= ball.radius + 50) {
// increase the click radius to 30 pixels
// Defines the ball as being dragged
ball.isDragging = true;
// Saves the initial mouse position when the ball is pressed
ball.dragStartX = event.offsetX;
ball.dragStartY = event.offsetY;
// If the ball is moving, stop it immediately
ball.velocityX = 0;
ball.velocityY = 0;
}
});
canvas.addEventListener("mousemove", function (event) {
if (ball.isDragging) {
// Update ball position with mouse position
ball.x = event.offsetX;
ball.y = event.offsetY;
}
});
canvas.addEventListener("mouseup", function (event) {
if (ball.isDragging) {
// Calculates the difference between the mouse position when the ball is released and when it is pressed
const diffX = event.offsetX - ball.dragStartX;
const diffY = event.offsetY - ball.dragStartY;
// Adjusts ball speed based on the difference
ball.velocityX = diffX / 50;
ball.velocityY = diffY / 50;
// Sets the ball as not being dragged
ball.isDragging = false;
}
});
});
lastFrameTime = currentTime;
fpsDisplay.innerText = `FPS: ${fps.toFixed(2)}`;
totalDisplay.innerText = `TOTAL: ${balls.length}`;
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>