-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathjquery.pano.js
240 lines (171 loc) · 5.13 KB
/
jquery.pano.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* global jQuery, module, require */
/*!
Pano v1.3.0
jQuery plugin to display a 360 degree panoramic image
Sean Coyne
https://github.com/seancoyne/pano
https://seancoyne.github.io/pano
*/
(function (factory) {
if(typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(require("jquery"));
} else {
factory(jQuery);
}
}(function($) {
$.fn.pano = function(options){
this.version = "1.3.0";
// get a handle on the panorama and controls
var $pano = this;
var $leftCtrl = $pano.find(".controls").find("a.left");
var $rightCtrl = $pano.find(".controls").find("a.right");
var getImageWidth = function(imgSrc) {
var img = new Image();
img.src = imgSrc;
return img.width;
};
var moveBackgroundTo = function(newPos, duration, cb) {
duration = duration || 0;
cb = cb || function(){};
$pano.animate({
"background-position": newPos.toString() + "px"
}, duration, "linear", cb);
};
var moveBackgroundBy = function(distance, duration, cb) {
duration = duration || 0;
cb = cb || function(){};
moveBackgroundTo(getCurrentPosition() + distance, duration, cb);
};
var getCurrentPosition = function() {
return parseInt($pano.css("background-position").split(" ")[0].replace("px", ""));
};
var indicateMovement = function() {
$pano.addClass("moving");
};
var noMovement = function() {
$pano.removeClass("moving");
};
var insideImage = function(mouseXPos) {
var $offsetLeft = $pano.offset().left;
var maxLeft = $offsetLeft;
var maxRight = $offsetLeft + $pano.width();
if( mouseXPos < maxLeft || mouseXPos > maxRight) {
return false;
}
return true;
};
var dragMove = function(xPos, startPosition, cb) {
// dont move if you're outside the image
if (!insideImage(xPos)) {
return false;
}
// calculate the change in position
var diff = (xPos - startPosition);
// move it
moveBackgroundBy(diff, 0, cb);
};
var leftMover,
rightMover,
ctrlInterval = options.interval || 100,
ctrlSpeed = options.speed || 50;
// setup the initial styling
$pano.css({
"background-image": "url('" + options.img + "')",
"background-position": "50% 50%",
"background-size": "auto 100%",
"background-repeat": "repeat-x"
});
// set the initial position in pixels (easier math)
var halfWidth = (getImageWidth(options.img) / 2);
moveBackgroundTo(halfWidth);
var moveLeft = function(interval, speed) {
interval = interval || ctrlInterval;
speed = speed || ctrlSpeed;
// indicate movement
indicateMovement();
// immediately move
moveBackgroundBy(speed, 100);
// move left on interval
leftMover = setInterval(function(){
moveBackgroundBy(speed, 100);
}, interval);
};
var moveRight = function(interval, speed) {
interval = interval || ctrlInterval;
speed = speed || ctrlSpeed;
// indicate movement
indicateMovement();
// immediately move
moveBackgroundBy(-speed, 100);
// move right on interval
rightMover = setInterval(function(){
moveBackgroundBy(-speed, 100);
}, interval);
};
var stopMoving = function() {
$pano.off("touchmove");
$pano.off("mousemove");
$pano.stop(true, true);
clearInterval(leftMover);
clearInterval(rightMover);
noMovement();
};
$leftCtrl.on("mousedown", function(event){
// dont process the drag events
event.stopPropagation();
moveLeft();
}).on("touchstart", function(event){
// dont process the drag events
event.stopPropagation();
// don't show the context menu while holding
event.preventDefault();
moveLeft();
});
$rightCtrl.on("mousedown", function(event){
// dont process the drag events
event.stopPropagation();
moveRight();
}).on("touchstart", function(event){
// dont process the drag events
event.stopPropagation();
// don't show the context menu while holding
event.preventDefault();
moveRight();
});
$pano.on("mousedown", function(event){
// indicate movement
indicateMovement();
var startPosition = event.pageX;
$pano.on("mousemove", function(event){
var xPos = event.pageX;
dragMove(xPos, startPosition, function(){
// after animation is complete, set the "start" position to the current position
startPosition =xPos;
});
});
}).on("touchstart", function(event){
// indicate movement
indicateMovement();
// don't show the context menu while holding
event.preventDefault();
var startPosition = event.pageX;
$pano.on("touchmove", function(event){
var xPos = event.originalEvent.changedTouches[0].pageX;
dragMove(xPos, startPosition, function(){
// after animation is complete, set the "start" position to the current position
startPosition = xPos;
});
});
});
$("body").on("mouseup", function(){
stopMoving();
}).on("touchend", function(){
stopMoving();
});
return {
moveLeft: moveLeft,
moveRight: moveRight,
stopMoving: stopMoving
};
};
}));