Skip to content

Commit

Permalink
Merge pull request #47 from elemoine/pinchzoom
Browse files Browse the repository at this point in the history
fix bug where center of fingers is not preserved on pinch zoom
  • Loading branch information
elemoine committed Nov 14, 2011
2 parents b5d41fa + 629e14d commit 29256bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions lib/OpenLayers/Control/PinchZoom.js
Expand Up @@ -22,10 +22,10 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
type: OpenLayers.Control.TYPE_TOOL,

/**
* Property: containerOrigin
* {Object} Cached object representing the layer container origin (in pixels).
* Property: containerCenter
* {Object} Cached object representing the layer container center (in pixels).
*/
containerOrigin: null,
containerCenter: null,

/**
* Property: pinchOrigin
Expand Down Expand Up @@ -76,10 +76,10 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
var activated = OpenLayers.Control.prototype.activate.apply(this,arguments);
if (activated) {
this.map.events.on({
moveend: this.updateContainerOrigin,
moveend: this.updateContainerCenter,
scope: this
});
this.updateContainerOrigin();
this.updateContainerCenter();
}
return activated;
},
Expand All @@ -95,22 +95,23 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
var deactivated = OpenLayers.Control.prototype.deactivate.apply(this,arguments);
if (this.map && this.map.events) {
this.map.events.un({
moveend: this.updateContainerOrigin,
moveend: this.updateContainerCenter,
scope: this
});
}
return deactivated;
},

/**
* Method: updateContainerOrigin
* Must be called each time the layer container origin changes.
* Method: updateContainerCenter
* Must be called each time the layer container moves.
*/
updateContainerOrigin: function() {
updateContainerCenter: function() {
var container = this.map.layerContainerDiv;
this.containerOrigin = {
x: parseInt(container.style.left, 10),
y: parseInt(container.style.top, 10)
// the layer container div is a square of 100px/100px
this.containerCenter = {
x: parseInt(container.style.left, 10) + 50,
y: parseInt(container.style.top, 10) + 50
};
},

Expand All @@ -137,12 +138,12 @@ OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, {
*/
pinchMove: function(evt, pinchData) {
var scale = pinchData.scale;
var containerOrigin = this.containerOrigin;
var containerCenter = this.containerCenter;
var pinchOrigin = this.pinchOrigin;
var current = evt.xy;

var dx = Math.round((current.x - pinchOrigin.x) + (scale - 1) * (containerOrigin.x - pinchOrigin.x));
var dy = Math.round((current.y - pinchOrigin.y) + (scale - 1) * (containerOrigin.y - pinchOrigin.y));
var dx = Math.round((current.x - pinchOrigin.x) + (scale - 1) * (containerCenter.x - pinchOrigin.x));
var dy = Math.round((current.y - pinchOrigin.y) + (scale - 1) * (containerCenter.y - pinchOrigin.y));

this.applyTransform(
"translate(" + dx + "px, " + dy + "px) scale(" + scale + ")"
Expand Down
2 changes: 1 addition & 1 deletion tests/Control/PinchZoom.html
Expand Up @@ -49,7 +49,7 @@
log.push(transform);
}

control.containerOrigin = {
control.containerCenter = {
x: 0, y: 0
};

Expand Down

0 comments on commit 29256bd

Please sign in to comment.