Spaces:
Running
Running
adnannovus
commited on
Update index.html
Browse files- index.html +185 -19
index.html
CHANGED
@@ -1,19 +1,185 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Particle Collider</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
margin: 0;
|
10 |
+
overflow: hidden;
|
11 |
+
background: #000;
|
12 |
+
display: flex;
|
13 |
+
justify-content: center;
|
14 |
+
align-items: center;
|
15 |
+
height: 100vh;
|
16 |
+
}
|
17 |
+
canvas {
|
18 |
+
border: 1px solid #333;
|
19 |
+
}
|
20 |
+
.controls {
|
21 |
+
position: fixed;
|
22 |
+
top: 10px;
|
23 |
+
left: 10px;
|
24 |
+
color: white;
|
25 |
+
}
|
26 |
+
button {
|
27 |
+
padding: 5px 10px;
|
28 |
+
margin: 5px;
|
29 |
+
cursor: pointer;
|
30 |
+
}
|
31 |
+
</style>
|
32 |
+
</head>
|
33 |
+
<body>
|
34 |
+
<div class="controls">
|
35 |
+
<button onclick="addParticles(10)">Add 10 Particles</button>
|
36 |
+
<button onclick="toggleGravity()">Toggle Gravity</button>
|
37 |
+
<button onclick="clearParticles()">Clear</button>
|
38 |
+
</div>
|
39 |
+
<canvas id="canvas"></canvas>
|
40 |
+
|
41 |
+
<script>
|
42 |
+
const canvas = document.getElementById('canvas');
|
43 |
+
const ctx = canvas.getContext('2d');
|
44 |
+
|
45 |
+
// Set canvas size
|
46 |
+
canvas.width = 800;
|
47 |
+
canvas.height = 600;
|
48 |
+
|
49 |
+
let particles = [];
|
50 |
+
let gravity = false;
|
51 |
+
|
52 |
+
class Particle {
|
53 |
+
constructor(x, y) {
|
54 |
+
this.x = x;
|
55 |
+
this.y = y;
|
56 |
+
this.radius = Math.random() * 10 + 5;
|
57 |
+
this.mass = this.radius;
|
58 |
+
this.vx = (Math.random() - 0.5) * 10;
|
59 |
+
this.vy = (Math.random() - 0.5) * 10;
|
60 |
+
this.color = `hsl(${Math.random() * 360}, 50%, 50%)`;
|
61 |
+
}
|
62 |
+
|
63 |
+
draw() {
|
64 |
+
ctx.beginPath();
|
65 |
+
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
|
66 |
+
ctx.fillStyle = this.color;
|
67 |
+
ctx.fill();
|
68 |
+
ctx.strokeStyle = 'rgba(255, 255, 255, 0.5)';
|
69 |
+
ctx.stroke();
|
70 |
+
}
|
71 |
+
|
72 |
+
update() {
|
73 |
+
if (gravity) {
|
74 |
+
this.vy += 0.2; // Gravity effect
|
75 |
+
}
|
76 |
+
|
77 |
+
this.x += this.vx;
|
78 |
+
this.y += this.vy;
|
79 |
+
|
80 |
+
// Bounce off walls
|
81 |
+
if (this.x - this.radius < 0 || this.x + this.radius > canvas.width) {
|
82 |
+
this.vx *= -0.9;
|
83 |
+
this.x = Math.max(this.radius, Math.min(canvas.width - this.radius, this.x));
|
84 |
+
}
|
85 |
+
if (this.y - this.radius < 0 || this.y + this.radius > canvas.height) {
|
86 |
+
this.vy *= -0.9;
|
87 |
+
this.y = Math.max(this.radius, Math.min(canvas.height - this.radius, this.y));
|
88 |
+
}
|
89 |
+
}
|
90 |
+
}
|
91 |
+
|
92 |
+
function checkCollision(p1, p2) {
|
93 |
+
const dx = p2.x - p1.x;
|
94 |
+
const dy = p2.y - p1.y;
|
95 |
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
96 |
+
|
97 |
+
if (distance < p1.radius + p2.radius) {
|
98 |
+
// Collision detected - Calculate collision response
|
99 |
+
const angle = Math.atan2(dy, dx);
|
100 |
+
const sin = Math.sin(angle);
|
101 |
+
const cos = Math.cos(angle);
|
102 |
+
|
103 |
+
// Rotate velocities
|
104 |
+
const vx1 = p1.vx * cos + p1.vy * sin;
|
105 |
+
const vy1 = p1.vy * cos - p1.vx * sin;
|
106 |
+
const vx2 = p2.vx * cos + p2.vy * sin;
|
107 |
+
const vy2 = p2.vy * cos - p2.vx * sin;
|
108 |
+
|
109 |
+
// Collision elastic equations
|
110 |
+
const m1 = p1.mass;
|
111 |
+
const m2 = p2.mass;
|
112 |
+
const u1 = vx1 * (m1 - m2) / (m1 + m2) + vx2 * 2 * m2 / (m1 + m2);
|
113 |
+
const u2 = vx2 * (m2 - m1) / (m1 + m2) + vx1 * 2 * m1 / (m1 + m2);
|
114 |
+
|
115 |
+
// Rotate velocities back
|
116 |
+
p1.vx = u1 * cos - vy1 * sin;
|
117 |
+
p1.vy = vy1 * cos + u1 * sin;
|
118 |
+
p2.vx = u2 * cos - vy2 * sin;
|
119 |
+
p2.vy = vy2 * cos + u2 * sin;
|
120 |
+
|
121 |
+
// Move particles apart to prevent sticking
|
122 |
+
const overlap = (p1.radius + p2.radius - distance) / 2;
|
123 |
+
p1.x -= overlap * cos;
|
124 |
+
p1.y -= overlap * sin;
|
125 |
+
p2.x += overlap * cos;
|
126 |
+
p2.y += overlap * sin;
|
127 |
+
}
|
128 |
+
}
|
129 |
+
|
130 |
+
function addParticles(count) {
|
131 |
+
for (let i = 0; i < count; i++) {
|
132 |
+
particles.push(new Particle(
|
133 |
+
Math.random() * canvas.width,
|
134 |
+
Math.random() * canvas.height
|
135 |
+
));
|
136 |
+
}
|
137 |
+
}
|
138 |
+
|
139 |
+
function toggleGravity() {
|
140 |
+
gravity = !gravity;
|
141 |
+
}
|
142 |
+
|
143 |
+
function clearParticles() {
|
144 |
+
particles = [];
|
145 |
+
}
|
146 |
+
|
147 |
+
function animate() {
|
148 |
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
|
149 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
150 |
+
|
151 |
+
// Update and draw particles
|
152 |
+
particles.forEach(particle => {
|
153 |
+
particle.update();
|
154 |
+
particle.draw();
|
155 |
+
});
|
156 |
+
|
157 |
+
// Check collisions
|
158 |
+
for (let i = 0; i < particles.length; i++) {
|
159 |
+
for (let j = i + 1; j < particles.length; j++) {
|
160 |
+
checkCollision(particles[i], particles[j]);
|
161 |
+
}
|
162 |
+
}
|
163 |
+
|
164 |
+
requestAnimationFrame(animate);
|
165 |
+
}
|
166 |
+
|
167 |
+
// Add initial particles and start animation
|
168 |
+
addParticles(20);
|
169 |
+
animate();
|
170 |
+
|
171 |
+
// Mouse interaction
|
172 |
+
let isMouseDown = false;
|
173 |
+
canvas.addEventListener('mousedown', () => isMouseDown = true);
|
174 |
+
canvas.addEventListener('mouseup', () => isMouseDown = false);
|
175 |
+
canvas.addEventListener('mousemove', (e) => {
|
176 |
+
if (isMouseDown) {
|
177 |
+
const rect = canvas.getBoundingClientRect();
|
178 |
+
const x = e.clientX - rect.left;
|
179 |
+
const y = e.clientY - rect.top;
|
180 |
+
particles.push(new Particle(x, y));
|
181 |
+
}
|
182 |
+
});
|
183 |
+
</script>
|
184 |
+
</body>
|
185 |
+
</html>
|