-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathzoomscript.js
119 lines (105 loc) · 3.67 KB
/
zoomscript.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var source = document.querySelector("#map");
var target = document.querySelector("#canvas");
var compass = document.querySelector('#compass');
var isPinching = false;
var startPinchDistance = 0;
var startWidth = 0;
var touchStartX = 0;
var touchStartY = 0;
var panX = 0;
var panY = 0;
var rotationAngle = 0;
var maxDisplacement = 500; // Maximum displacement in pixels for pan gestures
var scale = 1; // Initial scale for zoom.
source.addEventListener("wheel", function (e) {
e.preventDefault();
if (e.ctrlKey) {
scale -= e.deltaY * 0.01;
scale = Math.max(0.5, Math.min(3, scale)); // Limit scale between 0.5 and 3
target.style.transform = `scale(${scale})`;
} else {
// Pan
var x = getStyleInt(target, "left");
var newX = x - e.deltaX;
var y = getStyleInt(target, "top");
var newY = y - e.deltaY;
// Limit pan displacement
newX = Math.max(Math.min(newX, maxDisplacement), -maxDisplacement);
newY = Math.max(Math.min(newY, maxDisplacement), -maxDisplacement);
setStyleInt(target, "left", newX);
setStyleInt(target, "top", newY);
}
});
source.addEventListener("touchstart", function (e) {
if (e.touches.length === 2) {
// Start of pinch gesture
isPinching = true;
startPinchDistance = getPinchDistance(e.touches[0], e.touches[1]);
startWidth = target.getBoundingClientRect().width;
} else if (e.touches.length === 1) {
// Start of pan gesture
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
panX = getStyleInt(target, "left");
panY = getStyleInt(target, "top");
}
});
source.addEventListener("touchmove", function (e) {
if (isPinching && e.touches.length === 2) {
// Pinch gesture
var currentPinchDistance = getPinchDistance(e.touches[0], e.touches[1]);
var scale = currentPinchDistance / startPinchDistance;
scale = startWidth * scale / getStyleInt(target, "width");
target.style.transform = `scale(${scale})`;
} else if (e.touches.length === 1) {
// Pan gesture
var touchX = e.touches[0].clientX;
var touchY = e.touches[0].clientY;
var deltaX = touchX - touchStartX;
var deltaY = touchY - touchStartY;
var newX = panX + deltaX;
var newY = panY + deltaY;
// Limit pan displacement
newX = Math.max(Math.min(newX, maxDisplacement), -maxDisplacement);
newY = Math.max(Math.min(newY, maxDisplacement), -maxDisplacement);
setStyleInt(target, "left", newX);
setStyleInt(target, "top", newY);
}
});
source.addEventListener("touchend", function () {
// End of pinch gesture
isPinching = false;
});
source.addEventListener("touchstart", function (e) {
if (e.touches.length === 3) {
// Start of rotation gesture
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
}
});
source.addEventListener("touchmove", function (e) {
if (e.touches.length === 3) {
// Rotate
rt=0;
var touchX = e.touches[0].clientX;
var touchY = e.touches[0].clientY;
var deltaX = touchX - touchStartX;
var deltaY = touchY - touchStartY;
rotationAngle += (deltaX + deltaY) * 0.02;
target.style.transform = 'rotate(' + rotationAngle + 'deg)';
compass.style.transform= 'rotate(' + rotationAngle + 'deg)';
adjustDir(i-1);
}
});
function getStyleInt(target, key) {
var val = getComputedStyle(target)[key];
return parseInt(val, 10);
}
function setStyleInt(target, key, val) {
target.style[key] = val + "px";
}
function getPinchDistance(touch1, touch2) {
var dx = touch1.clientX - touch2.clientX;
var dy = touch1.clientY - touch2.clientY;
return Math.sqrt(dx * dx + dy * dy);
}