Changed around line 1
+ const ball = document.getElementById('ball');
+ let isDragging = false;
+ let offsetX, offsetY;
+
+ ball.addEventListener('mousedown', (e) => {
+ isDragging = true;
+ offsetX = e.clientX - ball.getBoundingClientRect().left;
+ offsetY = e.clientY - ball.getBoundingClientRect().top;
+ ball.style.transition = 'none';
+ });
+
+ document.addEventListener('mousemove', (e) => {
+ if (isDragging) {
+ const x = e.clientX - offsetX;
+ const y = e.clientY - offsetY;
+ ball.style.left = `${x}px`;
+ ball.style.top = `${y}px`;
+ }
+ });
+
+ document.addEventListener('mouseup', () => {
+ if (isDragging) {
+ isDragging = false;
+ ball.style.transition = 'all 0.5s ease-out';
+ const ballRect = ball.getBoundingClientRect();
+ const containerRect = document.querySelector('.container').getBoundingClientRect();
+
+ let newX = ballRect.left;
+ let newY = ballRect.top;
+
+ if (ballRect.left < containerRect.left) {
+ newX = containerRect.left;
+ } else if (ballRect.right > containerRect.right) {
+ newX = containerRect.right - ballRect.width;
+ }
+
+ if (ballRect.top < containerRect.top) {
+ newY = containerRect.top;
+ } else if (ballRect.bottom > containerRect.bottom) {
+ newY = containerRect.bottom - ballRect.height;
+ }
+
+ ball.style.left = `${newX}px`;
+ ball.style.top = `${newY}px`;
+ }
+ });