fix bug where center of fingers is not preserved on pinch zoom

This commit is contained in:
Éric Lemoine
2011-11-10 21:35:38 +01:00
parent 18b23e944d
commit e6f2b0e294
2 changed files with 13 additions and 12 deletions

View File

@@ -22,10 +22,10 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
type: OpenLayers.Control.TYPE_TOOL, type: OpenLayers.Control.TYPE_TOOL,
/** /**
* Property: containerOrigin * Property: containerCenter
* {Object} Cached object representing the layer container origin (in pixels). * {Object} Cached object representing the layer container center (in pixels).
*/ */
containerOrigin: null, containerCenter: null,
/** /**
* Property: pinchOrigin * Property: pinchOrigin
@@ -103,14 +103,15 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
}, },
/** /**
* Method: updateContainerOrigin * Method: updateContainerCenter
* Must be called each time the layer container origin changes. * Must be called each time the layer container moves.
*/ */
updateContainerOrigin: function() { updateContainerOrigin: function() {
var container = this.map.layerContainerDiv; var container = this.map.layerContainerDiv;
this.containerOrigin = { // the layer container div is a square of 100px/100px
x: parseInt(container.style.left, 10), this.containerCenter = {
y: parseInt(container.style.top, 10) x: parseInt(container.style.left, 10) + 50,
y: parseInt(container.style.top, 10) + 50
}; };
}, },
@@ -137,12 +138,12 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
*/ */
pinchMove: function(evt, pinchData) { pinchMove: function(evt, pinchData) {
var scale = pinchData.scale; var scale = pinchData.scale;
var containerOrigin = this.containerOrigin; var containerCenter = this.containerCenter;
var pinchOrigin = this.pinchOrigin; var pinchOrigin = this.pinchOrigin;
var current = evt.xy; var current = evt.xy;
var dx = Math.round((current.x - pinchOrigin.x) + (scale - 1) * (containerOrigin.x - pinchOrigin.x)); var dx = Math.round((current.x - pinchOrigin.x) + (scale - 1) * (containerCenter.x - pinchOrigin.x));
var dy = Math.round((current.y - pinchOrigin.y) + (scale - 1) * (containerOrigin.y - pinchOrigin.y)); var dy = Math.round((current.y - pinchOrigin.y) + (scale - 1) * (containerCenter.y - pinchOrigin.y));
this.applyTransform( this.applyTransform(
"translate(" + dx + "px, " + dy + "px) scale(" + scale + ")" "translate(" + dx + "px, " + dy + "px) scale(" + scale + ")"

View File

@@ -49,7 +49,7 @@
log.push(transform); log.push(transform);
} }
control.containerOrigin = { control.containerCenter = {
x: 0, y: 0 x: 0, y: 0
}; };