Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Agario Movement Keys
- // @description Use arrow keys or WASD to move. E to eject mass.
- // @version 0.4
- // @match http://agar.io/
- // @grant none
- // @noframes
- // ==/UserScript==
- (function overwriteCanvasLoop() {
- 'use strict';
- var canvas = document.getElementById('canvas');
- if (!canvas || !canvas.onmousemove) {
- setTimeout(overwriteCanvasLoop, 100);
- return;
- }
- var endPoint = {
- clientX: window.innerWidth / 2,
- clientY: window.innerHeight / 2
- };
- var handleKeys = (function() {
- var keys = [ 37, 38, 39, 40, 65, 87, 68, 83 ];
- var key = {
- left: 'keyup',
- up: 'keyup',
- right: 'keyup',
- down: 'keyup'
- };
- ['blur', 'resize'].forEach(function(listener) {
- window.addEventListener(listener, function() {
- key.left = key.up = key.right = key.down = 'keyup';
- endPoint = {
- clientX: window.innerWidth / 2,
- clientY: window.innerHeight / 2
- };
- }, false);
- });
- return function(event, keyState) {
- if (event.repeat && keyState === 'keydown') {
- return;
- }
- for (var i = 0; i < keys.length; i++) {
- if (event.which !== keys[i]) continue;
- var axis = (i % 2) ? {
- dir: [ 'up', 'down' ],
- value: 'clientY'
- } : {
- dir: [ 'left', 'right' ],
- value: 'clientX'
- };
- var direction = ((i % 4) === 0 || ((i - 1) % 4) === 0) ? axis.dir[0] : axis.dir[1];
- if (key[direction] === keyState) {
- return;
- }
- key[direction] = keyState;
- var point = (keyState === 'keydown') ? 500 : -500;
- point = (direction === 'left' || direction === 'up') ? -point : point;
- return [ axis.value, point ];
- }
- };
- })();
- ['keydown', 'keyup'].forEach(function(keyState) {
- window.addEventListener(keyState, function(event) {
- var data = handleKeys(event, keyState);
- if (data && keyState === 'keyup') {
- var fraction = data[1] / 10;
- for (var i = 0; i < 2; i++) {
- endPoint[data[0]] += (fraction * (i ? 1 : 9));
- canvas.onmousemove(endPoint);
- }
- } else if (data && keyState === 'keydown') {
- endPoint[data[0]] += data[1];
- canvas.onmousemove(endPoint);
- }
- if (event.which === 87) {
- event.stopPropagation();
- } else if (event.which === 69) {
- window['on' + keyState]({ keyCode: 87 });
- }
- }, true);
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement