Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Dumping old code as gists...
  2.  
  3. (function($) {
  4. $.mouseMovement = {
  5. stack: [],
  6. clearInterval: 1000
  7. };
  8.  
  9. var stack = $.mouseMovement.stack;
  10.  
  11. /*
  12. * Point
  13. * -------------------------
  14. */
  15. var Point = function( x, y, t ) {
  16. this.x = x;
  17. this.y = y;
  18. this.t = t || $.now();
  19. }
  20. Point.fromEvent = function( e ) {
  21. return new Point( e.pageX, e.pageY, e.timeStamp );
  22. };
  23.  
  24. /*
  25. * ANGLE
  26. * -------------------------
  27. */
  28. var Angle = function( val ) {
  29. this.val = val;
  30. };
  31. $.extend( Angle, {
  32. fromVector: function( vector ) {
  33. return (new Angle( Math.atan2( vector.y, vector.x ) )).normalize();
  34. },
  35. fromDeg: function( deg ) {
  36. return new Angle( Math.PI / 180 * deg );
  37. }
  38. });
  39. Angle.prototype = {
  40. MOD_CIRCLE: 2 * Math.PI,
  41. normalize: function() {
  42. this.val = (this.val + this.MOD_CIRCLE) % this.MOD_CIRCLE;
  43. return this;
  44. },
  45. isOpposite: function( angle, boundingInterval ) {
  46. var min = (new Angle( angle.val + Math.PI - boundingInterval / 2 )).normalize(),
  47. max = (new Angle( angle.val + Math.PI + boundingInterval / 2 )).normalize();
  48.  
  49. return this.normalize().val >= min.val && this.val <= max.val;
  50. },
  51. toDeg: function() {
  52. return 180 / Math.PI * this.val;
  53. }
  54. };
  55.  
  56. /*
  57. * Vector
  58. * -------------------------
  59. */
  60. var Vector = function( p1, p2 ) {
  61. if ( p1 && p2 ) {
  62. this.p1 = p1;
  63. this.p2 = p2;
  64.  
  65. // Delta Time, X, Y -> calculate!.
  66. this.diff( 't' ).diff( 'x' ).diff( 'y' ).calc();
  67. }
  68. };
  69. Vector.prototype = {
  70. add: function( vector ) {
  71. this.x = (this.x || 0) + vector.x;
  72. this.y = (this.y || 0) + vector.y;
  73. this.t = (this.t || 0) + vector.t;
  74.  
  75. return this;
  76. },
  77. calc: function() {
  78. // The total distance (delta S, hypotenuse) - this is the total distance in scalar form.
  79. this.length = Math.sqrt( Math.pow( this.x, 2 ) + Math.pow( this.y, 2 ) );
  80.  
  81. // Angle in radians.
  82. this.angle = Angle.fromVector( this );
  83.  
  84. return this;
  85. },
  86. diff: function( prop ) {
  87. this[prop] = this.p1[prop] - this.p2[prop];
  88. return this;
  89. }
  90. };
  91.  
  92. $( document ).mousemove(function( e ) {
  93.  
  94. // Add to stack.
  95. stack.push( Point.fromEvent( e ) );
  96.  
  97. var length = stack.length, i = length - 1,
  98. first = stack[i],
  99. vector = new Vector, add,
  100. quadrant = Math.PI / 2;
  101.  
  102. for ( ; i > -1; i-- ) {
  103.  
  104. if ( i === 0 /* last one ( captures first if it's alone ), or: */ || (
  105. // time diff between i & first too long, or:
  106. stack[i].t < first.t - $.mouseMovement.clearInterval ||
  107. // {add} is within the opposite quadrant of {vector}.
  108. (add = new Vector( stack[i], stack[i - 1] )) && vector.angle && vector.angle.isOpposite( Angle.fromVector( add ), quadrant )
  109. ) && stack.splice( 0, i + 1 )
  110. ) {
  111. // Match! Avoid vector-addition;
  112. break;
  113. }
  114.  
  115. vector.add( add ).calc();
  116. }
  117.  
  118. console.log( vector.angle ? [Math.round( vector.angle.toDeg() ), vector] : false );
  119. });
  120.  
  121. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement