-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessingMapZoomPan.pde
160 lines (125 loc) · 2.98 KB
/
ProcessingMapZoomPan.pde
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*Map Zoom and Pan in Processing
Author: Jinlong Yang
Email: jinlong.yang@mail.sdsu.edu
*/
/*When use the code, you may change
the zoomFactor to have finer/coarser
zooming, or change the maxScale to
limit the most detailed level of zooming
allowed.*/
//Define the map vars
PImage map;
int imgW;
int imgH;
int centerX;
int centerY;
//Define the zoom vars
int scale = 1;
int maxScale = 10;
float zoomFactor = 0.4;
//Define the pan vars
int panFromX;
int panFromY;
int panToX;
int panToY;
int xShift = 0;
int yShift = 0;
void setup() {
map = loadImage("image.jpg");
imgW = map.width;
imgH = map.height;
centerX = imgW / 2;
centerY = imgH / 2;
size(800, 600);
}
void draw(){
background(0);
imageMode(CENTER);
image(map, centerX, centerY, imgW, imgH);
}
//Pan function
void mousePressed(){
if(mouseButton == LEFT){
panFromX = mouseX;
panFromY = mouseY;
}
}
//Pan function continued..
void mouseDragged(){
if(mouseButton == LEFT){
panToX = mouseX;
panToY = mouseY;
xShift = panToX - panFromX;
yShift = panToY - panFromY;
//Only pan with the image occupies the whole display
if(centerX - imgW / 2 <= 0
&& centerX + imgW / 2 >= width
&& centerY - imgH / 2 <= 0
&& centerY + imgH / 2 >= height){
centerX = centerX + xShift;
centerY = centerY + yShift;
}
//Set the constraints for pan
if(centerX - imgW / 2 > 0){
centerX = imgW / 2;
}
if(centerX + imgW / 2 < width){
centerX = width - imgW / 2;
}
if(centerY - imgH / 2 > 0){
centerY = imgH / 2;
}
if(centerY + imgH / 2 < height){
centerY = height - imgH / 2;
}
panFromX = panToX;
panFromY = panToY;
}
}
//Zoom function
void mouseWheel(MouseEvent event) {
float e = event.getAmount();
//Zoom in
if(e == -1){
if(scale < maxScale){
scale++;
imgW = int(imgW * (1+zoomFactor));
imgH = int(imgH * (1+zoomFactor));
int oldCenterX = centerX;
int oldCenterY = centerY;
centerX = centerX - int(zoomFactor * (mouseX - centerX));
centerY = centerY - int(zoomFactor * (mouseY - centerY));
}
}
//Zoom out
if(e == 1){
if(scale < 1){
scale = 1;
imgW = map.width;
imgH = map.height;
}
if(scale > 1){
scale--;
imgH = int(imgH/(1+zoomFactor));
imgW = int(imgW/(1+zoomFactor));
int oldCenterX = centerX;
int oldCenterY = centerY;
centerX = centerX + int((mouseX - centerX)
* (zoomFactor/(zoomFactor + 1)));
centerY = centerY + int((mouseY - centerY)
* (zoomFactor/(zoomFactor + 1)));
if(centerX - imgW / 2 > 0){
centerX = imgW / 2;
}
if(centerX + imgW / 2 < width){
centerX = width - imgW / 2;
}
if(centerY - imgH / 2 > 0){
centerY = imgH / 2;
}
if(centerY + imgH / 2 < height){
centerY = height - imgH / 2;
}
}
}
}