Translsis commited on
Commit
708d048
·
verified ·
1 Parent(s): c4be80f

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +81 -216
index.html CHANGED
@@ -57,25 +57,6 @@
57
  .container:active {
58
  cursor: grabbing;
59
  }
60
-
61
- /* Ô mini */
62
- .mini-box {
63
- position: absolute;
64
- border: 3px solid #8B4513;
65
- padding: 15px;
66
- border-radius: 12px;
67
- background-color: rgba(255, 255, 255, 0.25);
68
- box-shadow: 0 0 15px rgba(0, 0, 0, 0.15);
69
- overflow: hidden;
70
- cursor: grab;
71
- user-select: none;
72
- width: 120px;
73
- height: 120px;
74
- }
75
-
76
- .mini-box:active {
77
- cursor: grabbing;
78
- }
79
 
80
  /* Video nền cho container */
81
  .video-background {
@@ -191,127 +172,34 @@
191
  let mouseY = window.innerHeight / 2;
192
  let spiderX = mouseX, spiderY = mouseY;
193
 
194
- // Mảng chứa tất cả các box (bao gồm cả box chính và mini boxes)
195
- const boxes = [];
196
-
197
- // Khởi tạo box chính
198
- const mainBox = {
199
- element: container,
200
- x: window.innerWidth / 2,
201
- y: window.innerHeight / 2,
202
- velX: 0,
203
- velY: 0,
204
- isDragging: false,
205
- dragOffsetX: 0,
206
- dragOffsetY: 0
207
- };
208
- boxes.push(mainBox);
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';
261
-
262
- // Random chọn video
263
- const videoSrc = Math.random() > 0.5 ? 'video.mp4' : 'Background.mp4';
264
-
265
- miniBox.innerHTML = `
266
- <video class="video-background" autoplay loop muted playsinline preload="auto" loading="eager">
267
- <source src="${videoSrc}" type="video/mp4">
268
- </video>
269
- `;
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,
283
- dragOffsetX: 0,
284
- dragOffsetY: 0
285
- };
286
-
287
- miniBoxes.push(boxData);
288
- boxes.push(boxData);
289
-
290
- // Thêm event listener cho mini box
291
- miniBox.addEventListener('mousedown', (e) => startDrag(e, boxData));
292
- miniBox.addEventListener('touchstart', (e) => startDrag(e, boxData));
293
- }
294
-
295
  const friction = 0.95;
296
  const elasticity = 0.7;
297
  const wallMargin = 50;
298
 
299
- // Khởi tạo vị trí ban đầu cho box chính
300
- mainBox.element.style.left = mainBox.x + 'px';
301
- mainBox.element.style.top = mainBox.y + 'px';
302
- mainBox.element.style.transform = 'translate(-50%, -50%)';
303
 
304
- // Xử lý kéo thả
305
- container.addEventListener('mousedown', (e) => startDrag(e, mainBox));
306
- container.addEventListener('touchstart', (e) => startDrag(e, mainBox));
307
 
308
- function startDrag(e, box) {
309
- box.isDragging = true;
310
  const touch = e.type === 'touchstart' ? e.touches[0] : e;
311
- box.dragOffsetX = touch.clientX - box.x;
312
- box.dragOffsetY = touch.clientY - box.y;
313
- box.velX = 0;
314
- box.velY = 0;
315
  e.preventDefault();
316
  }
317
 
@@ -319,83 +207,72 @@
319
  document.addEventListener('touchmove', drag);
320
 
321
  function drag(e) {
322
- boxes.forEach(box => {
323
- if (box.isDragging) {
324
- const touch = e.type === 'touchmove' ? e.touches[0] : e;
325
- const newX = touch.clientX - box.dragOffsetX;
326
- const newY = touch.clientY - box.dragOffsetY;
327
-
328
- box.velX = (newX - box.x) * 0.5;
329
- box.velY = (newY - box.y) * 0.5;
330
-
331
- box.x = newX;
332
- box.y = newY;
333
- }
334
- });
335
  }
336
 
337
  document.addEventListener('mouseup', stopDrag);
338
  document.addEventListener('touchend', stopDrag);
339
 
340
  function stopDrag() {
341
- boxes.forEach(box => {
342
- box.isDragging = false;
343
- });
344
  }
345
 
346
- // Cập nhật vật lý cho tất cả boxes
347
- function updateBoxPhysics() {
348
- boxes.forEach(box => {
349
- if (!box.isDragging) {
350
- // Áp dụng ma sát
351
- box.velX *= friction;
352
- box.velY *= friction;
353
-
354
- // Cập nhật vị trí
355
- box.x += box.velX;
356
- box.y += box.velY;
357
-
358
- // Kiểm tra va chạm với tường
359
- const rect = box.element.getBoundingClientRect();
360
- const halfWidth = rect.width / 2;
361
- const halfHeight = rect.height / 2;
362
-
363
- // Tường trái
364
- if (box.x - halfWidth < wallMargin) {
365
- box.x = wallMargin + halfWidth;
366
- box.velX = Math.abs(box.velX) * elasticity;
367
- }
368
- // Tường phải
369
- if (box.x + halfWidth > window.innerWidth - wallMargin) {
370
- box.x = window.innerWidth - wallMargin - halfWidth;
371
- box.velX = -Math.abs(box.velX) * elasticity;
372
- }
373
- // Tường trên
374
- if (box.y - halfHeight < wallMargin) {
375
- box.y = wallMargin + halfHeight;
376
- box.velY = Math.abs(box.velY) * elasticity;
377
- }
378
- // Tường dưới
379
- if (box.y + halfHeight > window.innerHeight - wallMargin) {
380
- box.y = window.innerHeight - wallMargin - halfHeight;
381
- box.velY = -Math.abs(box.velY) * elasticity;
382
- }
383
- }
384
 
385
- // Cập nhật vị trí DOM
386
- if (box.element === container) {
387
- box.element.style.left = box.x + 'px';
388
- box.element.style.top = box.y + 'px';
389
- } else {
390
- box.element.style.left = (box.x - 60) + 'px';
391
- box.element.style.top = (box.y - 60) + 'px';
 
 
 
 
 
 
 
 
 
 
 
 
392
  }
393
- });
 
 
 
 
394
 
395
- requestAnimationFrame(updateBoxPhysics);
396
  }
397
 
398
- updateBoxPhysics();
399
 
400
  // Nhện con (ở các góc màn hình)
401
  const babySpiders = [
@@ -447,12 +324,9 @@
447
  }
448
 
449
  document.addEventListener('mousemove', (e) => {
450
- mouseX = e.clientX;
451
- mouseY = e.clientY;
452
-
453
- // Chỉ tạo emoji khi không kéo box nào
454
- const anyDragging = boxes.some(box => box.isDragging);
455
- if (!anyDragging) {
456
  createEmoji(e.clientX, e.clientY);
457
  }
458
  });
@@ -477,19 +351,10 @@
477
  spiderY = touch.clientY;
478
  });
479
 
480
- // Bỏ qua block autoplay của trình duyệt - tối ưu âm thanh
481
- let musicStarted = false;
482
- function startMusic() {
483
- if (!musicStarted) {
484
- music.loop = true;
485
- music.play().catch(e => console.log('Music autoplay blocked:', e));
486
- musicStarted = true;
487
- }
488
- }
489
-
490
- document.addEventListener('click', startMusic, { once: true });
491
- document.addEventListener('touchstart', startMusic, { once: true });
492
- document.addEventListener('keydown', startMusic, { once: true });
493
 
494
  // Di chuyển nhện lớn
495
  function moveSpider() {
 
57
  .container:active {
58
  cursor: grabbing;
59
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  /* Video nền cho container */
62
  .video-background {
 
172
  let mouseY = window.innerHeight / 2;
173
  let spiderX = mouseX, spiderY = mouseY;
174
 
175
+ // Vật cho container
176
+ let containerX = window.innerWidth / 2;
177
+ let containerY = window.innerHeight / 2;
178
+ let containerVelX = 0;
179
+ let containerVelY = 0;
180
+ let isDragging = false;
181
+ let dragOffsetX = 0;
182
+ let dragOffsetY = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  const friction = 0.95;
184
  const elasticity = 0.7;
185
  const wallMargin = 50;
186
 
187
+ // Khởi tạo vị trí ban đầu
188
+ container.style.left = containerX + 'px';
189
+ container.style.top = containerY + 'px';
190
+ container.style.transform = 'translate(-50%, -50%)';
191
 
192
+ // Xử lý kéo thả container
193
+ container.addEventListener('mousedown', startDrag);
194
+ container.addEventListener('touchstart', startDrag);
195
 
196
+ function startDrag(e) {
197
+ isDragging = true;
198
  const touch = e.type === 'touchstart' ? e.touches[0] : e;
199
+ dragOffsetX = touch.clientX - containerX;
200
+ dragOffsetY = touch.clientY - containerY;
201
+ containerVelX = 0;
202
+ containerVelY = 0;
203
  e.preventDefault();
204
  }
205
 
 
207
  document.addEventListener('touchmove', drag);
208
 
209
  function drag(e) {
210
+ if (isDragging) {
211
+ const touch = e.type === 'touchmove' ? e.touches[0] : e;
212
+ const newX = touch.clientX - dragOffsetX;
213
+ const newY = touch.clientY - dragOffsetY;
214
+
215
+ containerVelX = (newX - containerX) * 0.5;
216
+ containerVelY = (newY - containerY) * 0.5;
217
+
218
+ containerX = newX;
219
+ containerY = newY;
220
+ }
 
 
221
  }
222
 
223
  document.addEventListener('mouseup', stopDrag);
224
  document.addEventListener('touchend', stopDrag);
225
 
226
  function stopDrag() {
227
+ isDragging = false;
 
 
228
  }
229
 
230
+ // Cập nhật vật lý container
231
+ function updateContainerPhysics() {
232
+ if (!isDragging) {
233
+ // Áp dụng ma sát
234
+ containerVelX *= friction;
235
+ containerVelY *= friction;
236
+
237
+ // Cập nhật vị trí
238
+ containerX += containerVelX;
239
+ containerY += containerVelY;
240
+
241
+ // Kiểm tra va chạm với tường
242
+ const rect = container.getBoundingClientRect();
243
+ const halfWidth = rect.width / 2;
244
+ const halfHeight = rect.height / 2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
 
246
+ // Tường trái
247
+ if (containerX - halfWidth < wallMargin) {
248
+ containerX = wallMargin + halfWidth;
249
+ containerVelX = Math.abs(containerVelX) * elasticity;
250
+ }
251
+ // Tường phải
252
+ if (containerX + halfWidth > window.innerWidth - wallMargin) {
253
+ containerX = window.innerWidth - wallMargin - halfWidth;
254
+ containerVelX = -Math.abs(containerVelX) * elasticity;
255
+ }
256
+ // Tường trên
257
+ if (containerY - halfHeight < wallMargin) {
258
+ containerY = wallMargin + halfHeight;
259
+ containerVelY = Math.abs(containerVelY) * elasticity;
260
+ }
261
+ // Tường dưới
262
+ if (containerY + halfHeight > window.innerHeight - wallMargin) {
263
+ containerY = window.innerHeight - wallMargin - halfHeight;
264
+ containerVelY = -Math.abs(containerVelY) * elasticity;
265
  }
266
+ }
267
+
268
+ // Cập nhật vị trí DOM
269
+ container.style.left = containerX + 'px';
270
+ container.style.top = containerY + 'px';
271
 
272
+ requestAnimationFrame(updateContainerPhysics);
273
  }
274
 
275
+ updateContainerPhysics();
276
 
277
  // Nhện con (ở các góc màn hình)
278
  const babySpiders = [
 
324
  }
325
 
326
  document.addEventListener('mousemove', (e) => {
327
+ if (!isDragging) {
328
+ mouseX = e.clientX;
329
+ mouseY = e.clientY;
 
 
 
330
  createEmoji(e.clientX, e.clientY);
331
  }
332
  });
 
351
  spiderY = touch.clientY;
352
  });
353
 
354
+ // Bỏ qua block autoplay của trình duyệt
355
+ document.addEventListener('click', () => {
356
+ music.play();
357
+ }, { once: true });
 
 
 
 
 
 
 
 
 
358
 
359
  // Di chuyển nhện lớn
360
  function moveSpider() {