Advertisement
Guest User

Agario Movement Keys

a guest
May 8th, 2016
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Agario Movement Keys
  3. // @description  Use arrow keys or WASD to move. E to eject mass.
  4. // @version      0.4
  5. // @match        http://agar.io/
  6. // @grant        none
  7. // @noframes
  8. // ==/UserScript==
  9.  
  10. (function overwriteCanvasLoop() {
  11.     'use strict';
  12.  
  13.     var canvas = document.getElementById('canvas');
  14.     if (!canvas || !canvas.onmousemove) {
  15.         setTimeout(overwriteCanvasLoop, 100);
  16.         return;
  17.     }
  18.  
  19.     var endPoint = {
  20.         clientX: window.innerWidth / 2,
  21.         clientY: window.innerHeight / 2
  22.     };
  23.  
  24.     var handleKeys = (function() {
  25.         var keys = [ 37, 38, 39, 40, 65, 87, 68, 83 ];
  26.         var key = {
  27.             left: 'keyup',
  28.             up: 'keyup',
  29.             right: 'keyup',
  30.             down: 'keyup'
  31.         };
  32.         ['blur', 'resize'].forEach(function(listener) {
  33.             window.addEventListener(listener, function() {
  34.                 key.left = key.up = key.right = key.down = 'keyup';
  35.                 endPoint = {
  36.                     clientX: window.innerWidth / 2,
  37.                     clientY: window.innerHeight / 2
  38.                 };
  39.             }, false);
  40.         });
  41.         return function(event, keyState) {
  42.             if (event.repeat && keyState === 'keydown') {
  43.                 return;
  44.             }
  45.             for (var i = 0; i < keys.length; i++) {
  46.                 if (event.which !== keys[i]) continue;
  47.                 var axis = (i % 2) ? {
  48.                     dir: [ 'up', 'down' ],
  49.                     value: 'clientY'
  50.                 } : {
  51.                     dir: [ 'left', 'right' ],
  52.                     value: 'clientX'
  53.                 };
  54.                 var direction = ((i % 4) === 0 || ((i - 1) % 4) === 0) ? axis.dir[0] : axis.dir[1];
  55.                 if (key[direction] === keyState) {
  56.                     return;
  57.                 }
  58.                 key[direction] = keyState;
  59.                 var point = (keyState === 'keydown') ? 500 : -500;
  60.                 point = (direction === 'left' || direction === 'up') ? -point : point;
  61.                 return [ axis.value, point ];
  62.             }
  63.         };
  64.     })();
  65.  
  66.     ['keydown', 'keyup'].forEach(function(keyState) {
  67.         window.addEventListener(keyState, function(event) {
  68.             var data = handleKeys(event, keyState);
  69.             if (data && keyState === 'keyup') {
  70.                 var fraction = data[1] / 10;
  71.                 for (var i = 0; i < 2; i++) {
  72.                     endPoint[data[0]] += (fraction * (i ? 1 : 9));
  73.                     canvas.onmousemove(endPoint);
  74.                 }
  75.             } else if (data && keyState === 'keydown') {
  76.                 endPoint[data[0]] += data[1];
  77.                 canvas.onmousemove(endPoint);
  78.             }
  79.             if (event.which === 87) {
  80.                 event.stopPropagation();
  81.             } else if (event.which === 69) {
  82.                 window['on' + keyState]({ keyCode: 87 });
  83.             }
  84.         }, true);
  85.     });
  86. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement