From 73951394f9356ef59a1e9d5a25d956d635c0467a Mon Sep 17 00:00:00 2001 From: tsauerwein Date: Fri, 18 Jul 2014 16:07:59 +0200 Subject: [PATCH] Ensure that the first point for kinetic is valid --- src/ol/kinetic.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/ol/kinetic.js b/src/ol/kinetic.js index 0960f81ca4..a6d9b54d67 100644 --- a/src/ol/kinetic.js +++ b/src/ol/kinetic.js @@ -81,25 +81,30 @@ ol.Kinetic.prototype.update = function(x, y) { * @return {boolean} Whether we should do kinetic animation. */ 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 lastIndex = this.points_.length - 3; 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; } + + // get the first point which still falls into the delay time var firstIndex = lastIndex - 3; - while (firstIndex >= 0 && this.points_[firstIndex + 2] > delay) { + while (firstIndex > 0 && this.points_[firstIndex + 2] > delay) { firstIndex -= 3; } - if (firstIndex >= 0) { - var duration = this.points_[lastIndex + 2] - this.points_[firstIndex + 2]; - var dx = this.points_[lastIndex] - this.points_[firstIndex]; - var dy = this.points_[lastIndex + 1] - this.points_[firstIndex + 1]; - this.angle_ = Math.atan2(dy, dx); - this.initialVelocity_ = Math.sqrt(dx * dx + dy * dy) / duration; - return this.initialVelocity_ > this.minVelocity_; - } - return false; + var duration = this.points_[lastIndex + 2] - this.points_[firstIndex + 2]; + var dx = this.points_[lastIndex] - this.points_[firstIndex]; + var dy = this.points_[lastIndex + 1] - this.points_[firstIndex + 1]; + this.angle_ = Math.atan2(dy, dx); + this.initialVelocity_ = Math.sqrt(dx * dx + dy * dy) / duration; + return this.initialVelocity_ > this.minVelocity_; };