Skip to content

Commit

Permalink
Merge pull request #23 from benjaminluck/dev/new
Browse files Browse the repository at this point in the history
Finished scrolling mechanism detection.
  • Loading branch information
bennyvdhoogen authored Jul 12, 2017
2 parents 937526c + 7e61ddb commit c8ad204
Show file tree
Hide file tree
Showing 8 changed files with 52,501 additions and 8 deletions.
7 changes: 4 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ var components = [
"./js/src/components/StateManager.js",
"./js/src/components/SoundManager.js",
"./js/src/components/EasingTools.js",
"./js/src/components/ScrollingController.js",
"./js/src/components/_main.js",
"./js/src/components/world/init.js",
"./js/src/components/world/setup.js",
"./js/src/components/world/render.js",
"./js/src/components/InputController.js",
"./js/src/components/NavigationController.js"
]
]

gulp.task('watch-js', function(event){
gulp.watch('js/src/**/*.js', ['build']);
});

gulp.task('build', function() {
return gulp.src(components)
return gulp.src(components)
.pipe(concat('app.js'))
.pipe(uglify())
// .pipe(uglify())
.pipe(gulp.dest('js/dist/'));
});
52,060 changes: 52,059 additions & 1 deletion js/dist/app.js

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion js/src/components/InputController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,27 @@ function toggleMobileExpand(){
trackInfoElem.classList.toggle('expand-mobile');
}

document.addEventListener( 'mouseover' , mouseEnters, false);
document.addEventListener( 'mouseout' , mouseLeaves, false);
document.addEventListener( "visibilitychange" , windowActiveToggle, false);


function mouseEnters(){
window.shouldRender = true;
}

function mouseLeaves() {
window.touchingEdge = 0;
}

function windowActiveToggle(){
if(document.hidden){
window.shouldRender = false;
}else{
window.shouldRender = true;
}
}

function checkIfMouseNearEdges(event){
edges = {};
var edgeThickness = window.innerWidth * 0.20; // edge is 20% of screen width;
Expand Down Expand Up @@ -60,7 +75,13 @@ function onWindowResize() {
}

function onMouseWheel(event){
controls.rotate(0.001 * event.deltaY);
ScrollingController.runScrollingTypeCheck();

if(ScrollingController.applySmoothScrolling){
ScrollingController.handleEvent(event);
}else{
controls.rotate(0.001 * event.deltaY);
}
}

function onMouseMove( event ) {
Expand Down
280 changes: 280 additions & 0 deletions js/src/components/ScrollingController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
var ScrollingController = (function () {
var self = {};
// idle, playing, muted
self.applySmoothScrolling = false;
self.scrollingCurrentSpeed = 0;
self.scrollingLeftSpeed = 0;
self.scrollingRightSpeed = 0;
self.scrollingProgress = 0; // float bewteen 0 <-> 1

self.scrollingIncrement = 0.03;
self.dampingFactor = self.scrollingIncrement * 1;
self.scrollingStartSpeed = 0.01;
self.scrollingCap = 0.02;
self.scrollDuration = 100; //in ms
self.scrollStartTime = {};
self.scrollTimeLeft = 0;
self.scrollEndTime;
self.isScrolling = false;
self.isStopping = false;

self.scrollingCooldown = 25; // period (in ms) AFTER scroll when wheel events will be ignored
self.scrollingCooldownStart; // timestamp when cooldown started
self.scrollingThreshold = 155;

self.scrollingTypeCheck = {
status: 0,
startTime : 0,
endTime : 0,
duration: 200,
eventTimestamps: [],
numEvents: 0,
maxResults: 50,
checkResults: []
};

self.determinedScrollingMechanism = 'continous'; // default stepping

self.scrollingMechanismReads = {
data: {},
minPercentage: 0.6,
minReads: 15,
countMemory: 50,
init: function(){
standard = {
total: 0,
history: {
"stepper": {
count: 0,
percentage: 0
},
"continous": {
count: 0,
percentage: 0
}
}
}
return standard;
}
};

self.scrollingMechanismReads.data = self.scrollingMechanismReads.init();

self.determineScrollType = function(){
self.scrollingMechanismReads;
if(self.scrollingMechanismReads){
if(self.scrollingMechanismReads.data && self.scrollingMechanismReads.data.history){
for(key in self.scrollingMechanismReads.data.history){
if(self.scrollingMechanismReads.data.history[key].count > self.scrollingMechanismReads.minReads){
if(self.scrollingMechanismReads.data.history[key].percentage > self.scrollingMechanismReads.minPercentage){
self.determinedScrollingMechanism = key;
}
}
}
}
}

switch(self.determinedScrollingMechanism){
case 'continous':
self.applySmoothScrolling = false;
break;
case 'stepper':
self.applySmoothScrolling = true;
break;
}
}

self.readScrollingType = function(){
if(self.scrollingTypeCheck.checkResults){
var arrLength = self.scrollingTypeCheck.checkResults.length;
var targetResult = self.scrollingTypeCheck.checkResults[arrLength - 1];
}

if(targetResult){

if(targetResult.timeDeltaAvg < 20){
self.scrollingMechanismReads.data.total++;
var determinedMechanism = 'continous';
}

if(targetResult.timeDeltaAvg > 20){
self.scrollingMechanismReads.data.total++;
var determinedMechanism = 'stepper';
}

self.determinedScrollingMechanism = determinedMechanism
self.scrollingMechanismReads.data.history[determinedMechanism].count++;
self.scrollingMechanismReads.data.history[determinedMechanism].percentage = self.scrollingMechanismReads.data.history[determinedMechanism].count / self.scrollingMechanismReads.data.total;
self.applySmoothScrolling = false;
}

if(self.scrollingMechanismReads.data.total > self.scrollingMechanismReads.countMemory){
self.scrollingMechanismReads.data = self.scrollingMechanismReads.init();
}
}

self.runScrollingTypeCheck = function(){
var currentTime = new Date().getTime();
function init(status){
self.scrollingTypeCheck.status = status;
self.scrollingTypeCheck.startTime = currentTime;
self.scrollingTypeCheck.endTime = self.scrollingTypeCheck.startTime + self.scrollingTypeCheck.duration;
self.scrollingTypeCheck.numEvents = 0;
self.scrollingTypeCheck.eventTimestamps = [];
}

if(self.scrollingTypeCheck.status == 0){
init(1);
}

// on running
if(self.scrollingTypeCheck.status == 1){
self.scrollingTypeCheck.numEvents++;
self.scrollingTypeCheck.eventTimestamps.push(currentTime);
}

// on end
if(self.scrollingTypeCheck.status == 1 && currentTime > self.scrollingTypeCheck.endTime){
var avg = self.scrollingTypeCheck.eventTimestamps.reduce(function(acc, element, index, array) {
acc.current = array[index -1];
acc.current = element - acc.current;

if(!acc.current.isNan){
acc.timeDeltas.push(acc.current);
}
var sum = 0;
for(i = 0 ; i < acc.timeDeltas.length; i++){
if(!isNaN(acc.timeDeltas[i]) ){
sum = sum + acc.timeDeltas[i];
}
}

var avg = sum / array.length;
if(index == array.length -1){
return avg;
}

return acc;
}, {timeDeltas:[], current: self.scrollingTypeCheck.eventTimestamps[0]});

self.scrollingTypeCheck.checkResults.push({timestamp: self.scrollingTypeCheck.startTime, count: self.scrollingTypeCheck.numEvents, timeDeltaAvg: avg});
if(self.scrollingTypeCheck.checkResults.length > self.scrollingTypeCheck.maxResults){
self.scrollingTypeCheck.checkResults = [];
}

init(0);
}

self.readScrollingType();
self.determineScrollType();
}
// on scroll up/down event:
// 1) scroll left speed increases (if event.deltaY < 0)
// 1) scroll right speed increases (if event.deltaY > 0)
// every frame the scroll speed decreases so that after {{scrollDuration}} the speed is 0


self.determineTargetDirection = function(event){
if(event.deltaY < 0){
self.scrollingTargetDirection = -1;
if(self.scrollingLeftSpeed < self.scrollingCap){
self.scrollingLeftSpeed = self.scrollingLeftSpeed + (self.scrollingIncrement);
// self.scrollingLeftSpeed = self.scrollingIncrement * (1 - self.scrollingProgress);
}
}
if(event.deltaY > 0){
self.scrollingTargetDirection = 1;
if(self.scrollingRightSpeed < self.scrollingCap){
self.scrollingRightSpeed = self.scrollingRightSpeed + (self.scrollingIncrement);
// self.scrollingRightSpeed = self.scrollingIncrement * (1 - self.scrollingProgress);
}
}
}

self.updateProgress = function(){
if(self.isScrolling){
var currentTime = new Date().getTime();
self.scrollingProgress = (currentTime - self.scrollStartTime) / self.scrollDuration;
}
if(self.scrollingProgress > 1){
self.scrollingProgress = 1;
}
}

self.handleEvent = function(event){
var currentTime = new Date().getTime();
var wheelDelta = event.deltaY;
if(wheelDelta < 0){
wheelDelta = wheelDelta * -1;
}


if(currentTime < (self.scrollingCooldownStart + self.scrollingCooldown)){
self.scrollingCooldownStart = currentTime;
if(wheelDelta < self.scrollingThreshold){
return false;
}
//return false;
}


self.determineTargetDirection(event);

if(!self.isScrolling){
self.scrollStartTime = new Date().getTime();
self.scrollingProgress = 0;
self.scrollEndTime = self.scrollStartTime + self.scrollDuration;
self.scrollingCurrentSpeed = self.scrollingStartSpeed;
self.isScrolling = true;
self.scrollingCooldownStart = new Date().getTime();
}else{
// if already scrolling:
}
}

self.updateSpeed = function(){
self.scrollingCurrentSpeed = (self.scrollingLeftSpeed * -1) + self.scrollingRightSpeed;
self.scrollingCurrentSpeed = self.scrollingCurrentSpeed * (1 - self.scrollingProgress);
}

self.applyDamping = function(){

if(self.scrollingLeftSpeed > 0){
self.scrollingLeftSpeed = self.scrollingLeftSpeed - (self.dampingFactor);
}else{
self.scrollingLeftSpeed = 0;
}
if(self.scrollingRightSpeed > 0){
self.scrollingRightSpeed = self.scrollingRightSpeed - (self.dampingFactor);
}else{
self.scrollingRightSpeed = 0;
}

if(self.scrollingCurrentSpeed < 0){
self.scrollingActualDirection = 0;
self.scrollingTargetDirection = 0;
}
}

self.updateTimeElapsed = function(){
if(self.isScrolling){
self.scrollTimeLeft = self.scrollEndTime - new Date().getTime();
if(self.scrollStartTime < (new Date().getTime() - self.scrollDuration)){
self.applyDamping();
self.isScrolling = false;

}
}
}

self.applyScrollRotation = function(){
if(self.applySmoothScrolling){
self.updateTimeElapsed();
window.controls.rotate(self.scrollingCurrentSpeed);
self.updateSpeed();
self.updateProgress();
}
}

return self;
}());
Loading

0 comments on commit c8ad204

Please sign in to comment.