Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dumping old code as gists...
- (function($) {
- $.mouseMovement = {
- stack: [],
- clearInterval: 1000
- };
- var stack = $.mouseMovement.stack;
- /*
- * Point
- * -------------------------
- */
- var Point = function( x, y, t ) {
- this.x = x;
- this.y = y;
- this.t = t || $.now();
- }
- Point.fromEvent = function( e ) {
- return new Point( e.pageX, e.pageY, e.timeStamp );
- };
- /*
- * ANGLE
- * -------------------------
- */
- var Angle = function( val ) {
- this.val = val;
- };
- $.extend( Angle, {
- fromVector: function( vector ) {
- return (new Angle( Math.atan2( vector.y, vector.x ) )).normalize();
- },
- fromDeg: function( deg ) {
- return new Angle( Math.PI / 180 * deg );
- }
- });
- Angle.prototype = {
- MOD_CIRCLE: 2 * Math.PI,
- normalize: function() {
- this.val = (this.val + this.MOD_CIRCLE) % this.MOD_CIRCLE;
- return this;
- },
- isOpposite: function( angle, boundingInterval ) {
- var min = (new Angle( angle.val + Math.PI - boundingInterval / 2 )).normalize(),
- max = (new Angle( angle.val + Math.PI + boundingInterval / 2 )).normalize();
- return this.normalize().val >= min.val && this.val <= max.val;
- },
- toDeg: function() {
- return 180 / Math.PI * this.val;
- }
- };
- /*
- * Vector
- * -------------------------
- */
- var Vector = function( p1, p2 ) {
- if ( p1 && p2 ) {
- this.p1 = p1;
- this.p2 = p2;
- // Delta Time, X, Y -> calculate!.
- this.diff( 't' ).diff( 'x' ).diff( 'y' ).calc();
- }
- };
- Vector.prototype = {
- add: function( vector ) {
- this.x = (this.x || 0) + vector.x;
- this.y = (this.y || 0) + vector.y;
- this.t = (this.t || 0) + vector.t;
- return this;
- },
- calc: function() {
- // The total distance (delta S, hypotenuse) - this is the total distance in scalar form.
- this.length = Math.sqrt( Math.pow( this.x, 2 ) + Math.pow( this.y, 2 ) );
- // Angle in radians.
- this.angle = Angle.fromVector( this );
- return this;
- },
- diff: function( prop ) {
- this[prop] = this.p1[prop] - this.p2[prop];
- return this;
- }
- };
- $( document ).mousemove(function( e ) {
- // Add to stack.
- stack.push( Point.fromEvent( e ) );
- var length = stack.length, i = length - 1,
- first = stack[i],
- vector = new Vector, add,
- quadrant = Math.PI / 2;
- for ( ; i > -1; i-- ) {
- if ( i === 0 /* last one ( captures first if it's alone ), or: */ || (
- // time diff between i & first too long, or:
- stack[i].t < first.t - $.mouseMovement.clearInterval ||
- // {add} is within the opposite quadrant of {vector}.
- (add = new Vector( stack[i], stack[i - 1] )) && vector.angle && vector.angle.isOpposite( Angle.fromVector( add ), quadrant )
- ) && stack.splice( 0, i + 1 )
- ) {
- // Match! Avoid vector-addition;
- break;
- }
- vector.add( add ).calc();
- }
- console.log( vector.angle ? [Math.round( vector.angle.toDeg() ), vector] : false );
- });
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement