JQVMap.prototype.makeDraggable = function () {
var mouseDown = false;
var oldPageX, oldPageY;
var self = this;
self.isMoving = false;
self.isMovingTimeout = false;
var lastTouchCount;
var touchCenterX;
var touchCenterY;
var touchStartDistance;
var touchStartScale;
var touchX;
var touchY;
this.container.mousemove(function (e) {
if (mouseDown) {
self.transX -= (oldPageX - e.pageX) / self.scale;
self.transY -= (oldPageY - e.pageY) / self.scale;
self.applyTransform();
oldPageX = e.pageX;
oldPageY = e.pageY;
self.isMoving = true;
if (self.isMovingTimeout) {
clearTimeout(self.isMovingTimeout);
}
self.container.trigger('drag');
}
return false;
}).mousedown(function (e) {
mouseDown = true;
oldPageX = e.pageX;
oldPageY = e.pageY;
return false;
}).mouseup(function () {
mouseDown = false;
clearTimeout(self.isMovingTimeout);
self.isMovingTimeout = setTimeout(function () {
self.isMoving = false;
}, 100);
return false;
}).mouseout(function () {
if(mouseDown && self.isMoving){
clearTimeout(self.isMovingTimeout);
self.isMovingTimeout = setTimeout(function () {
mouseDown = false;
self.isMoving = false;
}, 100);
return false;
}
});
jQuery(this.container).bind('touchmove', function (e) {
var offset;
var scale;
var touches = e.originalEvent.touches;
var transformXOld;
var transformYOld;
if (touches.length === 1) {
if (lastTouchCount === 1) {
if(touchX === touches[0].pageX && touchY === touches[0].pageY){
return;
}
transformXOld = self.transX;
transformYOld = self.transY;
self.transX -= (touchX - touches[0].pageX) / self.scale;
self.transY -= (touchY - touches[0].pageY) / self.scale;
self.applyTransform();
if (transformXOld !== self.transX || transformYOld !== self.transY) {
e.preventDefault();
}
self.isMoving = true;
if (self.isMovingTimeout) {
clearTimeout(self.isMovingTimeout);
}
}
touchX = touches[0].pageX;
touchY = touches[0].pageY;
} else if (touches.length === 2) {
if (lastTouchCount === 2) {
scale = Math.sqrt(
Math.pow(touches[0].pageX - touches[1].pageX, 2) +
Math.pow(touches[0].pageY - touches[1].pageY, 2)
) / touchStartDistance;
self.setScale(
touchStartScale * scale,
touchCenterX,
touchCenterY
);
e.preventDefault();
} else {
offset = jQuery(self.container).offset();
if (touches[0].pageX > touches[1].pageX) {
touchCenterX = touches[1].pageX + (touches[0].pageX - touches[1].pageX) / 2;
} else {
touchCenterX = touches[0].pageX + (touches[1].pageX - touches[0].pageX) / 2;
}
if (touches[0].pageY > touches[1].pageY) {
touchCenterY = touches[1].pageY + (touches[0].pageY - touches[1].pageY) / 2;
} else {
touchCenterY = touches[0].pageY + (touches[1].pageY - touches[0].pageY) / 2;
}
touchCenterX -= offset.left;
touchCenterY -= offset.top;
touchStartScale = self.scale;
touchStartDistance = Math.sqrt(
Math.pow(touches[0].pageX - touches[1].pageX, 2) +
Math.pow(touches[0].pageY - touches[1].pageY, 2)
);
}
}
lastTouchCount = touches.length;
});
jQuery(this.container).bind('touchstart', function () {
lastTouchCount = 0;
});
jQuery(this.container).bind('touchend', function () {
lastTouchCount = 0;
});
};
|