soiz1 commited on
Commit
d9bc37f
·
verified ·
1 Parent(s): d6663d5

Update eruda-orientation.js

Browse files
Files changed (1) hide show
  1. eruda-orientation.js +146 -215
eruda-orientation.js CHANGED
@@ -1,217 +1,148 @@
1
- ;(function (root, factory) {
2
- if (typeof define === 'function' && define.amd) {
3
- define([], factory)
4
- } else if (typeof module === 'object' && module.exports) {
5
- module.exports = factory()
6
- } else {
7
- root.erudaOrientation = factory()
8
  }
9
- })(this, function () {
10
- return function (eruda) {
11
- var Tool = eruda.Tool
12
- var util = eruda.util
13
-
14
- var Orientation = Tool.extend({
15
- name: 'orientation',
16
- displayName: 'デバイスの回転',
17
- init: function ($el) {
18
- this.callSuper(Tool, 'init', arguments)
19
- this._style = util.evalCss(
20
- [
21
- '.eruda-dev-tools .eruda-tools .eruda-orientation {padding: 10px; overflow-y: auto; -webkit-overflow-scrolling: touch;}',
22
- '.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;}',
23
- '.eruda-cube {width: 150px; height: 150px; color: var(--foreground); position: relative; margin: 50px auto; -webkit-transform-style: preserve-3d; transform-style: preserve-3d;}',
24
- '.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; }',
25
- '.eruda-one {-webkit-transform: translateZ(75px); transform: translateZ(75px);} ',
26
- '.eruda-two {-webkit-transform: rotateY(90deg) translateZ(75px); transform: rotateY(90deg) translateZ(75px);} ',
27
- '.eruda-three {-webkit-transform: rotateY(180deg) translateZ(75px); transform: rotateY(180deg) translateZ(75px);} ',
28
- '.eruda-four {-webkit-transform: rotateY(-90deg) translateZ(75px); transform: rotateY(-90deg) translateZ(75px);} ',
29
- '.eruda-five {-webkit-transform: rotateX(90deg) translateZ(75px); transform: rotateX(90deg) translateZ(75px);} ',
30
- '.eruda-six {-webkit-transform: rotateX(-90deg) translateZ(75px) rotate(0deg); transform: rotateX(-90deg) translateZ(75px) rotate(0deg);} ',
31
- '.eruda-orientation-data {margin: 10px;} ',
32
- '.eruda-content {padding: 10px;} ',
33
- 'table {width: 100%;} ',
34
- 'table td {border: 1px solid var(--border); padding: 10px;} ',
35
- '.eruda-key {width: 150px;} ',
36
- 'sup {vertical-align: super; font-size: smaller;} ',
37
- ].join('.eruda-dev-tools .eruda-tools .eruda-orientation ')
38
- )
39
- var isSupported =
40
- window.DeviceOrientationEvent && window.DeviceMotionEvent
41
-
42
- if (!isSupported) {
43
- $el.html(
44
- '<div class="eruda-not-supported">Not supported for this browser!</div>'
45
- )
46
- } else {
47
- $el.html(
48
- [
49
- '<div class="eruda-cube" class="cube">',
50
- ' <div class="eruda-face eruda-one">1</div>',
51
- ' <div class="eruda-face eruda-two">2</div>',
52
- ' <div class="eruda-face eruda-three">3</div>',
53
- ' <div class="eruda-face eruda-four">4</div>',
54
- ' <div class="eruda-face eruda-five">5</div>',
55
- ' <div class="eruda-face eruda-six">6</div>',
56
- '</div>',
57
- '<div class="eruda-orientation-data">',
58
- ' <div class="eruda-content">',
59
- ' <table>',
60
- ' <tbody>',
61
- ' <tr>',
62
- ' <td class="eruda-key">coordinates</td>',
63
- ' <td class="eruda-coordinates">unknown</td>',
64
- ' </tr>',
65
- ' <tr>',
66
- ' <td class="eruda-key">acceleration</td>',
67
- ' <td class="eruda-acceleration">unknown</td>',
68
- ' </tr>',
69
- ' <tr>',
70
- ' <td class="eruda-key">acceleration including gravity</td>',
71
- ' <td class="eruda-acceleration-including-gravity">unknown</td>',
72
- ' </tr>',
73
- ' <tr>',
74
- ' <td class="eruda-key">rotation rate</td>',
75
- ' <td class="eruda-rotation-rate">unknown</td>',
76
- ' </tr>',
77
- ' <tr>',
78
- ' <td class="eruda-key">interval</td>',
79
- ' <td class="eruda-interval">unknown</td>',
80
- ' </tr>',
81
- ' </tbody>',
82
- ' </table>',
83
- ' </div>',
84
- '</div>',
85
- ].join('')
86
- )
87
- this._$cube = $el.find('.eruda-cube')
88
- this._$coordinates = $el.find('.eruda-coordinates')
89
- this._$acceleration = $el.find('.eruda-acceleration')
90
- this._$accGravity = $el.find('.eruda-acceleration-including-gravity')
91
- this._$rotationRate = $el.find('.eruda-rotation-rate')
92
- this._$interval = $el.find('.eruda-interval')
93
- this._bindEvent()
94
- }
95
- },
96
- _bindEvent: function () {
97
- var $cube = this._$cube
98
- var $coordinates = this._$coordinates
99
- var $acceleration = this._$acceleration
100
- var $accGravity = this._$accGravity
101
- var $rotationRate = this._$rotationRate
102
- var $interval = this._$interval
103
-
104
- var self = this
105
- this._onDeviceorientation = function (e) {
106
- if (!self._isShow) return
107
-
108
- $cube.css(
109
- 'transform',
110
- 'rotateX(' +
111
- e.beta +
112
- 'deg) ' +
113
- 'rotateY(' +
114
- e.gamma +
115
- 'deg) ' +
116
- 'rotateZ(' +
117
- e.alpha +
118
- 'deg)'
119
- )
120
-
121
- $coordinates.text(
122
- '(' +
123
- Math.round(e.beta) +
124
- ', ' +
125
- Math.round(e.gamma) +
126
- ', ' +
127
- Math.round(e.alpha) +
128
- ')'
129
- )
130
- }
131
- this._onDevicemotion = function (e) {
132
- if (!self._isShow) return
133
-
134
- var acceleration = e.acceleration
135
- $acceleration.html(
136
- '(' +
137
- Math.round(acceleration.x) +
138
- ', ' +
139
- Math.round(acceleration.y) +
140
- ', ' +
141
- Math.round(acceleration.z) +
142
- ') m/s<sup>2</sup>'
143
- )
144
-
145
- var accGravity = e.accelerationIncludingGravity
146
- $accGravity.html(
147
- '(' +
148
- Math.round(accGravity.x) +
149
- ', ' +
150
- Math.round(accGravity.y) +
151
- ', ' +
152
- Math.round(accGravity.z) +
153
- ') m/s<sup>2</sup>'
154
- )
155
-
156
- var rotationRate = e.rotationRate
157
- $rotationRate.text(
158
- '(' +
159
- Math.round(rotationRate.beta) +
160
- ', ' +
161
- Math.round(rotationRate.gamma) +
162
- ', ' +
163
- Math.round(rotationRate.alpha) +
164
- ')'
165
- )
166
-
167
- $interval.text(e.interval + 'ms')
168
- }
169
-
170
- function bind(type) {
171
- var Event = DeviceOrientationEvent
172
- var eventName = 'deviceorientation'
173
- var listener = self._onDeviceorientation
174
- if (type === 'motion') {
175
- Event = DeviceMotionEvent
176
- eventName = 'devicemotion'
177
- listener = self._onDevicemotion
178
- }
179
-
180
- function requestPermission() {
181
- Event.requestPermission().then(function (response) {
182
- if (response === 'granted') {
183
- $cube.off('click', requestPermission)
184
- }
185
- })
186
- }
187
- if (Event.requestPermission) {
188
- $cube.on('click', requestPermission)
189
- }
190
- window.addEventListener(eventName, listener)
191
- }
192
-
193
- bind('orientation')
194
- bind('motion')
195
- },
196
- show: function () {
197
- this.callSuper(Tool, 'show', arguments)
198
- this._isShow = true
199
- },
200
- hide: function () {
201
- this.callSuper(Tool, 'hide', arguments)
202
- this._isShow = false
203
- },
204
- destroy: function () {
205
- this.callSuper(Tool, 'destroy', arguments)
206
- util.evalCss.remove(this._style)
207
- window.removeEventListener(
208
- 'deviceorientation',
209
- this._onDeviceorientation
210
- )
211
- window.removeEventListener('devicemotion', this._onDevicemotion)
212
- },
213
- })
214
-
215
- return Orientation
216
  }
217
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Orientation extends eruda.Tool {
2
+ constructor() {
3
+ super()
4
+ this.name = 'orientation'
5
+ this.displayName = 'デバイスの回転'
 
 
6
  }
7
+
8
+ init($el) {
9
+ super.init($el)
10
+ this._style = eruda.util.evalCss(
11
+ [
12
+ '.eruda-dev-tools .eruda-tools .eruda-orientation {',
13
+ ' padding: 10px;',
14
+ ' font-family: Arial, sans-serif;',
15
+ '}',
16
+ '.eruda-cube {',
17
+ ' width: 100px;',
18
+ ' height: 100px;',
19
+ ' margin: 10px auto;',
20
+ ' background: #eee;',
21
+ ' border: 1px solid #ccc;',
22
+ ' transform-style: preserve-3d;',
23
+ ' transition: transform 0.3s ease;',
24
+ '}',
25
+ '.eruda-coordinates, .eruda-acceleration, .eruda-acceleration-including-gravity, .eruda-rotation-rate, .eruda-interval {',
26
+ ' margin-top: 5px;',
27
+ ' font-size: 12px;',
28
+ ' color: #333;',
29
+ '}',
30
+ '.eruda-not-supported {',
31
+ ' color: red;',
32
+ ' text-align: center;',
33
+ ' padding: 20px;',
34
+ '}',
35
+ ].join('')
36
+ )
37
+
38
+ const isSupported =
39
+ window.DeviceOrientationEvent && window.DeviceMotionEvent
40
+
41
+ if (!isSupported) {
42
+ $el.html(
43
+ '<div class="eruda-not-supported">Not supported for this browser!</div>'
44
+ )
45
+ } else {
46
+ $el.html(
47
+ [
48
+ '<div class="eruda-cube"></div>',
49
+ '<div class="eruda-coordinates">Alpha: <span class="alpha">0</span>, Beta: <span class="beta">0</span>, Gamma: <span class="gamma">0</span></div>',
50
+ '<div class="eruda-acceleration">Acceleration: <span class="x">0</span>, <span class="y">0</span>, <span class="z">0</span></div>',
51
+ '<div class="eruda-acceleration-including-gravity">Acceleration Including Gravity: <span class="xg">0</span>, <span class="yg">0</span>, <span class="zg">0</span></div>',
52
+ '<div class="eruda-rotation-rate">Rotation Rate: <span class="alpha-rate">0</span>, <span class="beta-rate">0</span>, <span class="gamma-rate">0</span></div>',
53
+ '<div class="eruda-interval">Interval: <span class="interval">0</span></div>',
54
+ ].join('')
55
+ )
56
+ this._$cube = $el.find('.eruda-cube')
57
+ this._$coordinates = {
58
+ alpha: $el.find('.alpha'),
59
+ beta: $el.find('.beta'),
60
+ gamma: $el.find('.gamma'),
61
+ }
62
+ this._$acceleration = {
63
+ x: $el.find('.eruda-acceleration .x'),
64
+ y: $el.find('.eruda-acceleration .y'),
65
+ z: $el.find('.eruda-acceleration .z'),
66
+ }
67
+ this._$accGravity = {
68
+ xg: $el.find('.eruda-acceleration-including-gravity .xg'),
69
+ yg: $el.find('.eruda-acceleration-including-gravity .yg'),
70
+ zg: $el.find('.eruda-acceleration-including-gravity .zg'),
71
+ }
72
+ this._$rotationRate = {
73
+ alpha: $el.find('.alpha-rate'),
74
+ beta: $el.find('.beta-rate'),
75
+ gamma: $el.find('.gamma-rate'),
76
+ }
77
+ this._$interval = $el.find('.interval')
78
+
79
+ this._bindEvent()
80
+ }
81
+ }
82
+
83
+ _bindEvent() {
84
+ this._onDeviceorientation = (event) => {
85
+ if (!this._isShow) return
86
+ const { alpha, beta, gamma } = event
87
+ this._$coordinates.alpha.text(alpha.toFixed(2))
88
+ this._$coordinates.beta.text(beta.toFixed(2))
89
+ this._$coordinates.gamma.text(gamma.toFixed(2))
90
+
91
+ // 回転キューブの回転
92
+ this._$cube.css(
93
+ 'transform',
94
+ `rotateZ(${alpha}deg) rotateX(${beta}deg) rotateY(${gamma}deg)`
95
+ )
96
+ }
97
+
98
+ this._onDevicemotion = (event) => {
99
+ if (!this._isShow) return
100
+ const acc = event.acceleration
101
+ const accG = event.accelerationIncludingGravity
102
+ const rotRate = event.rotationRate
103
+ const interval = event.interval
104
+
105
+ if (acc) {
106
+ this._$acceleration.x.text(acc.x ? acc.x.toFixed(2) : '0')
107
+ this._$acceleration.y.text(acc.y ? acc.y.toFixed(2) : '0')
108
+ this._$acceleration.z.text(acc.z ? acc.z.toFixed(2) : '0')
109
+ }
110
+
111
+ if (accG) {
112
+ this._$accGravity.xg.text(accG.x ? accG.x.toFixed(2) : '0')
113
+ this._$accGravity.yg.text(accG.y ? accG.y.toFixed(2) : '0')
114
+ this._$accGravity.zg.text(accG.z ? accG.z.toFixed(2) : '0')
115
+ }
116
+
117
+ if (rotRate) {
118
+ this._$rotationRate.alpha.text(rotRate.alpha ? rotRate.alpha.toFixed(2) : '0')
119
+ this._$rotationRate.beta.text(rotRate.beta ? rotRate.beta.toFixed(2) : '0')
120
+ this._$rotationRate.gamma.text(rotRate.gamma ? rotRate.gamma.toFixed(2) : '0')
121
+ }
122
+
123
+ this._$interval.text(interval ? interval.toFixed(2) : '0')
124
+ }
125
+
126
+ window.addEventListener('deviceorientation', this._onDeviceorientation)
127
+ window.addEventListener('devicemotion', this._onDevicemotion)
128
+ }
129
+
130
+ show() {
131
+ super.show()
132
+ this._isShow = true
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  }
134
+
135
+ hide() {
136
+ super.hide()
137
+ this._isShow = false
138
+ }
139
+
140
+ destroy() {
141
+ super.destroy()
142
+ eruda.util.evalCss.remove(this._style)
143
+ window.removeEventListener('deviceorientation', this._onDeviceorientation)
144
+ window.removeEventListener('devicemotion', this._onDevicemotion)
145
+ }
146
+ }
147
+
148
+ module.exports = Orientation