kinetic scrolling should base its speed on more than just the last event mobile
authordougt@dougt-ubuntu
Thu, 22 May 2008 10:52:42 -0700
branchmobile
changeset 64715 7613d50cb838f4446da07bba80dded404647ae73
parent 64714 008aaff8abfa251eaa16b7880f2e96c400d3b699
child 64716 5440b8355e0e42f2822e172eedf5757229be53ca
push idunknown
push userunknown
push dateunknown
kinetic scrolling should base its speed on more than just the last event
mobile/chrome/content/browser.js
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -411,26 +411,63 @@ ProgressController.prototype = {
 window.addEventListener("keydown", fskey, true);
 
 function fskey(aEvent) {
   alert(aEvent.keyCode);
   if (117 == aEvent.keyCode) {
   }
 }
 */
+var SpeedCache = function(maxsize) {
+    this.init(maxsize);
+}
+
+SpeedCache.prototype = {
+    _items   : null,
+    _maxsize : 1,
+
+    init: function(maxsize) {
+	
+	if (maxsize <= 0) maxsize = 1;
+	this._items = new Array(maxsize);
+	this._count = 0;
+	
+	for (x = 0; x < maxsize; x++) {
+	    this._items[x] = 0;
+	}
+    },
+
+    addSpeed: function(speed){
+	var index = this._count % this._items.length;
+	this._items[index] = speed;
+	this._count++;
+    },
+    
+    getAverage: function() {
+	var maxsize = this._items.length;
+	var sum = 0;
+	for (x = 0; x < maxsize; x++) {
+	    sum += this._items[x];
+	}
+	return sum / maxsize;
+    },
+}
 
 var MouseController = function(browser) {
   this.init(browser);
 }
 
 MouseController.prototype = {
   _browser: null,
   _contextID : null,
   _mousedown : false,
   _panning : false,
+  // just remember the last 5 events. 
+  _lastX   : new SpeedCache(5),
+  _lastY   : new SpeedCache(5),
 
   init: function(aBrowser)
   {
     this._browser = aBrowser;
     this._browser.addEventListener("mousedown", this, false);
     this._browser.addEventListener("mouseup",this, false);
     this._browser.addEventListener("mousemove", this, false);
   },
@@ -535,37 +572,36 @@ MouseController.prototype = {
       // see if we should continue
       if (Math.abs(speedX) > cutoff || Math.abs(speedY) > cutoff)
         setTimeout( function() { _doKineticScroll(browser, speedX, speedY, ++step); }, 0);
       else
         browser.endPan();
     };
 
     var browser = this._browser;
-    var speedX  = this._speedX * 100;
-    var speedY  = this._speedY * 100;
+    var speedX  = this._lastX.getAverage() * 100;
+    var speedY  = this._lastY.getAverage() * 100;
     setTimeout(function() { _doKineticScroll(browser, speedX, speedY, 0); }, 0);
   },
 
   mousemove: function(aEvent)
   {
     if (!this._mousedown)
       return;
 
     var delta = aEvent.timeStamp - this.lastEvent.timeStamp;
     var x = aEvent.screenX - this.lastEvent.screenX;
     var y = aEvent.screenY - this.lastEvent.screenY;
 
     // To reduce gitters, return if the mousemove was a small delta (bug 433513)
     if (40 > delta || (2 > Math.abs(x) && 2 > Math.abs(y)))
       return;
 
-    // Remember the last event so that we can do the kinetic scrolling
-    this._speedX = (x / delta);
-    this._speedY = (y / delta);
+    this._lastX.addSpeed(x / delta);
+    this._lastY.addSpeed(y / delta);
     this.lastEvent = aEvent;
 
     //dump("##: " + delta + " [" + x + ", " + y + "]\n");
     if (this._contextID) {
       clearTimeout(this._contextID);
       this._contextID = null;
     }