Ensure that the first point for kinetic is valid

This commit is contained in:
tsauerwein
2014-07-18 16:07:59 +02:00
parent 02a2c5aceb
commit 73951394f9
+16 -11
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;
}; };