<script>
let startCoord;
window.addEventListener('mousedown', (event) => {
startCoord = [event.clientX, event.clientY]; -------------- ①
});
window.addEventListener('mouseup', (event) => {
const endCoord = [event.clientX, event.clientY]; ---------- ②
const diffX = endCoord[0] - startCoord[0]; ---------------- ③
const diffY = endCoord[1] - startCoord[1]; ---------------- ③
const isUnder45 = Math.abs(diffX) > Math.abs(diffY); ------ ④
if (diffX < 0 && isUnder45) { ----------------------------- ⑤
console.log('왼쪽');
} else if (diffX > 0 && isUnder45) {
console.log('오른쪽');
} else if (diffY > 0 && !isUnder45) {
console.log('아래쪽');
} else if (diffY < 0 && !isUnder45) {
console.log('위쪽');
}
});
</script>
코드가 복잡하니 한 줄씩 살펴보겠습니다.