[javascript] 터치 이벤트를 사용하여 터치한 위치에서 스와이프 동작을 인식하는 방법은?
let startX;
let startY;
let endX;
let endY;

const threshold = 100; // 스와이프를 감지하기 위한 최소 이동 거리

document.addEventListener("touchstart", function(event) {
  startX = event.touches[0].clientX;
  startY = event.touches[0].clientY;
});

document.addEventListener("touchend", function(event) {
  endX = event.changedTouches[0].clientX;
  endY = event.changedTouches[0].clientY;
  handleSwipe();
});

function handleSwipe() {
  const dx = endX - startX;
  const dy = endY - startY;
  const absDx = Math.abs(dx);
  const absDy = Math.abs(dy);

  if (absDx > absDy && absDx > threshold) {
    if (dx > 0) {
      // 오른쪽 스와이프
      console.log("Right swipe detected");
    } else {
      // 왼쪽 스와이프
      console.log("Left swipe detected");
    }
  } else if (absDy > threshold) {
    if (dy > 0) {
      // 아래쪽 스와이프
      console.log("Downward swipe detected");
    } else {
      // 위쪽 스와이프
      console.log("Upward swipe detected");
    }
  }
}

위의 코드는 터치 이벤트를 사용하여 터치한 위치에서 스와이프 동작을 감지합니다. touchstart 이벤트로 시작 위치를 기록하고, touchend 이벤트로 최종 위치를 기록한 후 handleSwipe 함수를 호출하여 스와이프 방향을 확인합니다.

이 코드를 통해 웹 애플리케이션에서 터치 이벤트를 사용하여 스와이프 동작을 쉽게 구현할 수 있습니다.