Merge pull request #2405 from tsauerwein/kinetic-fast

Improve kinetic effect when panning fast
This commit is contained in:
Tobias Sauerwein
2014-07-22 08:37:57 +02:00
2 changed files with 19 additions and 14 deletions

View File

@@ -67,10 +67,10 @@ ol.interaction.DragPan.prototype.handlePointerDrag = function(mapBrowserEvent) {
goog.asserts.assert(this.targetPointers.length >= 1); goog.asserts.assert(this.targetPointers.length >= 1);
var centroid = var centroid =
ol.interaction.Pointer.centroid(this.targetPointers); ol.interaction.Pointer.centroid(this.targetPointers);
if (this.kinetic_) {
this.kinetic_.update(centroid[0], centroid[1]);
}
if (!goog.isNull(this.lastCentroid)) { if (!goog.isNull(this.lastCentroid)) {
if (this.kinetic_) {
this.kinetic_.update(centroid[0], centroid[1]);
}
var deltaX = this.lastCentroid[0] - centroid[0]; var deltaX = this.lastCentroid[0] - centroid[0];
var deltaY = centroid[1] - this.lastCentroid[1]; var deltaY = centroid[1] - this.lastCentroid[1];
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;

View File

@@ -81,25 +81,30 @@ ol.Kinetic.prototype.update = function(x, y) {
* @return {boolean} Whether we should do kinetic animation. * @return {boolean} Whether we should do kinetic animation.
*/ */
ol.Kinetic.prototype.end = function() { ol.Kinetic.prototype.end = function() {
if (this.points_.length < 6) {
// at least 2 points are required (i.e. there must be at least 6 elements
// in the array)
return false;
}
var delay = goog.now() - this.delay_; var delay = goog.now() - this.delay_;
var lastIndex = this.points_.length - 3; var lastIndex = this.points_.length - 3;
if (this.points_[lastIndex + 2] < delay) { if (this.points_[lastIndex + 2] < delay) {
// the user stopped panning before releasing the map // the last tracked point is too old, which means that the user stopped
// panning before releasing the map
return false; return false;
} }
// get the first point which still falls into the delay time
var firstIndex = lastIndex - 3; var firstIndex = lastIndex - 3;
while (firstIndex >= 0 && this.points_[firstIndex + 2] > delay) { while (firstIndex > 0 && this.points_[firstIndex + 2] > delay) {
firstIndex -= 3; firstIndex -= 3;
} }
if (firstIndex >= 0) { var duration = this.points_[lastIndex + 2] - this.points_[firstIndex + 2];
var duration = this.points_[lastIndex + 2] - this.points_[firstIndex + 2]; var dx = this.points_[lastIndex] - this.points_[firstIndex];
var dx = this.points_[lastIndex] - this.points_[firstIndex]; var dy = this.points_[lastIndex + 1] - this.points_[firstIndex + 1];
var dy = this.points_[lastIndex + 1] - this.points_[firstIndex + 1]; this.angle_ = Math.atan2(dy, dx);
this.angle_ = Math.atan2(dy, dx); this.initialVelocity_ = Math.sqrt(dx * dx + dy * dy) / duration;
this.initialVelocity_ = Math.sqrt(dx * dx + dy * dy) / duration; return this.initialVelocity_ > this.minVelocity_;
return this.initialVelocity_ > this.minVelocity_;
}
return false;
}; };