How to implement simple kinetic scrolling in Javascript for such functions?
I am trying to implement simple kinetic scrolling / eazing on 2 parameters. It works... partly/mostly. But something is very wrong. Problem and full code can be observed here.
So I have tried such code (contains no errors that Google Chrome can see but is not working as needed - crashes something from time to time (randomly) and scrolls in wrong direction (only in one).. ):
var dragDuration = 0;
var lonBuffer = 0;
var latBuffer = 0;
var lastLonValue = 0;
var lastLatValue = 0;
var lonSignBuffer = 0;
var latSignBuffer = 0;
var kineticTimer;
var kineticScrollingPrivateTimer;
function startKineticTimer() {
stopKineticScrolling();
kineticTimerTick();
}
function kineticTimerTick() {
dragDuration = dragDuration + 1;
kineticTimer = setTimeout(kineticTimerTick, 50);
}
function stopKineticTimer() {
clearTimeout(kineticTimer);
startKineticScrolling(dragDuration);
}
function addToKineticBuffers(Lon, Lat) {
var currentLonSign = ((lastLonValue - Lon) >= 0) ? 1 : -1; //if(lon >= 0){currentLonSign = 1;}else{currentLonSign = -1;}
var currentLatSign = ((lastLatValue - Lat) >= 0) ? 1 : -1;
if (currentLonSign != lonSignBuffer) {
lonBuffer = 0;
dragDuration = 0;
lonSignBuffer = currentLonSign;
}
if (currentLatSign != latSignBuffer) {
latBuffer = 0;
dragDuration = 0;
latSignBuffer = currentLatSign;
}
lonBuffer = lonBuffer + Lon;
latBuffer = latBuffer + Lat;
lastLonValue = Lon;
lastLatValue = Lat;
}
function moveLonLat(newLon, newLat) {
lon += newLon * 0.01;
lat += newLat * 0.01;
lastLonValue = lon;
lastLatValue = lat;
render();
}
function kineticScrolling() {
if (latBuffer < 0) {
latBuffer = 0;
}
if (lonBuffer < 0) {
lonBuffer = 0;
}
moveLonLat(lonBuffer / dragDuration, latBuffer / dragDuration);
lonBuffer = lonBuffer - lonBuffer / dragDuration;
latBuffer = latBuffer - latBuffer / dragDuration;
kineticScrollingPrivateTimer = setTimeout(kineticScrolling, 100);
if (latBuffer < 0) {
latBuffer = 0; stopKineticScrolling();
}
if (lonBuffer <= 0) {
lonBuffer = 0; stopKineticScrolling();
}
}
function startKineticScrolling() {
kineticScrolling();
}
function stopKineticScrolling() {
clearTimeout(kineticScrollingPrivateTimer);
}
function onDocumentMouseDown(event) {
event.preventDefault();
stopKineticScrolling();
startKineticTimer();
isUserInteracting = true;
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
function onDocumentMouseMove(event) {
if (is开发者_如何学编程UserInteracting) {
lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon;
lat = (event.clientY - onPointerDownPointerY) * 0.1 + onPointerDownLat;
addToKineticBuffers(lon, lat);
render();
}
}
function onDocumentMouseUp(event) {
isUserInteracting = false;
stopKineticTimer();
render();
}
function onDocumentMouseWheel(event) {
fov -= event.wheelDeltaY * 0.05;
camera.projectionMatrix = THREE.Matrix4.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
render();
}
function onDocumentTouchStart(event) {
if (event.touches.length == 1) {
event.preventDefault();
onPointerDownPointerX = event.touches[0].pageX;
onPointerDownPointerY = event.touches[0].pageY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
}
function onDocumentTouchMove(event) {
if (event.touches.length == 1) {
event.preventDefault();
lon = (onPointerDownPointerX - event.touches[0].pageX) * 0.1 + onPointerDownLon;
lat = (event.touches[0].pageY - onPointerDownPointerY) * 0.1 + onPointerDownLat;
render();
}
}
function render() {
lat = Math.max(-85, Math.min(85, lat));
phi = (90 - lat) * Math.PI / 180;
theta = lon * Math.PI / 180;
camera.target.position.x = 500 * Math.sin(phi) * Math.cos(theta);
camera.target.position.y = 500 * Math.cos(phi);
camera.target.position.z = 500 * Math.sin(phi) * Math.sin(theta);
renderer.render(scene, camera);
}
I wonder how to fix my errors? (but not using Jquery or any other libs)
精彩评论