Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const football = document.querySelector('.football');
+ let isDragging = false;
+ let currentX, currentY, initialX, initialY;
+ let xOffset = 0;
+ let yOffset = 0;
+ let velocity = { x: 0, y: 0 };
+ let lastPos = { x: 0, y: 0 };
+ let animationId;
+
+ const gravity = 0.5;
+ const bounce = 0.7;
+ const friction = 0.99;
+
+ function dragStart(e) {
+ if (e.type === "touchstart") {
+ initialX = e.touches[0].clientX - xOffset;
+ initialY = e.touches[0].clientY - yOffset;
+ } else {
+ initialX = e.clientX - xOffset;
+ initialY = e.clientY - yOffset;
+ }
+
+ if (e.target === football) {
+ isDragging = true;
+ cancelAnimationFrame(animationId);
+ }
+ }
+
+ function drag(e) {
+ if (isDragging) {
+ e.preventDefault();
+
+ if (e.type === "touchmove") {
+ currentX = e.touches[0].clientX - initialX;
+ currentY = e.touches[0].clientY - initialY;
+ } else {
+ currentX = e.clientX - initialX;
+ currentY = e.clientY - initialY;
+ }
+
+ xOffset = currentX;
+ yOffset = currentY;
+
+ velocity.x = currentX - lastPos.x;
+ velocity.y = currentY - lastPos.y;
+
+ lastPos.x = currentX;
+ lastPos.y = currentY;
+
+ setTranslate(currentX, currentY, football);
+ }
+ }
+
+ function setTranslate(xPos, yPos, el) {
+ el.style.transform = `translate(${xPos}px, ${yPos}px) rotate(${xPos * 0.2}deg)`;
+ }
+
+ function dragEnd() {
+ if (isDragging) {
+ isDragging = false;
+ animate();
+ }
+ }
+
+ function animate() {
+ if (!isDragging) {
+ velocity.y += gravity;
+ velocity.x *= friction;
+
+ xOffset += velocity.x;
+ yOffset += velocity.y;
+
+ const ground = window.innerHeight - 160;
+
+ if (yOffset > ground) {
+ yOffset = ground;
+ velocity.y *= -bounce;
+ }
+
+ if (Math.abs(xOffset) > window.innerWidth/2) {
+ velocity.x *= -1;
+ xOffset = Math.sign(xOffset) * window.innerWidth/2;
+ }
+
+ setTranslate(xOffset, yOffset, football);
+
+ if (Math.abs(velocity.y) > 0.1 || Math.abs(velocity.x) > 0.1) {
+ animationId = requestAnimationFrame(animate);
+ }
+ }
+ }
+
+ football.addEventListener('mousedown', dragStart);
+ football.addEventListener('touchstart', dragStart, { passive: false });
+ document.addEventListener('mousemove', drag);
+ document.addEventListener('touchmove', drag, { passive: false });
+ document.addEventListener('mouseup', dragEnd);
+ document.addEventListener('touchend', dragEnd);
+ });