Skip to content

Commit

Permalink
cleaned up source
Browse files Browse the repository at this point in the history
  • Loading branch information
twobit committed Mar 20, 2012
1 parent c45605f commit 4c6b22f
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions js/parallaxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
this._stage = stage;
this._transitioning = false;
this._lastCursor = null;
this._xRange = 0.1; // Move at most 1/10 window width in x direction
this._yRange = 0.2; // Move at most 1/5 window height in y direction

window.addEventListener('devicemotion', this._onDeviceMove.bind(this), true);
window.addEventListener('mousemove', this._onMouseMove.bind(this), true);
}

Parallaxer.TRANSITION = 'all .08s linear';
Parallaxer.TRANSITION = 'all .01s linear';
Parallaxer.TRANSITION_DIST_SQ = 200 * 200; // Minimum distance before applying a smooth transition effect

Parallaxer.prototype = {
Expand All @@ -17,7 +19,6 @@
},

_transition: function() {
console.log('TRANSITION');
this._transitioning = true;
this._stage.style.WebkitTransition = Parallaxer.TRANSITION;
window.addEventListener('webkitTransitionEnd', this._onEndTransition.bind(this), true);
Expand All @@ -29,27 +30,24 @@
},

_onMouseMove: function(e) {
var cursor = {x: e.clientX, y: e.clientY},
win = {width: window.innerWidth, height: window.innerHeight},
stage = {width: this._stage.offsetWidth, height: this._stage.offsetHeight};
var cursor = {x: e.clientX, y: e.clientY};

if (this._lastCursor && !this._transitioning) {
if (!this._lastCursor) { // Transition to initial mouse coordinates
this._transition();
}
else if (!this._transitioning) { // Transition if distance between mouse events is large
var dx = cursor.x - this._lastCursor.x,
dy = cursor.y - this._lastCursor.y;

if (dx * dx + dy * dy > Parallaxer.TRANSITION_DIST_SQ) {
this._transition();
}
}
else {
this._transition();
}

var x = -(cursor.x / win.width - 0.5) * stage.width * 0.1,
y = -(cursor.y / win.height - 0.5) * stage.height * 0.2;
var x = -(cursor.x / window.innerWidth - 0.5) * window.innerWidth * this._xRange,
y = -(cursor.y / window.innerHeight - 0.5) * window.innerHeight * this._yRange;

this._stage.style.WebkitTransform = 'translate(' + x + 'px,' + y + 'px)';

this._lastCursor = cursor;
}
};
Expand Down

0 comments on commit 4c6b22f

Please sign in to comment.