Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/controllers/OrientationController.js
4627 views
1
// Generated by CoffeeScript 1.4.0
2
3
/*
4
OrientationController (Orientation + buttons) for touch devices
5
6
@class bkcore.OrientationController
7
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
8
*/
9
10
11
(function() {
12
var OrientationController, exports, _base;
13
14
OrientationController = (function() {
15
16
OrientationController.isCompatible = function() {
17
return 'DeviceOrientationEvent' in window;
18
};
19
20
/*
21
Creates a new OrientationController
22
23
@param dom DOMElement The element that will listen to touch events
24
@param registerTouch bool Enable touch detection
25
@param touchCallback function Callback for touches
26
*/
27
28
29
function OrientationController(dom, registerTouch, touchCallback) {
30
var _this = this;
31
this.dom = dom;
32
this.registerTouch = registerTouch != null ? registerTouch : true;
33
this.touchCallback = touchCallback != null ? touchCallback : null;
34
this.active = true;
35
this.alpha = 0.0;
36
this.beta = 0.0;
37
this.gamma = 0.0;
38
this.dalpha = null;
39
this.dbeta = null;
40
this.dgamma = null;
41
this.touches = null;
42
window.addEventListener('deviceorientation', (function(e) {
43
return _this.orientationChange(e);
44
}), false);
45
if (this.registerTouch) {
46
this.dom.addEventListener('touchstart', (function(e) {
47
return _this.touchStart(e);
48
}), false);
49
this.dom.addEventListener('touchend', (function(e) {
50
return _this.touchEnd(e);
51
}), false);
52
}
53
}
54
55
/*
56
@private
57
*/
58
59
60
OrientationController.prototype.orientationChange = function(event) {
61
if (!this.active) {
62
return;
63
}
64
if (this.dalpha === null) {
65
console.log("calbrate", event.beta);
66
this.dalpha = event.alpha;
67
this.dbeta = event.beta;
68
this.dgamma = event.gamma;
69
}
70
this.alpha = event.alpha - this.dalpha;
71
this.beta = event.beta - this.dbeta;
72
this.gamma = event.gamma - this.dgamma;
73
return false;
74
};
75
76
/*
77
@private
78
*/
79
80
81
OrientationController.prototype.touchStart = function(event) {
82
var touch, _i, _len, _ref;
83
if (!this.active) {
84
return;
85
}
86
_ref = event.changedTouches;
87
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
88
touch = _ref[_i];
89
if (typeof this.touchCallback === "function") {
90
this.touchCallback(true, touch, event);
91
}
92
}
93
this.touches = event.touches;
94
return false;
95
};
96
97
/*
98
@private
99
*/
100
101
102
OrientationController.prototype.touchEnd = function(event) {
103
var touch, _i, _len, _ref;
104
if (!this.active) {
105
return;
106
}
107
_ref = event.changedTouches;
108
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
109
touch = _ref[_i];
110
if (typeof this.touchCallback === "function") {
111
this.touchCallback(true, touch, event);
112
}
113
}
114
this.touches = event.touches;
115
return false;
116
};
117
118
return OrientationController;
119
120
})();
121
122
exports = exports != null ? exports : this;
123
124
exports.bkcore || (exports.bkcore = {});
125
126
(_base = exports.bkcore).controllers || (_base.controllers = {});
127
128
exports.bkcore.controllers.OrientationController = OrientationController;
129
130
}).call(this);
131
132