Translsis commited on
Commit
354e19e
·
verified ·
1 Parent(s): a55762e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +52 -3
index.html CHANGED
@@ -209,6 +209,52 @@
209
 
210
  // Tạo 8 ô mini với video ngẫu nhiên
211
  const miniBoxCount = 8;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  for (let i = 0; i < miniBoxCount; i++) {
213
  const miniBox = document.createElement('div');
214
  miniBox.className = 'mini-box';
@@ -224,11 +270,13 @@
224
 
225
  document.body.appendChild(miniBox);
226
 
227
- // Vị trí ngẫu nhiên
 
 
228
  const boxData = {
229
  element: miniBox,
230
- x: Math.random() * (window.innerWidth - 150) + 75,
231
- y: Math.random() * (window.innerHeight - 150) + 75,
232
  velX: (Math.random() - 0.5) * 5,
233
  velY: (Math.random() - 0.5) * 5,
234
  isDragging: false,
@@ -236,6 +284,7 @@
236
  dragOffsetY: 0
237
  };
238
 
 
239
  boxes.push(boxData);
240
 
241
  // Thêm event listener cho mini box
 
209
 
210
  // Tạo 8 ô mini với video ngẫu nhiên
211
  const miniBoxCount = 8;
212
+ const miniBoxSize = 120;
213
+ const spawnMargin = 100; // Khoảng cách tối thiểu giữa các ô
214
+
215
+ // Hàm kiểm tra va chạm giữa 2 ô
216
+ function isOverlapping(x1, y1, x2, y2, size1, size2) {
217
+ const distance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
218
+ const minDistance = (size1 + size2) / 2 + spawnMargin;
219
+ return distance < minDistance;
220
+ }
221
+
222
+ // Hàm tìm vị trí không bị chồng lên
223
+ function findNonOverlappingPosition(existingBoxes, maxAttempts = 100) {
224
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
225
+ const x = Math.random() * (window.innerWidth - 200) + 100;
226
+ const y = Math.random() * (window.innerHeight - 200) + 100;
227
+
228
+ let hasOverlap = false;
229
+
230
+ // Kiểm tra với box chính
231
+ const mainRect = mainBox.element.getBoundingClientRect();
232
+ if (isOverlapping(x, y, mainBox.x, mainBox.y, miniBoxSize, Math.max(mainRect.width, mainRect.height))) {
233
+ hasOverlap = true;
234
+ }
235
+
236
+ // Kiểm tra với các mini box đã tạo
237
+ for (let box of existingBoxes) {
238
+ if (isOverlapping(x, y, box.x, box.y, miniBoxSize, miniBoxSize)) {
239
+ hasOverlap = true;
240
+ break;
241
+ }
242
+ }
243
+
244
+ if (!hasOverlap) {
245
+ return { x, y };
246
+ }
247
+ }
248
+
249
+ // Nếu không tìm được vị trí sau nhiều lần thử, trả về vị trí ngẫu nhiên
250
+ return {
251
+ x: Math.random() * (window.innerWidth - 200) + 100,
252
+ y: Math.random() * (window.innerHeight - 200) + 100
253
+ };
254
+ }
255
+
256
+ const miniBoxes = [];
257
+
258
  for (let i = 0; i < miniBoxCount; i++) {
259
  const miniBox = document.createElement('div');
260
  miniBox.className = 'mini-box';
 
270
 
271
  document.body.appendChild(miniBox);
272
 
273
+ // Tìm vị trí không bị chồng lên
274
+ const position = findNonOverlappingPosition(miniBoxes);
275
+
276
  const boxData = {
277
  element: miniBox,
278
+ x: position.x,
279
+ y: position.y,
280
  velX: (Math.random() - 0.5) * 5,
281
  velY: (Math.random() - 0.5) * 5,
282
  isDragging: false,
 
284
  dragOffsetY: 0
285
  };
286
 
287
+ miniBoxes.push(boxData);
288
  boxes.push(boxData);
289
 
290
  // Thêm event listener cho mini box