|
| 1 | +class DeviceOrientation extends HTMLElement { |
| 2 | + constructor() { |
| 3 | + super(); |
| 4 | + } |
| 5 | + |
| 6 | + connectedCallback() { |
| 7 | + if (!this.connected) { |
| 8 | + this.connected = true; |
| 9 | + this.setup(); |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + disconnectedCallback() { |
| 14 | + this.disable(); |
| 15 | + this.connected = false; |
| 16 | + } |
| 17 | + |
| 18 | + setup = async () => { |
| 19 | + this.attachShadow({mode: 'open'}); |
| 20 | + this.wrapper = document.createElement('div'); |
| 21 | + this.shadowRoot.append(this.wrapper); |
| 22 | + |
| 23 | + this.header = document.createElement('h3'); |
| 24 | + this.header.textContent = 'Device Orientation Control'; |
| 25 | + this.wrapper.appendChild(this.header); |
| 26 | + |
| 27 | + if (typeof DeviceOrientationEvent === 'undefined') { |
| 28 | + this.header.textContent = 'Device Orientation Not Supported On This Device!'; |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (typeof DeviceOrientationEvent.requestPermission === 'function') { |
| 33 | + this.permissionButton = document.createElement('input'); |
| 34 | + this.permissionButton.setAttribute('type','button'); |
| 35 | + this.permissionButton.textContent('Allow sensor access'); |
| 36 | + this.permissionButton.addEventListener('click', this.requestPermission); |
| 37 | + this.wrapper.appendChild(this.permissionButton); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + this.showEnable(); |
| 42 | + } |
| 43 | + |
| 44 | + requestPermission = async () => { |
| 45 | + const permissionState = await DeviceOrientationEvent.requestPermission() |
| 46 | + return permissionState === 'granted' ? this.permissionGranted() : this.permissionDenied(); |
| 47 | + } |
| 48 | + |
| 49 | + permissionGranted = () => { |
| 50 | + this.permissionButton.remove(); |
| 51 | + this.showEnable(); |
| 52 | + } |
| 53 | + |
| 54 | + permissionDenied = () => { |
| 55 | + this.header.textContent = 'Device Orientation Permission Denied!'; |
| 56 | + } |
| 57 | + |
| 58 | + showEnable = () => { |
| 59 | + this.enabled = document.createElement('input'); |
| 60 | + this.enabled.setAttribute('type','checkbox'); |
| 61 | + this.enabled.setAttribute('id','enabled'); |
| 62 | + this.wrapper.appendChild(this.enabled); |
| 63 | + |
| 64 | + this.enabledLabel = document.createElement('label'); |
| 65 | + this.enabledLabel.setAttribute('for','enabled'); |
| 66 | + this.enabledLabel.innerText = 'Disabled'; |
| 67 | + this.wrapper.appendChild(this.enabledLabel); |
| 68 | + |
| 69 | + this.enabled.addEventListener('change', (event) => { |
| 70 | + if (event.currentTarget.checked) { |
| 71 | + this.enable(); |
| 72 | + } else { |
| 73 | + this.disable(); |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + enable = () => { |
| 79 | + window.addEventListener('deviceorientation', this.handleOrientationEvent); |
| 80 | + // deviceorientation events don't fire when window blurs, so reset controls |
| 81 | + window.addEventListener('blur', this.orientationReset); |
| 82 | + |
| 83 | + this.showOrientationDisplay(); |
| 84 | + this.enabledLabel.innerText = 'Enabled'; |
| 85 | + } |
| 86 | + |
| 87 | + disable = () => { |
| 88 | + this.orientationReset(); |
| 89 | + |
| 90 | + window.removeEventListener('deviceorientation', this.handleOrientationEvent); |
| 91 | + window.removeEventListener('blur', this.orientationReset); |
| 92 | + |
| 93 | + this.hideOrientationDisplay(); |
| 94 | + this.enabledLabel.innerText = 'Disabled'; |
| 95 | + } |
| 96 | + |
| 97 | + orientationReset = () => { |
| 98 | + this.handleOrientationEvent({ alpha: 0, beta: 0, gamma: 0 }); |
| 99 | + }; |
| 100 | + |
| 101 | + handleOrientationEvent = (event) => { |
| 102 | + const x = event.beta; // landscape left right |
| 103 | + const y = event.gamma; // landscape forward back |
| 104 | + const z = event.alpha; // landscape rotation |
| 105 | + |
| 106 | + this.updateOrientationDisplay(x, y, z); |
| 107 | + |
| 108 | + eval(` |
| 109 | + const data = ${JSON.stringify({ x, y, z })}; |
| 110 | + ${this.getAttribute('onchange')} |
| 111 | + `); |
| 112 | + } |
| 113 | + |
| 114 | + showOrientationDisplay = () => { |
| 115 | + this.x = document.createElement('p'); |
| 116 | + this.y = document.createElement('p'); |
| 117 | + this.z = document.createElement('p'); |
| 118 | + this.wrapper.appendChild(this.x); |
| 119 | + this.wrapper.appendChild(this.y); |
| 120 | + this.wrapper.appendChild(this.z); |
| 121 | + |
| 122 | + this.updateOrientationDisplay(0, 0, 0) |
| 123 | + } |
| 124 | + |
| 125 | + hideOrientationDisplay = () => { |
| 126 | + this.x.remove(); |
| 127 | + this.y.remove(); |
| 128 | + this.z.remove(); |
| 129 | + } |
| 130 | + |
| 131 | + updateOrientationDisplay = (x, y, z) => { |
| 132 | + this.x.textContent = 'x: ' + x; |
| 133 | + this.y.textContent = 'y: ' + y; |
| 134 | + this.z.textContent = 'z: ' + z; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +window.customElements.define('orientation-component', DeviceOrientation); |
0 commit comments