Skip to content

Instantly share code, notes, and snippets.

@motionharvest
Created May 20, 2016 02:25
Show Gist options
  • Save motionharvest/b66d3dcd0f365a308f9ce9863db7a1ed to your computer and use it in GitHub Desktop.
Save motionharvest/b66d3dcd0f365a308f9ce9863db7a1ed to your computer and use it in GitHub Desktop.
Robotjs Script for moving the mouse around in a circle depending on a given degree
var Mathutils = {
//not so important
normalize: function ($value, $min, $max) {
return ($value - $min) / ($max - $min);
},
interpolate: function ($normValue, $min, $max) {
return $min + ($max - $min) * $normValue;
},
/*
really important. It's pretty cool
1. Set the boundaries
If less, make it the lower boundary
If more, make it the higher boundary
2. Normalize figures out the percentage
between the two first values
Q: 5 is what percentage between 2 and 8?
A:
(5 - 2) / (8 - 2)
3 / 6 = .5
.5 * 100 = 50% (decimal form is preferred, so this step is omitted)
3. Interpolate to figure out what the normalized value
is between the second set of values
Q: What is 50% beteween 30 and 100?
A:
30 + (100 - 30) * .5;
30 + 70 * .5;
30 + 35;
65
*/
map: function ($value, $min1, $max1, $min2, $max2) {
if ($value < $min1) {
$value = $min1;
}
if ($value > $max1) {
$value = $max1;
}
var res = this.interpolate(this.normalize($value, $min1, $max1), $min2, $max2);
return res;
}
};
//Move the mouse across the screen as a sine wave.
var robot = require("robotjs");
var raf = require("raf");
//Speed up the mouse.
robot.setMouseDelay(0);
var twoPI = Math.PI * 2.0;
var screenSize = robot.getScreenSize();
var height = screenSize.height;
var width = screenSize.width;
var origin = {
x: width / 2,
y: height / 2
};
var r = 200;
var x, y;
function updatePosition(deg) {
t = Mathutils.map(deg, 0, 360, 0, 6.3);
x = Math.floor(origin.x + (r * Math.cos(t)));
y = Math.floor(origin.y + (r * Math.sin(t)));
robot.moveMouse(x, y);
}
var d = 1;
function render() {
d += 2;
updatePosition(d);
if(d > 360) {
d = 1;
}
raf(render);
}
render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment