-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyrocopter.js
187 lines (156 loc) · 4.6 KB
/
gyrocopter.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(function (window, undefined) {
var Gyrocopter = (function(module) {
// protected variables
var document = window.document;
var init = false;
var gyroscope;
var accelerometer;
var keyboard = [];
var queue = {};
queue.motion = [];
queue.keyboard = [];
// public variables
module.TIME_STEP = 15;
// public functions
module.motion = function(map) {
// create input object and add to motion queue
var input = {
time: Date.now(),
gyroscope: gyroscope,
accelerometer: accelerometer
};
// add input to queue, then trim queue to max size
queue.motion.unshift(input);
queue.motion = queue.motion.slice(0, 10);
var last = queue.motion.length - 1;
var now = queue.motion[0].gyroscope;
var then = queue.motion[last].gyroscope;
if (now && then) {
// alpha, beta, gamma
var delta = [
now.alpha - then.alpha,
now.beta - then.beta,
now.gamma - then.gamma
];
var length = delta.length;
for (var i = 0; i < length; i++) {
var value = delta[i];
if (Math.abs(value) > 1) {
var index = (value >= 0 ? 0 : 1);
var event = map[i][index];
if (event) {
document.dispatchEvent(new CustomEvent('gyrocopter', {
detail: {
direction: event
}
}));
}
}
}
}
};
module.keyboard = function(map) {
// create input object and add to keyboard queue
var input = {
time: Date.now(),
keyboard: keyboard
};
// add input to queue, then trim queue to max size
queue.keyboard.unshift(input);
queue.keyboard = queue.keyboard.slice(0, 10);
var keys = Object.keys(map);
var length = keys.length;
for (var i = 0; i < length; i++) {
var event = keys[i];
var keydown = (function() {
var keybind = map[event];
for (var j = 0; j < keybind.length; j++) {
var key = keybind[j];
if (queue.keyboard[0]['keyboard'][key]) {
return true;
}
}
})();
if (keydown) {
document.dispatchEvent(new CustomEvent('gyrocopter', {
detail: {
direction: event
}
}));
}
}
};
// constructor
module.constructor = {};
module.constructor.gyroscope = function(callback) {
window.ondeviceorientation = function(event) {
gyroscope = event;
};
window.ondevicemotion = function(event) {
accelerometer = event;
};
if (typeof callback === 'function') {
callback();
}
};
module.constructor.keyboard = function(callback) {
window.onkeydown = function(event) {
var key = event.which || event.keyCode;
keyboard[key] = true;
};
window.onkeyup = function(event) {
var key = event.which || event.keyCode;
keyboard[key] = false;
};
if (typeof callback === 'function') {
callback();
}
};
// factory
module.factory = (function() {
var Worker = function(options) {
var self = this;
self.args = Array.prototype.slice.call(arguments[0]);
self.options = this.args.shift();
// options
self.map = self.options.map;
self.func = self.options.func;
self.step = self.options.step || module.TIME_STEP;
// constructor
var constructor = module.constructor[self.options.constructor];
// callback
var callback = self.options.callback || function() {
// TODO: do this in a web worker if available?
// init input loop, fixed time step in milliseconds
setInterval(function() {
module[self.func](self.map);
}, self.step);
};
if (constructor) {
constructor(callback);
} else if (typeof callback === 'function') {
callback();
}
return self;
};
return function() {
return new Worker(arguments);
};
})();
// return module
return module;
// loose augmentation
})(window.Gyrocopter || {});
// expose module to global scope
window.Gyrocopter = Gyrocopter;
// AMD module support
if (typeof define === 'function' && define.amd) {
define('gyrocopter', [], function() { return Gyrocopter; });
}
})(window);
/*
window.Gyrocopter = (function(module) {
module.input = function() {};
return module;
})(window.Gyrocopter || {});
*/