Update index.html
Browse files- 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 |
-
//
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 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
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
|
| 304 |
-
// Xử lý kéo thả
|
| 305 |
-
container.addEventListener('mousedown',
|
| 306 |
-
container.addEventListener('touchstart',
|
| 307 |
|
| 308 |
-
function startDrag(e
|
| 309 |
-
|
| 310 |
const touch = e.type === 'touchstart' ? e.touches[0] : e;
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
e.preventDefault();
|
| 316 |
}
|
| 317 |
|
|
@@ -319,83 +207,72 @@
|
|
| 319 |
document.addEventListener('touchmove', drag);
|
| 320 |
|
| 321 |
function drag(e) {
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
}
|
| 334 |
-
});
|
| 335 |
}
|
| 336 |
|
| 337 |
document.addEventListener('mouseup', stopDrag);
|
| 338 |
document.addEventListener('touchend', stopDrag);
|
| 339 |
|
| 340 |
function stopDrag() {
|
| 341 |
-
|
| 342 |
-
box.isDragging = false;
|
| 343 |
-
});
|
| 344 |
}
|
| 345 |
|
| 346 |
-
// Cập nhật vật lý
|
| 347 |
-
function
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 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 |
-
//
|
| 386 |
-
if (
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
}
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
}
|
| 393 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
-
requestAnimationFrame(
|
| 396 |
}
|
| 397 |
|
| 398 |
-
|
| 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 |
-
|
| 451 |
-
|
| 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
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 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 lý 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() {
|