-
Notifications
You must be signed in to change notification settings - Fork 27
/
history.js
70 lines (61 loc) · 1.42 KB
/
history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
const HISTORY_MAX = 500;
const SHAKE_DEGREES = 500;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const math = Me.imports.math;
let history = [];
var lastX = 0;
var lastY = 0;
var threshold = 300;
/**
* Check history and report whether the mouse is jiggling.
*
* @return {Boolean}
*/
function check()
{
// get the current loop timestamp
let now = new Date().getTime();
// prune stale buffer
for (let i = 0; i < history.length; ++i) {
if (now - history[i].t > HISTORY_MAX) {
history.splice(i, 1);
}
}
// reset degrees so we can add them again
let degrees = 0;
let max = 0;
// add up gammas (deg=sum(gamma))
if (history.length > 2) {
for (let i = 2; i < history.length; ++i) {
degrees += math.gamma(history[i], history[i-1], history[i-2]);
max = Math.max(max, math.distance(history[i-2], history[i-1]), math.distance(history[i-1], history[i]));
}
}
return (degrees > SHAKE_DEGREES && max > threshold);
}
/**
* Clear the history.
*/
function clear()
{
lastX = 0;
lastY = 0;
history = [];
}
/**
* Push new mouse coordinates to the history.
*
* @param {Number} x
* @param {Number} y
*/
function push(x, y)
{
if (x == 0 && y == 0) {
return;
}
if (x < 0 || y < 0) {
return;
}
history.push({x: lastX = x, y: lastY = y, t: new Date().getTime()});
}