forked from bkase/joyfull
-
Notifications
You must be signed in to change notification settings - Fork 1
/
joyfull.js
36 lines (31 loc) · 1.03 KB
/
joyfull.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
var Joystick = function(file, pollFrequency, onEvent){
if(!this.__proto__.addEvent){
console.log("must be called with new");
return;
}
var pluginObj = document.createElement('object');
pluginObj.id = "joystickPlugin";
pluginObj.type = "application/x-joyfull";
pluginObj.width = "0";
pluginObj.height = "0";
document.body.appendChild(pluginObj);
var joystickPlugin = this.getJoystickPlugin();
this.addEvent(joystickPlugin, 'joystickData', function(joyButton){
if (joyButton.button_type)
onEvent(joyButton.button_type, joyButton.button_value);
});
joystickPlugin.init(file)
setInterval(function() {
joystickPlugin.poll();
}, pollFrequency);
}
Joystick.prototype.getJoystickPlugin = function(){
return document.getElementById('joystickPlugin');
}
Joystick.prototype.addEvent = function(obj, name, func){
if (window.addEventListener) {
obj.addEventListener(name, func, false);
} else {
obj.attachEvent("on"+name, func);
}
}