-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
eruda-orientation.js
217 lines (204 loc) · 8.21 KB
/
eruda-orientation.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// https://code.tutsplus.com/tutorials/an-introduction-to-the-device-orientation-api--cms-21067
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof module === 'object' && module.exports) {
module.exports = factory()
} else {
root.erudaOrientation = factory()
}
})(this, function () {
return function (eruda) {
var Tool = eruda.Tool
var util = eruda.util
var Orientation = Tool.extend({
name: 'orientation',
init: function ($el) {
this.callSuper(Tool, 'init', arguments)
this._style = util.evalCss(
[
'.eruda-dev-tools .eruda-tools .eruda-orientation {padding: 10px; overflow-y: auto; -webkit-overflow-scrolling: touch;}',
'.eruda-not-supported {background: var(--console-error-background); color: var(--console-error-foreground); border: 1px solid var(--console-error-border);padding: 10px; text-align: center;}',
'.eruda-cube {width: 150px; height: 150px; color: var(--foreground); position: relative; margin: 50px auto; -webkit-transform-style: preserve-3d; transform-style: preserve-3d;}',
'.eruda-face {width: 150px; height: 150px; position: absolute; font-size: 80px; text-align: center; line-height: 150px; background-color: var(--accent); border: 1px solid var(--border); opacity: 0.6; }',
'.eruda-one {-webkit-transform: translateZ(75px); transform: translateZ(75px);}',
'.eruda-two {-webkit-transform: rotateY(90deg) translateZ(75px); transform: rotateY(90deg) translateZ(75px);}',
'.eruda-three {-webkit-transform: rotateY(180deg) translateZ(75px); transform: rotateY(180deg) translateZ(75px);}',
'.eruda-four {-webkit-transform: rotateY(-90deg) translateZ(75px); transform: rotateY(-90deg) translateZ(75px);}',
'.eruda-five {-webkit-transform: rotateX(90deg) translateZ(75px); transform: rotateX(90deg) translateZ(75px);}',
'.eruda-six {-webkit-transform: rotateX(-90deg) translateZ(75px) rotate(0deg); transform: rotateX(-90deg) translateZ(75px) rotate(0deg);}',
'.eruda-orientation-data {margin: 10px;}',
'.eruda-content {padding: 10px;}',
'table {width: 100%;}',
'table td {border: 1px solid var(--border); padding: 10px;}',
'.eruda-key {width: 150px;}',
'sup {vertical-align: super; font-size: smaller;}',
].join('.eruda-dev-tools .eruda-tools .eruda-orientation ')
)
var isSupported =
window.DeviceOrientationEvent && window.DeviceMotionEvent
if (!isSupported) {
$el.html(
'<div class="eruda-not-supported">Not supported for this browser!</div>'
)
} else {
$el.html(
[
'<div class="eruda-cube" class="cube">',
' <div class="eruda-face eruda-one">1</div>',
' <div class="eruda-face eruda-two">2</div>',
' <div class="eruda-face eruda-three">3</div>',
' <div class="eruda-face eruda-four">4</div>',
' <div class="eruda-face eruda-five">5</div>',
' <div class="eruda-face eruda-six">6</div>',
'</div>',
'<div class="eruda-orientation-data">',
' <div class="eruda-content">',
' <table>',
' <tbody>',
' <tr>',
' <td class="eruda-key">coordinates</td>',
' <td class="eruda-coordinates">unknown</td>',
' </tr>',
' <tr>',
' <td class="eruda-key">acceleration</td>',
' <td class="eruda-acceleration">unknown</td>',
' </tr>',
' <tr>',
' <td class="eruda-key">acceleration including gravity</td>',
' <td class="eruda-acceleration-including-gravity">unknown</td>',
' </tr>',
' <tr>',
' <td class="eruda-key">rotation rate</td>',
' <td class="eruda-rotation-rate">unknown</td>',
' </tr>',
' <tr>',
' <td class="eruda-key">interval</td>',
' <td class="eruda-interval">unknown</td>',
' </tr>',
' </tbody>',
' </table>',
' </div>',
'</div>',
].join('')
)
this._$cube = $el.find('.eruda-cube')
this._$coordinates = $el.find('.eruda-coordinates')
this._$acceleration = $el.find('.eruda-acceleration')
this._$accGravity = $el.find('.eruda-acceleration-including-gravity')
this._$rotationRate = $el.find('.eruda-rotation-rate')
this._$interval = $el.find('.eruda-interval')
this._bindEvent()
}
},
_bindEvent: function () {
var $cube = this._$cube
var $coordinates = this._$coordinates
var $acceleration = this._$acceleration
var $accGravity = this._$accGravity
var $rotationRate = this._$rotationRate
var $interval = this._$interval
var self = this
this._onDeviceorientation = function (e) {
if (!self._isShow) return
$cube.css(
'transform',
'rotateX(' +
e.beta +
'deg) ' +
'rotateY(' +
e.gamma +
'deg) ' +
'rotateZ(' +
e.alpha +
'deg)'
)
$coordinates.text(
'(' +
Math.round(e.beta) +
', ' +
Math.round(e.gamma) +
', ' +
Math.round(e.alpha) +
')'
)
}
this._onDevicemotion = function (e) {
if (!self._isShow) return
var acceleration = e.acceleration
$acceleration.html(
'(' +
Math.round(acceleration.x) +
', ' +
Math.round(acceleration.y) +
', ' +
Math.round(acceleration.z) +
') m/s<sup>2</sup>'
)
var accGravity = e.accelerationIncludingGravity
$accGravity.html(
'(' +
Math.round(accGravity.x) +
', ' +
Math.round(accGravity.y) +
', ' +
Math.round(accGravity.z) +
') m/s<sup>2</sup>'
)
var rotationRate = e.rotationRate
$rotationRate.text(
'(' +
Math.round(rotationRate.beta) +
', ' +
Math.round(rotationRate.gamma) +
', ' +
Math.round(rotationRate.alpha) +
')'
)
$interval.text(e.interval + 'ms')
}
function bind(type) {
var Event = DeviceOrientationEvent
var eventName = 'deviceorientation'
var listener = self._onDeviceorientation
if (type === 'motion') {
Event = DeviceMotionEvent
eventName = 'devicemotion'
listener = self._onDevicemotion
}
function requestPermission() {
Event.requestPermission().then(function (response) {
if (response === 'granted') {
$cube.off('click', requestPermission)
}
})
}
if (Event.requestPermission) {
$cube.on('click', requestPermission)
}
window.addEventListener(eventName, listener)
}
bind('orientation')
bind('motion')
},
show: function () {
this.callSuper(Tool, 'show', arguments)
this._isShow = true
},
hide: function () {
this.callSuper(Tool, 'hide', arguments)
this._isShow = false
},
destroy: function () {
this.callSuper(Tool, 'destroy', arguments)
util.evalCss.remove(this._style)
window.removeEventListener(
'deviceorientation',
this._onDeviceorientation
)
window.removeEventListener('devicemotion', this._onDevicemotion)
},
})
return new Orientation()
}
})