-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (37 loc) · 1 KB
/
index.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
"use strict"
function Otolith (options) {
this._options = options || {}
this._options.axis = this._options.axis || 'x'
this._options.range = this._options.range || 180
// Look up the axis to use.
this._axis = { x: 'gamma', y: 'beta', z: 'alpha' }[this._options.axis]
this._handleOrientationChange = this._handleOrientationChange.bind(this)
// Start the event listening.
this.start()
}
Otolith.prototype = {
stop: function () {
window.removeEventListener(
'deviceorientation',
this._handleOrientationChange,
false
)
},
start: function () {
window.addEventListener(
'deviceorientation',
this._handleOrientationChange,
false
)
},
_handleOrientationChange: function (e) {
this._options.callback(this._calculateValue(e[this._axis]))
},
_calculateValue: function (raw) {
return _clamp((raw / this._options.range), -1, 1)
}
}
function _clamp (val, min, max) {
return Math.max(min, Math.min(max, val))
}
module.exports = Otolith