Spaces:
Sleeping
Sleeping
File size: 5,004 Bytes
d9bc37f 684d847 d9bc37f d6663d5 d9bc37f |
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 |
class Orientation extends eruda.Tool {
constructor() {
super()
this.name = 'orientation'
this.displayName = 'デバイスの回転'
}
init($el) {
super.init($el)
this._style = eruda.util.evalCss(
[
'.eruda-dev-tools .eruda-tools .eruda-orientation {',
' padding: 10px;',
' font-family: Arial, sans-serif;',
'}',
'.eruda-cube {',
' width: 100px;',
' height: 100px;',
' margin: 10px auto;',
' background: #eee;',
' border: 1px solid #ccc;',
' transform-style: preserve-3d;',
' transition: transform 0.3s ease;',
'}',
'.eruda-coordinates, .eruda-acceleration, .eruda-acceleration-including-gravity, .eruda-rotation-rate, .eruda-interval {',
' margin-top: 5px;',
' font-size: 12px;',
' color: #333;',
'}',
'.eruda-not-supported {',
' color: red;',
' text-align: center;',
' padding: 20px;',
'}',
].join('')
)
const 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"></div>',
'<div class="eruda-coordinates">Alpha: <span class="alpha">0</span>, Beta: <span class="beta">0</span>, Gamma: <span class="gamma">0</span></div>',
'<div class="eruda-acceleration">Acceleration: <span class="x">0</span>, <span class="y">0</span>, <span class="z">0</span></div>',
'<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>',
'<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>',
'<div class="eruda-interval">Interval: <span class="interval">0</span></div>',
].join('')
)
this._$cube = $el.find('.eruda-cube')
this._$coordinates = {
alpha: $el.find('.alpha'),
beta: $el.find('.beta'),
gamma: $el.find('.gamma'),
}
this._$acceleration = {
x: $el.find('.eruda-acceleration .x'),
y: $el.find('.eruda-acceleration .y'),
z: $el.find('.eruda-acceleration .z'),
}
this._$accGravity = {
xg: $el.find('.eruda-acceleration-including-gravity .xg'),
yg: $el.find('.eruda-acceleration-including-gravity .yg'),
zg: $el.find('.eruda-acceleration-including-gravity .zg'),
}
this._$rotationRate = {
alpha: $el.find('.alpha-rate'),
beta: $el.find('.beta-rate'),
gamma: $el.find('.gamma-rate'),
}
this._$interval = $el.find('.interval')
this._bindEvent()
}
}
_bindEvent() {
this._onDeviceorientation = (event) => {
if (!this._isShow) return
const { alpha, beta, gamma } = event
this._$coordinates.alpha.text(alpha.toFixed(2))
this._$coordinates.beta.text(beta.toFixed(2))
this._$coordinates.gamma.text(gamma.toFixed(2))
// 回転キューブの回転
this._$cube.css(
'transform',
`rotateZ(${alpha}deg) rotateX(${beta}deg) rotateY(${gamma}deg)`
)
}
this._onDevicemotion = (event) => {
if (!this._isShow) return
const acc = event.acceleration
const accG = event.accelerationIncludingGravity
const rotRate = event.rotationRate
const interval = event.interval
if (acc) {
this._$acceleration.x.text(acc.x ? acc.x.toFixed(2) : '0')
this._$acceleration.y.text(acc.y ? acc.y.toFixed(2) : '0')
this._$acceleration.z.text(acc.z ? acc.z.toFixed(2) : '0')
}
if (accG) {
this._$accGravity.xg.text(accG.x ? accG.x.toFixed(2) : '0')
this._$accGravity.yg.text(accG.y ? accG.y.toFixed(2) : '0')
this._$accGravity.zg.text(accG.z ? accG.z.toFixed(2) : '0')
}
if (rotRate) {
this._$rotationRate.alpha.text(rotRate.alpha ? rotRate.alpha.toFixed(2) : '0')
this._$rotationRate.beta.text(rotRate.beta ? rotRate.beta.toFixed(2) : '0')
this._$rotationRate.gamma.text(rotRate.gamma ? rotRate.gamma.toFixed(2) : '0')
}
this._$interval.text(interval ? interval.toFixed(2) : '0')
}
window.addEventListener('deviceorientation', this._onDeviceorientation)
window.addEventListener('devicemotion', this._onDevicemotion)
}
show() {
super.show()
this._isShow = true
}
hide() {
super.hide()
this._isShow = false
}
destroy() {
super.destroy()
eruda.util.evalCss.remove(this._style)
window.removeEventListener('deviceorientation', this._onDeviceorientation)
window.removeEventListener('devicemotion', this._onDevicemotion)
}
}
module.exports = Orientation
|