Disclaimer:

Some hints / scripts in this blog are of my own creation, some collected from internet. I wish to give credit to the original creator even if I forget to do so in individual posts.

Apr 14, 2008

Kinetic scrolling with Greasemonkey by binauralbeat

Original thread

// ==UserScript==
// @name Kinetic
// @namespace
// @include *
// @description
// ==/UserScript==

var TIME_THRESHOLD = 500;
var MAX_VELOCITY = 5000;

var scrollBy;
var sintv;
var startY = 0;
var endY = 0;
var startTime;
var endTime;
var timeScrolled;
var distanceScrolled;
var velocity;
var direction;


var intervalCount = 0;

function setStartY(e){
stopScroll();
startY = e.pageY;
startTime = new Date();
}

function startScroll(e){
stopScroll();
endY = e.pageY;
endTime = new Date();
if(e.pageX < 700){ // 700 should be innerwidth of window ... change me
Scroll(startY, endY, startTime, endTime);
}
}


function Scroll(stY,enY,stTime,enTime){

timeScrolled = (enTime.getSeconds()+ enTime.getMilliseconds()/1000) -

(stTime.getSeconds()+stTime.getMilliseconds()/1000);



if(timeScrolled < TIME_THRESHOLD){

distanceScrolled = Math.abs(enY - stY);

velocity = (distanceScrolled / timeScrolled);

if(velocity > MAX_VELOCITY){
velocity = MAX_VELOCITY;
}


//get direction
if(enY > stY){
direction = -1;
}else{
direction = 1;
}

sintv = setInterval(scrollIt, 10);

}
}

function scrollIt(){

intervalCount +=1;

var cseconds = 1+intervalCount/100;

scrollBy = (velocity*0.098)/(cseconds*cseconds);

if(scrollBy > 2){
window.scrollBy(0,parseInt(scrollBy * direction));
}else{
stopScroll();
}

}

function stopScroll(){

direction = 0;
velocity = 0;
intervalCount = 0;
distanceToScroll = 0;
clearInterval(sintv);
}

No comments: