-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (25 loc) · 953 Bytes
/
Copy pathscript.js
File metadata and controls
31 lines (25 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
let isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
if (isMobile) {
let controls = document.querySelector('.controls');
let isDragging = false;
let offsetX, offsetY;
controls.addEventListener('touchstart', (e) => {
isDragging = true;
offsetX = e.touches[0].clientX - controls.offsetLeft;
offsetY = e.touches[0].clientY - controls.offsetTop;
});
document.addEventListener('touchmove', (e) => {
if (isDragging) {
let x = e.touches[0].clientX - offsetX;
let y = e.touches[0].clientY - offsetY;
// Prevent going out of bounds
x = Math.max(0, Math.min(window.innerWidth - controls.offsetWidth, x));
y = Math.max(0, Math.min(window.innerHeight - controls.offsetHeight, y));
controls.style.left = `${x}px`;
controls.style.top = `${y}px`;
}
});
document.addEventListener('touchend', () => {
isDragging = false;
});
}