summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--main.js25
1 files changed, 16 insertions, 9 deletions
diff --git a/main.js b/main.js
index 4a46d96..a1f91d4 100644
--- a/main.js
+++ b/main.js
@@ -30,14 +30,7 @@ function animate() {
}
renderer.setAnimationLoop(animate);
-if ( location.protocol != "https:" ) {
-location.href = "https:" + window.location.href.substring( window.location.protocol.length );
-}
-function permission () {
- if ( typeof( DeviceMotionEvent ) !== "undefined" && typeof( DeviceMotionEvent.requestPermission ) === "function" ) {
- // (optional) Do something before API request prompt.
- DeviceMotionEvent.requestPermission()
- .then( response => {
+const motionEvent = (response) => {
// (optional) Do something after API prompt dismissed.
if ( response == "granted" ) {
window.addEventListener("deviceorientation", (e) => {
@@ -76,8 +69,22 @@ function permission () {
document.getElementById("info").textContent=`x: ${Math.round(cube.position.x * 10)}, y: ${Math.round(cube.position.y * 10)}`;
});
}
- })
+}
+
+if ( location.protocol != "https:" ) {
+location.href = "https:" + window.location.href.substring( window.location.protocol.length );
+}
+function permission () {
+ if ( typeof( DeviceMotionEvent ) !== "undefined") {
+ // (optional) Do something before API request prompt.
+ if(typeof( DeviceMotionEvent.requestPermission ) === "function") {
+ DeviceMotionEvent.requestPermission()
+ .then(motionEvent)
.catch( console.error )
+ }
+ else {
+ motionEvent("granted");
+ }
} else {
alert( "DeviceMotionEvent is not defined" );
}